插入语句,变量失败

时间:2017-09-09 21:11:30

标签: php sql mysqli

这里新的 我遇到过这个问题。

它似乎无法使用我上次语句中的两个第一个语句中的id作为变量资源,因此sqlcharacter语句失败。

我做错了什么?

$sqlimg = ("INSERT INTO cimages(image) VALUES(?)");
$stmtimg = $conn->prepare($sqlimg);
$stmtimg->bind_param('s', $image);
$stmtimg->execute();
$img_id = $stmtimg->insert_id;

// I insert the picture first, and retrieve it's ID

$sqlstats = ("INSERT INTO cstats(Strength, Dexterity, Constitution, 
Intelligence, Wisdom, Charisma, Aligment) VALUES(?, ?, ?, ?, ?, ?, ?)");
$stmtstats = $conn->prepare($sqlstats);
$stmtstats->bind_param("iiiiiis", $strength, $dexterity, $constitution, 
$intelligence, $wisdom, $charisma, $aligment);
$stmtstats->execute();
$stats_id = $stmtstats->insert_id;

// I insert the characters stats, and retrieve it's ID
// Last I insert The user_id and img_id and stats_id
$user_id = mysqli_real_escape_string($conn, $_POST['user_id']);
// I've used the session id to get the user_id already 


$sqlcharacter = ("INSERT INTO characters(Cname, Clast, Crace, house, 
location, Bgstory, user_id, img_id, stats_id) VALUES(?, ?, ?, ?, ?, ?, ?, 
$img_id, $stats_id)");
$stmtChar = $conn->prepare($sqlcharacter);
$stmtChar->bind_param('ssssssiii', $Cname, $Clast, $Crace, $house, 
$location, $Bgstory, $user_id, $img_id, $stats_id);
$stmtChar->execute();

1 个答案:

答案 0 :(得分:1)

$sqlcharacter字符串看起来好像有两个变量$img_id$stats_id而不是?,所以我认为这就是为什么它没有约束这些价值观。

尝试更改此内容:

"INSERT INTO characters(Cname, Clast, Crace, house, 
 location, Bgstory, user_id, img_id, stats_id) VALUES(?, ?, ?, ?, ?, ?, ?, 
 $img_id, $stats_id)"

对此:

"INSERT INTO characters(Cname, Clast, Crace, house, 
 location, Bgstory, user_id, img_id, stats_id) VALUES(?, ?, ?, ?, ?, ?, ?, 
 ?, ?)"