I have a contact saved in database which I am using the code below to update. However, it's not being updated. I have already checked my POST
values to make sure they are not empty. I appreciate any guidance.
$conn=mysqli_connect("localhost","root", "", "contacts");
$sql = "UPDATE contact SET cont_fname='".$_POST['cont_fname']."', cont_family='".$_POST['cont_family']."', cont_phone='".$_POST['cont_phone']."' WHERE cont_id='".$_POST['cont_id']."'";
$add_contact = mysqli_query($conn, $sql) or die(mysqli_error($conn));
Use this
$cont_fname = $_POST['cont_fname'];
$cont_family = $_POST['cont_family'];
$cont_phone = $_POST['cont_phone'];
$cont_id = $_POST['cont_id'];
$mysqli = new mysqli('localhost','root','','contacts');
if ($mysqli->connect_error)
{
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
//MySqli Update Query
$results = $mysqli->query("UPDATE contact SET cont_fname= '$cont_fname', cont_family ='$cont_family', cont_phone='$cont_phone' WHERE cont_id='$cont_id'");
if($results)
{
print 'Success! record updated / deleted';
}
else
{
print 'Error : ('. $mysqli->errno .') '. $mysqli->error;
}
Alternative 1:
$sql = "UPDATE contact SET cont_fname='".$_POST[cont_fname]."',
cont_family='".$_POST[cont_family]."', cont_phone='".$_POST[cont_phone]."'
WHERE cont_id='".$_POST[cont_id]."'";
Alternative 2:
$cont_fname = $_POST['cont_fname'];
$cont_family = $_POST['cont_family'];
$cont_phone = $_POST['cont_phone'];
$cont_id = $_POST['cont_id'];
$sql = "UPDATE contact SET cont_fname='$cont_fname',
cont_family='$cont_family', cont_phone='$cont_phone' WHERE cont_id='$cont_id'";