Monday 3 September 2018

Mysqli multiple query does not update all fields

I will preface by stating I am fairly new at any type of programming. I am working on a school assignment for video store inventory system, and I am having trouble with a multi query statement. I am attempting to update 2 fields in different tables. I have successfully updated the video table's inventory with an increment operator, but the rental table's return date will not update. I do not get any errors. However, there is not change in the empty field in the database.

I'll upload what I believe to be relevant code.
This is the HTML for the form
<form class="form-horizontal" name="returnform" id="returnform" action="" method="post">
    <div align="center" class="form-group">
        <label class="col-sm-2">Rental number</label>
        <div class="col-sm-10">
            <input class="form-control" name="Rental_ref_num" type="text" name="s" placeholder="Enter reference number..." required="">
        </div>
    </div>

    <input id="Returned_date" name="Returned_date" type="hidden">
    <input id="Movie_id" name="Movie_id" type="hidden">

    <div align="center">
        <input type="submit" value="Return">
    </div>
</form>

I have some Javascript which sets the current date and sends it to a hidden form field.
<script>
    var mydate=new Date()
    var theyear=mydate.getYear()
    if (theyear < 1000)
        theyear+=1900
    var theday=mydate.getDay()
    var themonth=mydate.getMonth()+1
    if (themonth<10)
        themonth="0"+themonth
    var theday=mydate.getDate()
    if (theday<10)
        theday="0"+theday

    var displayfirst=theyear
    var displaysecond=themonth
    var displaythird=theday

document.returnform.Returned_date.value=displayfirst+displaysecond+displaythird
The PHP and queries
<?php
if ( ! empty( $_POST ) ) {
    $mysqli = new mysqli( 'localhost', 'root', 'AMerica12', 'videostoremodel' );

    $rd = $mysqli->real_escape_string($_POST['Returned_date']);
    $rrf = $mysqli->real_escape_string($_POST['Rental_ref_num']);
    ////////////////////////////////////////////////////////////////////

    $result = mysqli_query($mysqli,"SELECT Movie_id FROM rental_table where Rental_ref_num ='".$rrf."'");

    $row = mysqli_fetch_array($result);
    $name = $row['Movie_id'];
    /////////////////////////////////////////////////////////////////////
    $sql = "UPDATE `rental_table`
                SET (`Returned_date` = '$rd')
                WHERE (`Rental_ref_num` = '$rrf');";

    $sql = "UPDATE `video_table`
                SET `Amount_inventory` = `Amount_inventory` + 1
                WHERE (`Movie_id` = '$name');";

    $result = mysqli_multi_query($mysqli, $sql);

I'm sorry for the long post. However, I've searched for hours, and no matter what, I cannot figure out why the return date will not update in the rental_table. Once again, I get no error prompts.
Any help would be greatly appreciated.

There are multiple problems. One is the brackets you have in the first query in the SETstatement. There is no reason for brackets there and they're not allowed there. use this instead:
$sql = "UPDATE `rental_table`
        SET `Returned_date` = '$rd'
        WHERE (`Rental_ref_num` = '$rrf');"

Second you have to concatenate the queries, at the moment you're overwriting the first query with the second one. so use this:
$sql = "UPDATE `rental_table`
        SET `Returned_date` = '$rd'
        WHERE (`Rental_ref_num` = '$rrf');";

$sql .= "UPDATE `video_table`
            SET `Amount_inventory` = `Amount_inventory` + 1
            WHERE (`Movie_id` = '$name');";

$result = mysqli_multi_query($mysqli, $sql)

Instead of doing that i would suggest to execute 2 seperate queries instead, you can also easily implement error catching this way:
$sql = "UPDATE `rental_table`
        SET `Returned_date` = '$rd'
        WHERE (`Rental_ref_num` = '$rrf');";
if (!mysqli_query($mysqli, $sql)) {
    echo "Errormessage: ". mysqli_error($mysqli);
}

$sql = "UPDATE `video_table`
            SET `Amount_inventory` = `Amount_inventory` + 1
            WHERE (`Movie_id` = '$name');";
if (!mysqli_query($mysqli, $sql)) {
    echo "Errormessage: ". mysqli_error($mysqli);
}

0 comments:

Post a Comment