gpt4 book ai didi

php - 帮我删除 mysql_insert_id

转载 作者:行者123 更新时间:2023-11-29 14:49:58 25 4
gpt4 key购买 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 .= "<p class=\"alert\">Success - External blog Added!</p>";

/// 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 .= "<p class=\"alert\">This tag didn't exist - so I inserted a new tag</p>";
}

/// 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,
PRIMARY KEY (`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,
PRIMARY KEY (`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,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

最佳答案

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com