dsw7547
dsw7547
2017-09-09 21:11

插入语句,变量失败

已采纳

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();
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongzhui2636 dongzhui2636 4年前

    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(?, ?, ?, ?, ?, ?, ?, 
     ?, ?)"
    
    点赞 评论 复制链接分享

相关推荐