PHP Syntax error (bind_param)

Helo. I’m trying to do a mysql UPDATE query without success.
I hope someone can help me. Here is my code:

$db = new mysqli(HOST, USER, PASS, DBNAME);
$query = (“UPDATE cegek SET CegNev='”.$cegnev.“‘, Kozpont=’”.$kozpont.“‘, Bevetel=’”.$bevetel.“‘, Alkalmazottak=’”.$alkalmazott.“‘, Iparag=’”.$iparag.“’ WHERE id=”.$id.“'”);
$conn = $db->prepare($query);
$conn->bind_param(“ssiisi”, $cegnev, $kozpont, $bevetel, $alkalmazott, $iparag, $id);
$db->close();

Gives the following error:

Fatal error: Call to a member function bind_param() on boolean in

Anyone have an idea how to solve it?

I think (but I’ve been wrong before) that if $conn is a Boolean, that’s because the prepare() function failed.

In any case, if you’re using bound parameters, don’t you need to use the ? placeholder inside the query, rather than appending the PHP variables as you are doing here?

Also I suspect your quotations are unbalanced in the query, particularly around the id section at the end.

1 Like

I’ve done it successfully.

Here is the working code:

$db = new mysqli(HOST, USER, PASS, DBNAME);
$q = (‘UPDATE cegek SET CegNev=?, Kozpont=?, Bevetel=?, Alkalmazottak=?, Iparag=? WHERE ID=?’);
$conn = $db->prepare($q);
$conn->bind_param(“ssiisi”, $cegnev, $kozpont, $bevetel, $alkalmazott, $iparag, $id);
$conn->execute();
$db->close();

You also don’t need

$db->close

As PHP will do this automatically for you and it’s bad practice to open a new connection every time you want to connect to the database. So you are better off having it opened.

1 Like

Thanks!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.