gpt4 book ai didi

php mysql 不插入数据

转载 作者:行者123 更新时间:2023-11-29 20:54:09 24 4
gpt4 key购买 nike

我尝试编写 get-methods 将数据插入表中。事实上,一张 table 一切都很顺利。看代码:

<?php
error_reporting(E_ALL & ~E_DEPRECATED);

$db_host = "...";
$db_user = "...";
$db_password = "...";
$db_table = "Task";

$name = $_GET['Name'];
$groupId = $_GET['GroupId'];
$creatorId = $_GET['CreatorId'];
$comment = $_GET['Comment'];

$db = mysql_connect($db_host, $db_user, $db_password) OR DIE("DB connection fail...");
mysql_select_db("...", $db);
mysql_query("SET NAMES 'utf8'", $db);

$result = mysql_query ("INSERT INTO ".$db_table." (Name, Group_ID, Creator_ID, Comment) VALUES ('$name', '$groupId', '$creatorId', '$comment')");
$id = mysql_insert_id();

header('Content-Type: application/json');


if ($result = 'true'){
$response = array( 'result' => 'OK', 'id' => $id );
//setcookie("TaskManagerUser", $id);
echo json_encode($response);
} else{
$response = array( 'result' => 'FAIL');
echo json_encode($response);
}
?>

但是当我尝试插入另一个表“组”时,什么也没有发生。 mysql_insert_id() 中的 id 始终为 0;两个表中的主键都是 AU 并且是唯一的

<?php
error_reporting(E_ALL & ~E_DEPRECATED);

$db_host = "...";
$db_user = "...";
$db_password = "...";
$db_table = "Group";

$name = $_GET['Name'];

$db = mysql_connect($db_host, $db_user, $db_password) OR DIE("DB connection fail...");
mysql_select_db("...", $db);
mysql_query("SET NAMES 'utf8'", $db);

$result = mysql_query("INSERT INTO ".$db_table." (Name) VALUES ('$name')");

$id = mysql_insert_id();

header('Content-Type: application/json');

if ($result = 'true'){
$response = array( 'result' => 'OK', 'id' => $id );
//setcookie("TaskManagerUser", $id);
echo json_encode($response);
} else{
$response = array( 'result' => 'FAIL');
echo json_encode($response);
}
?>

最佳答案

对于那些决定创建名为“Group”的表的人 - 不要这样做!这就是重点。我更改了它的名称并且成功了!

关于php mysql 不插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37765842/

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