sách gpt4 ai đã đi

php - mysql_insert_id();成功插入行后未返回值

In lại 作者:行者123 更新时间:2023-11-30 00:47:56 25 4
mua khóa gpt4 Nike

我发誓我已经在这个网站和其他网站上的所有其他类似问题上倾注了很多……但我想我只是想念一些东西。希望有人可以指出一个愚蠢的错误,那就是我的大脑对我隐藏了。 :)

我的脚本将表单中的值插入称为“ notes”的表中

那时,它通过称为newRelationship的函数在称为“ relationships”的表中创建两个条目。

变量“ $ note_id”的值通过我的mysql_insert_id()填充;并传递给上面的函数

执行代码后,该条目将成功添加到“注释”,并且为“ id”列使用auto_increment赋予适当的值。

接下来,将两个条目添加到“关系”表中(它们应该如此)。

但是,我的mysql_insert_id()总是将值“ 0”而不是新的行ID踢出。

对于我的一生,我无法弄清楚自己在做错什么。我什至尝试从头开始创建具有相同结果的新表。最重要的是,我在项目的其他文件中使用了几乎相同的代码,没有任何问题。有人看到我在做什么错吗?

有问题的代码

    if ($user->is_loaded())
{

if($_POST['project_id']) {
$project_id = $_post['project_id'];
$long_note = $_POST['long_note'];
$created_by = $_POST['created_by'];
$note_sql="INSERT INTO notes (`long_note`, `added`, `created_by`) VALUES ('$long_note', '$timenow', '$created_by')";
if (!mysql_query($note_sql,$con))
{
die('Error: ' . mysql_error($note_sql));
}
else {
echo "note created Creating relationship ";
$note_id = mysql_insert_id();

echo $note_id;
newRelationship($project_id, "project", $note_id, "note");
newRelationship($client_id, "client", $note_id, "note");
echo "note added successfuly";

}


而我的功能

function newRelationship($parent_id, $parent_type, $child_id, $child_type)
{

global $sql, $con, $timenow;

$relationship_sql="INSERT INTO `relationships` (`parent_id`, `parent_type`, `child_id`, `child_type`) VALUES ('$parent_id', '$parent_type', '$child_id', '$child_type');";
if (!mysql_query($relationship_sql,$con))
{
die('Error: ' . mysql_error($relationship_sql));
}
else {
echo $parent_type." ".$parent_id." realationship with ".$child_type." ".$child_id." successful ";
}

}


每个@jack的建议是我的笔记表的sql

CREATE TABLE `notes` (
`id` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
`contact_id` int(10) NOT NULL,
`client_id` int(10) NOT NULL,
`status` text NOT NULL,
`long_note` text NOT NULL,
`added` int(11) NOT NULL,
`modified` int(11) NOT NULL,
`edit_by` int(10) NOT NULL,
`short_note` text NOT NULL,
`created_by` int(10) NOT NULL,
KHÓA CHÍNH (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=295 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC

1 Câu trả lời

theotài liệu:


  由上一个查询为AUTO_INCREMENT列生成的ID
  成功,如果前一个查询未生成AUTO_INCREMENT,则返回0
  值,如果未建立MySQL连接,则返回FALSE。


文档指出,如果上次执行的查询未生成0值,则只能返回AUTO_INCREMENT, có nghĩa lànotes中的PRIMARY KEY列未正确设置为auto_increment。我建议仔细检查notes中的PRIMARY KEY列是否是使用auto_increment进行的实际设置(再也不用担心再次检查!)。

查看示例代码,在插入后立即调用mysql_insert_id(),因此它们之间不应有任何冲突的查询来倾斜结果。

我看到的唯一可能引起问题的是,您正在将MySQL资源传递给mysql_query(),而不是传递给mysql_insert_id():

if (!mysql_query($note_sql,$con))
...
$note_id = mysql_insert_id();


因此,资源可能存在冲突。尝试将您的通话更新为:

$note_id = mysql_insert_id($con);

关于php - mysql_insert_id();成功插入行后未返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21210288/

25 4 0
行者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