Mysqli多查询不更新所有字段

时间:2016-04-14 05:41:01

标签: php mysql html5 mysqli

我将在前言中说明我对任何类型的编程都相当新。 我正在为视频商店库存系统进行学校作业,我遇到了多查询语句的问题。我试图更新不同表中的2个字段。我已使用增量运算符成功更新了视频表的广告资源,但租借表的返回日期不会更新。我没有得到任何错误。但是,数据库中的空字段没有变化。

我会上传我认为相关的代码。

这是表单的HTML

<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>

我有一些Javascript设置当前日期并将其发送到隐藏的表单字段。

<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     

PHP和查询

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

对不起,我很抱歉。但是,我已经搜索了几个小时,无论如何,我都无法弄清楚为什么返回日期不会在rental_table中更新。再一次,我没有得到任何错误提示。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

有很多问题。一个是SET语句中第一个查询中的括号。没有理由在那里括号,他们不允许在那里。请改用:

$sql = "UPDATE `rental_table` 
        SET `Returned_date` = '$rd'
        WHERE (`Rental_ref_num` = '$rrf');"

其次,您必须连接查询,此时您将使用第二个查询覆盖第一个查询。所以用这个:

$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)

而不是这样做我建议执行2个单独的查询,你也可以通过这种方式轻松实现错误:

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

答案 1 :(得分:0)

您的代码中存在一个问题:您只运行一个sql update查询。只需替换原始代码:

$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)

使用以下代码:

$sql = "UPDATE `rental_table` 
        SET (`Returned_date` = '$rd') 
        WHERE (`Rental_ref_num` = '$rrf');";
$result = mysqli_multi_query($mysqli, $sql)

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