-1

hey there i just want to insert some value´s in my database and i used this code:

define('SECURE', true);
include "storescripts/connect_to_mysql.php";

if (!$mysqli) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$stmt = mysqli_prepare($mysqli, "INSERT INTO `trans` VALUES (?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'sssd', $txn_id, $payer_email, $mc_gross);

$txn_id = 123456789;
$payer_email = 'someone@example.com';
$mc_gross = 100;

/* execute prepared statement */
mysqli_stmt_execute($stmt);

printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));

/* close statement and connection */
mysqli_stmt_close($stmt);

After calling this script i get this:

Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given

Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given

Warning: mysqli_stmt_affected_rows() expects parameter 1 to be mysqli_stmt, boolean given 

0 Row inserted. 

Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, boolean given

anybody could tell me why prepared statements do this error´s ? thanks! greetings!

  • Although this **typo** is pretty straightforward, [you have to learn how to get an error message from mysqli](http://stackoverflow.com/a/15447204/285587) – Your Common Sense Aug 01 '13 at 20:07
  • 1
    check `$stmt, 'sssd', $txn_id, $payer_email, $mc_gross)` number of parameters! –  Aug 01 '13 at 20:12
  • 1
    five paramenters, where is the bug? – user2602535 Aug 01 '13 at 20:14
  • WHy do so many people fail to check the MySQLi return value? Look at the value returned by `mysqli_prepare`, and if it's false, look at `mysqli_error`. That will tell you what your problem is. –  Aug 01 '13 at 20:14
  • ok i get this error: Notice: Column count doesn't match value count at row 1[] in but this is from the manual...so the manual is wrong? :http://de3.php.net/manual/en/mysqli-stmt.bind-param.php, so? – user2602535 Aug 01 '13 at 20:21

1 Answers1

0

The mysql_prepare() line is failing and returning false, rather than a resource.

What columns are in the trans table? If you don't have exactly three columns, then that may cause your prepare error. You should always define your columns along with your values in an insert statement:

INSERT INTO `trans` (`txn_id`, `payer_email`, `mc_gross`) VALUES (?, ?, ?)

As long as any other columns existing in the database have either allowed NULL or a default value, then you should be able to insert the data.

EDIT:

I am unsure of whether you can bind the variables before they are set. I would recommend not binding them until after they contain values.

Travis Hegner
  • 2,465
  • 1
  • 12
  • 11
  • this is my table: $sqlCommand = "CREATE TABLE trans ( id int(11) NOT NULL auto_increment, txn_id varchar(255) NOT NULL, payer_email varchar(255) NOT NULL, mc_gross int(255) NOT NULL, PRIMARY KEY (id), UNIQUE KEY (txn_id))"; – user2602535 Aug 01 '13 at 20:27
  • Because your original code doesn't specify which 3 of the 4 columns your trying to insert data into, mysql doesn't know where to put it. Use the insert statement in my answer and it should bypass that error. I'll edit to update the correct column names. – Travis Hegner Aug 01 '13 at 20:36
  • Also look at what Akam commented on your question... you are using 'sssd' in your second parameter where you probably intend to use 'ssd' . – Travis Hegner Aug 01 '13 at 20:40
  • this was BUG =>> 'sssd' ====> 'ssd' – user2602535 Aug 01 '13 at 20:51