简体   繁体   English

从表的每一行更新mysql

[英]updating mysql from table for each row

Im stuck and wanted to see if any one could help me out please. 我困住了,想看看是否有人可以帮助我。 I have built a table in bootstrap v4 which displays products and some data about that product. 我在bootstrap v4中建立了一个表,该表显示产品和有关该产品的一些数据。 I currently have a button (green with pencil) that when clicked on in each row sends me to another page where i can see and update all the info on that product and same for red button to delete the product from data base mysqli. 我目前有一个按钮(带铅笔的绿色),当单击每行时,会将我带到另一个页面,在该页面上我可以查看和更新​​该产品的所有信息,红色按钮也是如此,以从数据库mysqli中删除该产品。 What i am trying to do now is add a save button at the side with the others in blue so that i can alter the price and quantity quickly and save the row like in pic screen shot The first save works and database updates as it supose to but the rest dont. 我现在想做的是在侧面添加一个保存按钮,其他按钮以蓝色显示,以便我可以快速更改价格和数量,并像在pic 屏幕快照中那样保存行。但其余的不。 The save button does nothing at all and no matter what i seem to try, i cant get no where. 保存按钮根本不执行任何操作,无论我尝试执行什么操作,我都无法找到任何位置。

My code: 我的代码:

<?

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

                $price = $_POST["price"];
                $quantity = $_POST["quantity"];
                $id = $_POST["id"];                 

$stmt = $con->prepare("UPDATE products set price=?, quantity=? WHERE id = 
?");    
$stmt->bind_param("ssi", $price, $quantity, $id);
$stmt->execute();
$stmt->close();

header('Location: products.php');
}
?>

  //taken out header of the table       

          <tbody>

            <tr>

<?php
     $raw_results = mysqli_query($con, "SELECT * FROM products") or 
die(mysqli_error($con));
while($results = mysqli_fetch_array($raw_results)){

?>
<form method=post action="">
              <td><input class="form-control size-id" value="<? echo 
$results['id']; ?>" name="id"></td>
              <td><? echo $results['title']; ?></td>
              <td><input class="form-control size-price" type="text" value=" 
              <? echo $results['quantity']; ?>" name="quantity"></td>                 
              <td><? echo $results['rrp']; ?></td>
              <td><input class="form-control size-price" type="text" value=" 
              <? echo $results['price']; ?>" name="price"></td>
              <td>
              <button type=submit name=s1 class="btn btn-outline-primary 
btn-sm btn-block-xs-only"> <i class="fa fa-floppy-o" aria-hidden="true"></i> 
</button> 
              <a href="edit_product.php?id=<?=$results['id']?>" class="btn 
btn-outline-success btn-sm" role="button"><i class="fa fa-pencil" aria- 
hidden="true"></i></a> 
              <a href="delete_product.php?id=<?=$results['id']?>" class="btn 
btn-outline-danger btn-sm" role="button"><i class="fa fa-times" aria- 
hidden="true"></i></a>
              </td>
 </tr>
             <?
  }
?>
          </tbody>

You are going to wan't to use AJAX (Asynchronous JavaScript And XML) for this 您将不会为此使用AJAX(异步JavaScript和XML)

https://www.w3schools.com/xml/ajax_intro.asp https://www.w3schools.com/xml/ajax_intro.asp

This way you can update your quantity and price without having the need to refresh the entire page. 这样,您可以更新数量和价格,而无需刷新整个页面。 Also there is no need for a form tag with table cells in them which is invalid. 此外,也不需要其中包含表格单元格的表单标签。

Please read through this tutorial and you will have an idea what to do. 请通读本教程,您将知道该怎么做。 If not i could even provide you with a small code example. 如果没有,我什至可以为您提供一个小的代码示例。

The following script will alert the row_id and quantity you send to the server, without the need of a form. 以下脚本将警告您发送到服务器的row_id和数量,而无需表单。

Not that in my example i put the row_id by hand, of course your PHP script would take care of such things 并非在我的示例中我手动放置了row_id,当然您的PHP脚本会处理这些事情

Example (assumes index.html and echo.php are in same folder): 示例(假设index.html和echo.php在同一文件夹中):

index.html index.html

<html>
<head>
    <title>Example AJAX</title>
</head>
<body>

<table>
    <tr>
    <td>Row 1</td>
    <td><input id="quantity_1" type="text" size="3" name="quantity_1" /></td>
    <td><img src="save.png" onclick="save(1);" /></td>
    </tr>
    <tr>
    <td>Row 2</td>
    <td><input id="quantity_2" type="text" size="3" name="quantity_2" /></td>
    <td><img src="save.png" onclick="save(2);" /></td>
    </tr>
</table>

<script type="text/javascript">
    function save(row_id) {
    var quantity = document.getElementById('quantity_' + row_id).value;
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            alert(this.responseText);
        }
    };
    xhttp.open("GET", "echo.php?row_id=" + row_id + "&quantity=" + quantity, true);
    xhttp.send();
    }
</script>
</body>
</html>

echo.php echo.php

<?php

echo 'row id: ' . intval($_GET['row_id']);
echo ' quantity: ' . intval($_GET['quantity']);

Well echo.php could be your script that saves the submitted values ;) But i leave that part upto you. 好吧,echo.php可能是您保存提交值的脚本;)但是,我将这一部分留给您。 Be sure to validate input. 确保验证输入。 Never put $_GET vars straight in a SQL query. 切勿将$ _GET vars直接放在SQL查询中。

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

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