Prepared statements in mysqli do not work

Can someone explain to me why this is not working. Trying to convert into prepared statements in accordance with the advice of everyone, but get stuck right at the beginning ... the connection is fine and it does not return a message, but there is no (in my table called nametable)

<?php $dbhost = "localhost"; $dbuser = "root"; $dbpass = "fidelio"; $dbname = "test"; $con = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); if(mysqli_connect_errno()) { die("Database connection failed: " . mysqli_connect_error() . " (" . mysqli_connect_errno() . ")"); } $query = "INSERT INTO nametable (fname, lname) values (?,?)"; $stmt = mysqli_prepare($con, $query); $firstName = "simon"; $lastName = "morris"; mysqli_stmt_bind_param($stmt,"ss",$firstname, $lastname); mysqli_stmt_execute($stmt); printf("Error: %s.\n", $stmt->error); $stmt->close(); ?> 

I added the last 2 lines and the error returned

Error:.

This works fine, but prepared statements are not ... does anyone know why?

  $sql="INSERT INTO nametable (fname, lname) VALUES ('$firstName', '$lastName')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } 
+4
source share
1 answer

try it

  $dbhost = "localhost"; $dbuser = "root"; $dbpass = "fidelio"; $dbname = "test"; $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname); $firstName = "simon"; $lastName = "morris"; if ($stmtb = $mysqli->prepare("INSERT INTO nametable (fname, lname) values (?,?)")) { $stmtb->bind_param('ss',$firstName, $lastName); $stmtb->execute(); }else {printf("Prepared Statement Error: %s\n", $mysqli->error);} 
+2
source

Source: https://habr.com/ru/post/1493007/


All Articles