I'm trying to update a row in a table using PHP, although I'm this is not my first time, but the update function is not working.
This is the php code:
<?php
header("Access-Control-Allow-Origin: *");
$server = "localhost";
$username = "username";
$password = "dbpass";
$database = "dbname";
$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());
mysql_select_db($database, $con);
$q=mysqli_query("UPDATE `phonegap_login` SET `firstname` = 'Alhia' WHERE `reg_id` =50;");
if($q)
{
echo "success";
}
else
{
echo "failed";
}
?>
And by the way, if you try INSERT into, it will work:
$q=mysql_query("insert into `phonegap_login` (`reg_date`, `firstname`, `lastname`, `email`, `password`) values ('test', 'test', 'test', 'test', 'test')");
How do I fix it? the insert is working, updating a row is not. I'm using a row with reg_id=50 for testing.
You're mixing mysqli_query and mysql_query. Only use mysqli_query. mysql_query is deprecated
$con = mysqli_connect($server, $username, $password) or die ("Could not connect: " . mysqli_error());
mysqli_select_db($database, $con);
$q=mysqli_query("UPDATE `phonegap_login` SET `firstname` = 'Alhia' WHERE `reg_id` =50;");
Or, Object Oriented:
$mysqli = new mysqli($server, $username, $password, $database);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$q = $mysqli->query("UPDATE `phonegap_login` SET `firstname` = 'Alhia' WHERE `reg_id` =50;");
Or, PDO:
$connectionString = 'mysql:host=' . $server . ';dbname='.$database;
try {
$db = new PDO($connectionString, $user, $pass);
} catch ( PDOException $e) {
echo "Failed to connect to Database: (" . $e->getCode() . ") " . $e->getMessage();
}
$q = $db->query("UPDATE `phonegap_login` SET `firstname` = 'Alhia' WHERE `reg_id` =50;");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.