0

not able to insert data in table I am using mysql database

$conn = new mysqli("localhost","username","password", "dbname");

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$stmt = $conn->prepare("INSERT INTO mail_sent(mid, miid,status) VALUES (:mid, :miid,:status)");
$stmt->bind_param('dds',$mail_id, $inv_id, $mailStatus);//line 37
$stmt->bindParam(':mid', $mail_id);
$stmt->bindParam(':miid', $inv_id);
$stmt->bindParam(':status', $mailStatus);
$stmt->execute();

Fatal error: Call to a member function bind_param() on a non-object in C:\wamp\www\mail\toinvite.php on line 37

CoderPi
  • 12,985
  • 4
  • 34
  • 62
xrcwrn
  • 5,339
  • 17
  • 68
  • 129

2 Answers2

1

Your problem is that you are confusing PDO with MySQLi. Only PDO have the syntax :mid, MySQL/MySQLi can't understand this. For prepared statements in MySQLi, you'll have to use ? as placeholders.

if ($stmt = $conn->prepare("INSERT INTO mail_sent (mid, miid, status) VALUES (?, ?, ?)")) {
    $stmt->bind_param('iis', $mail_id, $inv_id, $mailStatus);
    $stmt->execute();
}

As you can see, MySQLi handles all bindings in a single line, a little different than what PDO does. Also, your database has the IDs as ints, so I assigned them as integers in the bind_param too (you previously used doubles).

  • i corresponding variable has type integer
  • d corresponding variable has type double
  • s corresponding variable has type string
  • b corresponding variable is a blob and will be sent in packets

Reference:

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • but it is executing 4 times?? – xrcwrn Jan 18 '16 at 11:44
  • Then you have a loop of sorts (`for`, `while`, `foreach`) wrapped around it (or load the script more times), because this code alone will only execute once. – Qirel Jan 18 '16 at 12:06
0

Try to do right after the prepare statement.

var_dump($stmt);

I am sure that the prepare statement is failing(i.e. returning FLASE). and if that fails you cannot call bind_param() on it. There might be an mismatch in the column names you have in DB or the Table name

and are you sure mid, miid are double and not integers as you are using 'd' to bind the params.

  • showing boolean false – xrcwrn Jan 16 '16 at 08:32
  • yes so there is a problem with the prepare statement. Can you please add the Table definition as well for the table mail_sent – Kumar Saurabh Sinha Jan 16 '16 at 08:34
  • mail_sent(id bigint(20),mid bigint(20),miid bigint(20),add_date timestamp,STATUS varchar(20)); – xrcwrn Jan 16 '16 at 08:43
  • create table mail_mailid_invitation( id bigint( 20 ) unsigned NOT NULL AUTO_INCREMENT , mid bigint(20) unsigned NOT NULL, miid bigint(20) unsigned NOT NULL, status varchar(20) NOT NULL default 'fail', add_date timestamp DEFAULT CURRENT_TIMESTAMP); – xrcwrn Jan 16 '16 at 08:45
  • okay, please show me how do you assign the vaiables $mail_id & $inv_id. Try using $mail_id = 'some int value' and $inv_id = 'some int value' and also use 's' (as string param) instead of 'd' (double) in the bind_param call – Kumar Saurabh Sinha Jan 16 '16 at 09:04
  • so it should look like $stmt->bind_param('sss',$mail_id, $inv_id, $mailStatus); and $mail_id & $inv_id make it as string – Kumar Saurabh Sinha Jan 16 '16 at 09:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100832/discussion-between-saurabh-sinha-and-xrcwrn). – Kumar Saurabh Sinha Jan 16 '16 at 09:07