插入语句,变量失败
问题描述:
new here I've come across this problem.
It doesn't seem to be able to use my id's from the two first statements in my last statements as a variable resource, so the sqlcharacter statement fails.
What do i do wrong?
$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();
new here 我遇到过这个问题。 p>
它似乎无法将我上一个语句中的两个第一个语句中的id作为变量资源使用,因此sqlcharacter语句失败。 p> \ n
我做错了什么? p>
$ sqlimg =(“INSERT INTO cimages(image)VALUES(?)”);
$ stmtimg = $ conn-> prepare($ sqlimg);
$ stmtimg-> bind_param('s',$ image);
$ stmtimg-> execute();
$ img_id = $ stmtimg-> insert_id;
//我先插入图片, 并检索它的ID
$ sqlstats =(“INSERT INTO cstats(力量,敏捷,体质,
智能,智慧,魅力,讽刺)值(?,?,?,?,?,?,?)”) ;
$ stmtstats = $ conn-> prepare($ sqlstats);
$ stmtstats-> bind_param(“iiiiiis”,$ strength,$ dexterity,$ constitution,
$ intelligence,$ wisdom,$ charisma, $ aligment);
$ stmtstats-> execute();
$ stats_id = $ stmtstats-> insert_id;
//我插入字符统计信息,并检索它的ID
//最后我插入 user_id和img_id和stats_id
$ user_id = mysqli_real_escape_string($ conn,$ _POST ['user_id']);
//我已经使用会话ID来获取user_id
$ sqlcharacter = (“INSERT INTO字符(Cname,Clast,Crace,house,
location,Bgstory,user_id,img_id,stats_id) )VALUES(?,?,?,?,?,?,?,
$ img_id,$ stats_id)“);
$ stmtChar = $ conn-> prepare($ sqlcharacter);
$ stmtChar-&gt ; bind_param('ssssssiii',$ Cname,$ Clast,$ Crace,$ house,
$ location,$ Bgstory,$ user_id,$ img_id,$ stats_id);
$ stmtChar-> execute();
code> pre>
div>
答
The $sqlcharacter
string looks like you've got two variables $img_id
and $stats_id
in there instead of ?
, so I think that's why it's not binding those values.
Try changing this:
"INSERT INTO characters(Cname, Clast, Crace, house,
location, Bgstory, user_id, img_id, stats_id) VALUES(?, ?, ?, ?, ?, ?, ?,
$img_id, $stats_id)"
To this:
"INSERT INTO characters(Cname, Clast, Crace, house,
location, Bgstory, user_id, img_id, stats_id) VALUES(?, ?, ?, ?, ?, ?, ?,
?, ?)"