1

I try to use mysqli in order to insert data in my database. But does not work. Where may be the error?

$myDb = new mysqli($hostname, $username, $password, $database);

if($myDb->connect_errno > 0){
    die('Unable to connect to database [' . $myDb->connect_error . ']');
}

$statment = $myDb->prepare("INSERT INTO user(name,surname,age)
VALUES (?,?,?)");
  $statement->bind_param('s', $_POST['name']);
  $statement->bind_param('s', $_POST['surname']);
  $statement->bind_param('i', 25);


  $statement->execute();

  $statement->free_result();

EDIT:

I obtain this error:

Binding parameters failed: (0) Execute failed: (2031) No data supplied for parameters in prepared statement
3
  • What does mysqli_error() tell you? Commented Apr 29, 2013 at 12:12
  • what error do you get? Commented Apr 29, 2013 at 12:13
  • Binding parameters failed: (0) Execute failed: (2031) No data supplied for parameters in prepared statement Commented Apr 29, 2013 at 18:13

2 Answers 2

2

You've got the error here:

$statement->bind_param('i', 25);

25 is not a variable. You can only use variables when binding parameters. You can't use constants nor fixed strings or numbers when binding.

Besides, it never worked for me to split the parameters when binding. I got an error. I need to do so:

$myDb = new mysqli($hostname, $username, $password, $database);

if($myDb->connect_errno > 0){
    die('Unable to connect to database [' . $myDb->connect_error . ']');
}

$statement = $myDb->prepare("INSERT INTO user (name,surname,age) VALUES (?,?,25)");
$statement->bind_param('ss', $_POST['name'], $_POST['surname']);

$statement->execute();

$statement->free_result();
$statement->close();
Sign up to request clarification or add additional context in comments.

Comments

-1

I solved the problem using a correct bind of parameter. Here the correct code:

$myDb = new mysqli($hostname, $username, $password, $database);

if($myDb->connect_errno > 0){
    die('Unable to connect to database [' . $myDb->connect_error . ']');
}

$statment = $myDb->prepare("INSERT INTO user(name,surname,age)
VALUES (?,?,?)");
  $statement->bind_param('s', $name);
  $statement->bind_param('s', $surname);
  $statement->bind_param('i', $age);

$name = $_POST['name'];
$surname = $_POST['surname'];
$age  = 25;


  $statement->execute();

  $statement->free_result();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.