-1

I am trouble getting my prepared statement to work. It seems like I do something wrong with the if ($conn->query($sql) === TRUE) {. I just need my code to work without passing error code.

"Warning: mysqli::query() expects parameter 1 to be string, object given in"

What am I supposed to do here? Can someone please tell what I need to do?

I have tried different things that is provided on the net without success.

<?php // sqltest.php  
require_once 'login.php';  
print "
        <form method='post'>
        <i><h>EDIT </h></i>
        <br>
        <input type='text' name='username'/>Title
        <br>
        <input type='password' name='password'/>Author
        <br>
        <input type='submit' value='Edit'>
        ";
$conn = new mysqli($hn, $un, $pw, $db);  
if ($conn->connect_error) die($conn->connect_error);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$firstname = "John";
$lastname = "Doe";
$email = "john@example.com";
$sql = $conn->prepare("INSERT INTO classics (author, title, type) VALUES (?, ?, ?)");
$sql->bind_param("sss", $firstname, $lastname, $email);

// set parameters and execute

$sql->execute();


if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

$conn->close();
?>

My code is getting a object and not string

D Malan
  • 10,272
  • 3
  • 25
  • 50
realme
  • 17
  • 4
  • You are probably receiving this error because `$conn->query($sql)` expects a SQL statement string and you are passing in a prepared statement. What are you trying to achieve with your code? – Martin Jul 31 '19 at 10:19
  • 2
    The prepared statement gets executed when you call its `execute` method. Calling `$conn->query($sql)` makes absolutely zero sense here, you are _mixing_ two different approaches. – misorude Jul 31 '19 at 10:20
  • I am trying to just edit my classics table. How can throw SQL statement string ?? – realme Jul 31 '19 at 10:22
  • That is what I am wondering. How do I make this work properly? – realme Jul 31 '19 at 10:22
  • @ misorude ahhhhh, thanks!! I got it now! – realme Jul 31 '19 at 10:24
  • If your problem is solved, mark your question as "closed" – Itamar Mushkin Jul 31 '19 at 10:56

1 Answers1

0

You are passing a prepared statement object into query. Remove that and write $sql->execute() in an if condition like shown below. The code given below will fix your problem.

if($sql->execute()) { 
    echo "Record updated successfully";
} 
else {
    print_r($sql->errorInfo());
}