dt614037527
dt614037527
2016-07-16 02:30

如何在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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douxiong5972 douxiong5972 5年前

    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);
    }
    
    点赞 评论 复制链接分享