sách gpt4 ai đã đi

php - 帮我删除 mysql_insert_id

In lại 作者:行者123 更新时间:2023-11-29 14:49:58 25 4
mua khóa gpt4 Nike

获得三个表(博客、标签和博客标签),所有 AI 和 ID 设置为主键。我正在制作一个标记系统来跟踪我的网站(本地主机)。下面的代码可以工作,但是 mysql_insert_id 似乎不够可靠,因为我得到了一些重复的行,并且其中偶尔有 0 值。

/// inserts the blog into blog table.
$insert = mysql_query("INSERT INTO blogs (id, url, user, pass, dname, islocal, cat2post) VALUES ('', '$blog', '$bloguser', '$blogpassword', '','NO','$_POST[cat2blog]')")or die( 'Error: ' . mysql_error());

$taggit1 = mysql_insert_id();

$page->content .= "

Success - External blog Added!

";

/// let's see what tags we have and explode them.
//$tags = $_POST['tags'] which is an array of words seperated by comma
$tags = 'fishing';
$pieces = explode(",", $tags);

/// go through the tags and add to tags table if needed.
foreach ($pieces as $l){
$l = trim($l);

$query = "SELECT id FROM tags WHERE tag = '$l'";
$result = mysql_query($query) or die( "Error: " . mysql_error() . " in query $query");
$row = mysql_fetch_array($result);
$taggit2 = $row[0];

if ($taggit2 == '') {
$insert2 = mysql_query("INSERT INTO tags (id, tag) VALUES ('','$l')")or die( 'Error: ' . mysql_error());
$taggit2 = mysql_insert_id();
$page->content .= "

This tag didn't exist - so I inserted a new tag

";
}

/// for each tag we have, let's insert the blogstags table so we can reference which blog goes to which tag. Blogstags_id should map to the id of the blog.

$insert3 = mysql_query("INSERT INTO blogstags (id, tag_id, blogstags_id) VALUES ('','$taggit2','$taggit1')")or die( 'Error: ' . mysql_error());

}

我想我需要一个与 mysql_insert_id 不同的解决方案 - 想法?有建议吗?

根据要求的表结构:

CREATE TABLE IF NOT EXISTS `blogs` (
`id` int(11) NOT NULL auto_increment,
`url` text NOT NULL,
`user` text NOT NULL,
`pass` text NOT NULL,
`dname` text NOT NULL,
`islocal` varchar(3) NOT NULL,
`cat2post` int(11) NOT NULL,
KHÓA CHÍNH (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;


CREATE TABLE IF NOT EXISTS `blogstags` (
`id` int(11) NOT NULL auto_increment,
`tag_id` int(11) NOT NULL,
`blogstags_id` int(11) NOT NULL,
KHÓA CHÍNH (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


CREATE TABLE IF NOT EXISTS `tags` (
`id` int(11) NOT NULL auto_increment,
`tag` text NOT NULL,
KHÓA CHÍNH (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

1 Câu trả lời

mysql_insert_id() 工作正常。问题可能是您正在使用持久连接。对于持久连接,可能会发生各种奇怪的并发问题。除非你真的非常需要,否则不要使用它们。

关于php - 帮我删除 mysql_insert_id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5972686/

25 4 0
Bài viết được đề xuất: android - Phonegap/ Cordova 3 Android : Connection is not defined
Bài viết được đề xuất: c# - MySQL 将文件保存到数据库
Bài viết được đề xuất: php - 自动 API 数据库插入?
Bài viết được đề xuất: mysql - CDN 将如何与我的网站配合使用?
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com