MySQL更新查询失败

时间:2017-12-01 08:27:11

标签: php mysql

我正在尝试通过两个不同的文本框更新2列,但在提交后,它在数据库中显示0个值。

HTML code:

<form action="update_quantity.php" method="post" class="c-shop-form-1">
     <tbody>   
          <?php while ($row = mysqli_fetch_array($query))
            { 
               $id2=$row['id'];

               echo " <tr> 

                <td>{$row['name']}

               <td></td>

                 <td>
                        <input type='text' class='form-control c-square c-theme' style='width: 90px;' placeholder='Quantity' name='Quantity' id='Quantity'>
                 </td> 

                </td> 
                <td>
                    <input type='text' class='form-control c-square c-theme' style='width: 90px;' placeholder='Quantity' name='Quantity2' id='Quantity2'>
                </td> 

               </tr>";
            }?> 
        </tbody>        
    </table>
    <button type="submit" value="submit" class="btn c-theme-btn c-btn-square center c-btn-bold c-btn-uppercase">Submit</button>
</form>

PHP代码:upddate_quantity.php

$quant = mysqli_real_escape_string($link, $_POST['Quantity']);
$id1 = mysqli_real_escape_string($link, $_POST['Quantity2']);

$sql = "UPDATE vm_elements SET physicalStock='$quant', transitQuantity='$id1'  WHERE id='1'";
//$res = mysqli_query($link,$sql) or trigger_error(mysqli_error()." in ".$sql);
if(mysqli_query($link, $sql))
{
    echo "Records added successfully.";
} 
else
{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

我需要帮助。我不明白缺少什么。

1 个答案:

答案 0 :(得分:0)

看看这是否适合您 - 至少它应该提供有关错误处理的更多信息。首先,为您的按钮添加一个名称:

SELECT *
FROM products p
     JOIN product_category pc ON p.product_id = pc.product_ID
     JOIN category c ON pc.category_id = c.category_id;

然后你可以试试这个:

<button type="submit" name="RCV" id="RCV" value="submit" class="btn c-theme-btn c-btn-square center c-btn-bold c-btn-uppercase">Submit</button>

如果这不起作用,请使用以下方法查看从表单发送的所有POST变量:

/* Check your mysqli connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

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

    try {

        $physStock = mysqli_real_escape_string($link, $_POST['Quantity']);
        $transQuant = mysqli_real_escape_string($link, $_POST['Quantity2']);    
        $id = 1;        

        $sql = "UPDATE vm_elements SET physicalStock=?, transitQuantity=?  WHERE id=?";

        if(!($stmt = $link->prepare($sql))) {
            echo "Prepare failed: (" . $link->errno . ") " . $link->error;
        }

        if(!$stmt->bind_param('ssi', $physStock, $transQuant, $id)) {
            echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
        }       

        if (!$stmt->execute()) {
            echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
        } else {
            echo "Records added successfully.";
        }

        $stmt->close();

    } catch (mysqli_sql_exception $e){
        //Handle exception $e
    }
}

实际上再次查看它 - 它可能更多地与HTML相关而不是PHP脚本。表格内部有一个表格 - 就在tbody标签的外面,在它之外结束。你应该重新安排你的html标签 - 将整个表格放在表格中并试一试。

 print_r($_POST)