简体   繁体   English

在同一脚本中执行两个查询

[英]Execute Two query in the same script php

I have made one script which is suppose to update 2 different table at the same time. 我制作了一个脚本,该脚本应该同时更新2个不同的表。 The problem is that only one table get updated and I cant get why? 问题是只有一个表被更新,而我却不能为什么?

Logic is: 逻辑是:

The script should update the record of job.status and customer.status at the same time after I click on update button. 单击更新按钮后,脚本应同时更新job.status和customer.status的记录。

Only the table job get updated. 仅表作业得到更新。

I tried to run only the second query and does not work. 我试图仅运行第二个查询,但不起作用。 I tried to run a query to another table on the same database but is not working. 我试图对同一数据库中的另一个表运行查询,但是无法正常工作。

Any suggestion? 有什么建议吗?

here is my code: 这是我的代码:

     <?php
include("connection.php");

if (isset($_POST['update'])) {
  $results = $link->query("UPDATE job SET status='$_POST[status]', priority='$_POST[priority]' WHERE id='$_POST[hidden]'");
  $results = $link->query("UPDATE customer SET status='$_POST[status]' WHERE id='$_POST[hidden]'");
             }    
$sql = "SELECT * from job";
if (!$result = $link->query($sql)) {
    die('There was an error running the query [' . $link->error . ']');
}
echo "
<table class='table'>
    <thead>
        <tr>";
/* Get field information for all columns */
while ($finfo = $result->fetch_field()) {
    echo "  <th>" . $finfo->name . "</th>";  }
echo "
        </tr>  </thead> <tbody>";

while ($row = $result->fetch_assoc()) {
   $job_id = $row['id'];

    echo "<form action='' method=post>";
    echo "<tr class='info'>

                 <input type=hidden name=hidden value=" . $row['id'] . ">
                <td>" . $row['id'] . "</td> 
                <td>" . $row['device'] . "</td>
                  <td>" . $row['model'] . "</td> 
                <td>" . $row['problem'] . "</td>
            <td>

           <select class='form-control col-sm-10' id='status' name='status'>
                <option value='new' ". ($row['status'] == 'new'? 'selected ': '') .">New</option>
                <option value='progress' ". ($row['status'] == 'progress'? 'selected ': '') .">Progress</option>
                <option  value='wait' ". ($row['status'] == 'wait'? 'selected ': '') .">Wait</option>
                <option value='done' ". ($row['status'] == 'done'? 'selected ': '') .">Done</option>
                <option value='close' ". ($row['status'] == 'close'? 'selected ': '') .">Close</option>
            </select>
            </td>        

          <td> 
             <button type='submit' class='btn btn-primary btn-sm' name='update'>Update</button>
         </td>
          <td> 
                <a class='btn btn-primary btn-sm get_info'  data-toggle='modal' data-target='#myModal' name='job_id'  value= '[$job_id]'> Customer Info</a>              
             </form>                 
                </td>

           </tr>"; 
    echo "</form>";


}
echo "
    </tbody>

</table>";

?>

----UPDATE---- ----更新----

I notice that when i send this query is working. 我注意到当我发送此查询正在工作。

$results = $link->query("UPDATE customer SET status='changethevalue' WHERE id='$_POST[hidden]'");

And when I send this one is working 当我发送此邮件时,它正在工作

 $results = $link->query("UPDATE customer SET status='changevalue'");

----UPDATE 2---- ----更新2 ----

I rewrote the query like this and is apparently working. 我重写了这样的查询,显然可以正常工作。 But I dont know why. 但是我不知道为什么。

if (isset($_POST['update'])) {

  $results = $link->query("UPDATE customer SET status='$_POST[status]' WHERE id='$_POST[hidden]'");

  $results = $link->query("UPDATE job SET status='$_POST[status]' WHERE id='$_POST[hidden]'");

}

There is no issue in the update queries but make sure you are using corect column names or try echo the 2nd query and run in php myadmin f.ex. 更新查询中没有问题,但是请确保您使用的是corect列名,或尝试echo显第二个查询并在php myadmin f.ex中运行。

echo $query="customer SET status='$_POST[status]' WHERE id='$_POST[hidden]'"

then 然后

$results = $link->query($query);

The queries should work. 查询应该工作。 Maybe your table customer is actually customers or the columns status or id are either named differently or have the wrong data type. 也许您的表customer实际上是customers或者列statusid的名称不同或数据类型错误。

I would carefully copy/paste the second query directly into PhpMyAdmin or directly into your database and see whether it gives you any errors. 我会仔细地将第二个查询直接复制/粘贴到PhpMyAdmin或直接复制到您的数据库中,看看它是否给您带来任何错误。 Maybe it's trying to assign a blank status but nulls are not accepted? 也许它正在尝试分配空白状态,但不接受null?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM