如何在php中插入另一个表后更新表

时间:2016-07-16 02:30:48

标签: php mysql mysqli

这是我的php文件... 我想在第二个表成功插入后更新第一个表,我要正确选择和插入,但是在插入数据后我想要更新的行没有得到更新。

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);
    }    

任何人都可以告诉我如何用php mysqli程序方式写这个。

1 个答案:

答案 0 :(得分:0)

您忘了致电mysqli_query()来执行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);
}