PHP不会更新记录,也不会抛出错误

时间:2015-08-11 03:00:37

标签: php mysql mysqli

我正在尝试更新MySQL表中的记录。我没有收到任何错误,我可以看到URL中的值,但我的数据库没有更新

<?php
if(isset($_GET['button1']))
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die('Could not connect: ' . mysql_error());
}

$fname = $_GET['fname'];
$id = $_GET['id'];

$sql = "UPDATE static_monday_count ".
    "SET count  = $fname ".
    "WHERE product_id = $id" ;

mysqli_select_db($conn, 'liquor_inventory');
$retval = mysqli_query($conn, $sql );
if(! $retval )
{
    die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysqli_close($conn);
}
?>



<form method="GET" action=''>
    <td id="Column4"><input type="text" name="fname" value="<?php echo $row["On Hand"] ?>"></td>
    <td id="Column5"><input type="text" name="id" value="<?php echo $row["Id"] ?>"></td>
    <input type="submit" onClick="refreshPage()" name="button1"          value="Calculate">
</form>

当我直接针对我的MySQL数据库运行查询时,它运行没有问题。

这是下拉数据的循环。它带来的数据没有问题。

 <?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "liquor_inventory";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql = "  /* Actual Varience in Units */
select static_monday_count.count as 'On Hand',static_monday_count.Product_id     as 'Id', products.name as 'Products', products.unit as 'Unit', 
cast(((st_inv_p7_wk1_2015.on_hand * measurements.value)
+ (receive_p7_wk1_2015.received * measurements.value)
- (pmix_p7_wk1_2015.sold *
recipes_products.ounces)
- (static_monday_count.count * measurements.value)) / measurements.value as     decimal (10,1)) as 'Variance in Units'
from products
Join st_inv_p7_wk1_2015
On products.id = st_inv_p7_wk1_2015.product_id
Join receive_p7_wk1_2015
On products.id = receive_p7_wk1_2015.product_id
join recipes_products
on products.id = recipes_products.productid
join pmix_p7_wk1_2015
on recipes_products.recipeid = pmix_p7_wk1_2015.recipe_id
join measurements
on products.unit = measurements.name
join static_monday_count
on products.id = static_monday_count.product_id
group by products.id
Order by products.id";
                ?>
                <div class="con_table">
                    <form method="GET" action=''>
                        <div class="inv_table">

                            <table class="table table-bordered">
                                <tr>
                                    <th id="Column4">On Hand</th>
                                    <th id="Column1">Products</th>
                                    <th id="Column2">Unit</th>
                                    <th id="Column3">Variance</th>
                                </tr>
                            </table>
                            <?php

$result = $conn->query($sql);
if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {?>

                            <table class="table-condensed table-bordered">

                                <tr>
                                    <td id="Column4"><input class="oh_input" type="text" name="fname" value="<?php echo $row["On Hand"] ?>"></td>
                                    <td id="Column5"><input type="text" name="id" value="<?php echo $row["Id"] ?>"></td>
                                    <td id="Column1"><?php echo $row["Products"] ?></td>
                                    <td id="Column2"><?php echo $row["Unit"] ?></td> 
                                    <td id="Column3"><?php echo $row["Variance in Units"] ?></td>
                                </tr>
                            </table>
                            <?php
                                     }
} else {
echo "0 results";
}
$conn->close();
                            ?>
                        </div>
                        <input class="cal_btn" type="submit" name="button1" value="Calculate">
                    </form>
                </div>

编辑:将隐藏字段更改为文本以尝试使其生效。

1 个答案:

答案 0 :(得分:1)

  • 将变量绑定到查询时使用撇号('),尤其是当此变量的内容为字符串时
  • 使用mysqli_real_escape_string()清理变量值以阻止SQL injections
  • 你有一个未完成的<input type="hidden" ...。您应该删除它或完成此字段。
  • 您的refreshPage()功能的脚本在哪里?
  • 您的表单是否在while()循环中?因为您尝试在$row["On Hand"]文本字段中添加值fname。并且不要为列名使用空格 - On Hand
  • 请勿将mysqli_* API与mysql_*混合使用。但最好采用prepared statement方法。
  • 检查输入字段中的value标记。 $row["On Hand"]$row["Id"]来自哪里?

样品:

if(isset($_GET['button1']))
{

  /* ESTABLISH CONNECTION */
  $dbhost = 'localhost';
  $dbuser = 'root';
  $dbpass = 'root';
  $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
  if(!$conn)
  {
    die('Could not connect: ' . mysqli_connect_error());
  }

  /* SANITIZE YOUR VARIABLES */
  $fname = mysqli_real_escape_string($conn, $_GET['fname']);
  $id = mysqli_real_escape_string($conn, $_GET['id']);

  /* YOUR QUERY */
  $sql = "UPDATE static_monday_count ".
         "SET count = '$fname' ".
         "WHERE product_id = '$id'";

  mysqli_select_db($conn, 'liquor_inventory'); /* CONNECT TO YOUR DATABASE */
  $retval = mysqli_query($conn, $sql); /* EXECUTE YOUR QUERY */

  if(! $retval ) /* CHECK IF NOT TRUE */
  {
    die('Could not update data: ' . mysqli_error($conn));
  }

  echo "Updated data successfully\n";
  mysqli_close($conn);

}