prashant prashant - 4 months ago 11
PHP Question

how to update table after inserting in another table in php

here is my php file...
i want to update first table after successful insertion in second table,i am to select and insert properly but the row which i want to update is not getting updated after the data is inserted.

if($_SERVER['REQUEST_METHOD']=='POST')
{
$full_name=$_POST['full_name'];
$email_address=$_POST['email_address'];
$contact_number=$_POST['contact_number'];
$gender=$_POST['gender'];
$location=$_POST['location'];
$standard=$_POST['standard'];
$institute=$_POST['institute'];
$code=$_POST['code'];

$sql = "SELECT * FROM activations WHERE code='$code' AND status='not used'";
$check = mysqli_fetch_array(mysqli_query($conn,$sql));
if(isset($check)==null)
{
echo 'exist';
}
else
{
$sql1="INSERT INTO students(full_name, email_address, contact_number, gender, location, standard, institute) VALUES('$full_name','$email_address','$contact_number','$gender','$location','$standard','$institute')";
}
if (mysqli_query($conn, $sql1)==true)
{
$sql2="UPDATE activations SET status='in use' WHERE code='$code';

} else {
echo "Error updating record: " . mysqli_error($conn);
}


can any one tell me how to write this in php mysqli procedural way.

Answer

You forgot to call mysqli_query() to perform the UPDATE.

if (mysqli_query($conn, $sql1)) {
    $sql2="UPDATE activations SET status='in use' WHERE code='$code'";
    if (!mysqli_query($conn, $sql2)) {
        echo "Error updating activations: " . mysqli_error($conn);
    }
} else {
    echo "Error inserting student: " . mysqli_error($conn);
}