James Blue
James Blue

Reputation: 1

Insert statement with variables failing

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();

Upvotes: 0

Views: 29

Answers (1)

Rnice4christ
Rnice4christ

Reputation: 78

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(?, ?, ?, ?, ?, ?, ?, 
 ?, ?)"

Upvotes: 1

Related Questions