INSQ和UPDATE使用mysqli_multi_query不起作用

时间:2018-04-13 14:40:12

标签: php mysql

是否可以在一个insert中拥有updatemysqli_multi_query。数据库更新,但值显示为0

$sql = "SELECT
        e.*,
        c.fee AS coursefee
        FROM enrollment e
            LEFT JOIN courses c ON e.course_id = c.course_id
        WHERE c.course_id = '$courseId'
        ";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result);
$courseFee = $row['coursefee']; // 3500

// fees page
$sql_f = "SELECT * FROM fees WHERE studentid = '$studentId'";
$result_f = mysqli_query($con, $sql_f);

if(mysqli_num_rows($result_f) == 0){
    $query = "INSERT INTO enrollment (student_id, course_id, batch_id, joiningdate) VALUES ('$studentId', '$courseId', '$batchId', '$joiningDate');";
    $query .= "INSERT INTO fees (studentid, coursefee) VALUES ('$studentId','$courseFee')";
}

// The code below is not working.
else{
    $query = "INSERT INTO enrollment (student_id, course_id, batch_id, joiningdate) VALUES ('$studentId', '$courseId', '$batchId', '$joiningDate');";
    $query .= "UPDATE fees SET coursefee='$courseFee' WHERE studentid = '$studentId'";
}

mysqli_multi_query($con, $query);

if(mysqli_affected_rows($con) > 0){
    $_SESSION['success'] = "New Enrollment is Successfully Created!";
    header ("Location: ../../enrollments.php");
}
else {
    echo "Records are NOT Added. Please try again<br />";
    echo mysqli_error ($con);
}

1 个答案:

答案 0 :(得分:0)

这是使用$query = "INSERT INTO enrollment (student_id, course_id, batch_id, joiningdate) VALUES ('$studentId', '$courseId', '$batchId', '$joiningDate');"; $query .= "INSERT INTO fees(fee_id, coursefee) VALUES (LAST_INSERT_ID($feeId), $courseFeeFromFeesTable) ON DUPLICATE KEY UPDATE coursefee = $total";

解决的
{{1}}