$mysqli = new mysqli("localhost", "user", "pass", "db");
if (!$mysqli) {
$mysqli=("UPDATE `home` SET `text`='$text' WHERE `home_id`='$home_id'");
}
mysqli_query($mysqli);
You're overwritting the $mysqli object with a string. I'm also not sure what you are trying to do with the if; mysqli will return an object even if you failed to connect, so you want to use mysqli_connect_error/errno to deal with connection errors.
Procedural mysqli_query will also require two arguments, the first being the mysqli object and the second being the string to execute.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
if ($mysqli->connect_errno) { die('Could not connect to database: ' . mysqli_connect_error($mysqli)); // this bug is fixed in 5.3+ so you can use $mysqli->connect_error() instead if you have 5.3+ }
That is also not the proper use of a prepared statement. If you are not making use of prepared statements (and you should be for anything that accepts variable data), than you may as well just use the query method.
PHP Code:
if ($stmt = $mysqli->prepare("UPDATE `home` SET `text`=? WHERE `home_id`=?")) { $stmt->bind_param('ss', $text, $home_id); $stmt->execute(); $stmt->close(); }
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
The Following 2 Users Say Thank You to Fou-Lu For This Useful Post:
$stmt = $mysqli->prepare("UPDATE `home` SET `text`='$text' WHERE `home_id`='$home_id'");
and yours?
PHP Code:
if ($stmt = $mysqli->prepare("UPDATE `home` SET `text`=? WHERE `home_id`=?"))
{
$stmt->bind_param('ss', $text, $home_id);
}
The difference is pretty obvious .. The first one is a bog standard query and you should be using mysqli_query() for it.
The second one is a prepared statement. You see those ? marks? They are called place holders. This tells mysqli that it is a place holder for data which will be supplied seperately. You then bind your data to the appropriate parameters and supply the data seperately. Mysqli then uses that data and does what the statement was telling it to do with it. As I understand this, it deals with the statement and the actual data seperately thus meaning that the query can't be injected with malicious instructions / data because the data is kept seperate.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
In this case, you have a string and then another string - hence ss. Then you put your types (in this case the ss) and variables in the same order as the statement - Say you had a string and an integer.. you'd use si and then put your $string and $Integer in bind_param() in that order like this:
bind_param('si', $String, $Integer);
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.