php + mysql 编辑\更新表单

标签 php mysql

我尝试在数据库中编辑我的数据,但它崩溃了并说我的变量未定义。输入命令 isset($_POST) 后,我的数据未在数据库中编辑,并显示消息“未更新”。 这是我的编辑文件:

  <?php

 // connect to the database

  include('mysql_connect.php');

// get results from database

  $result = mysqli_query($conn,"SELECT * FROM products")

  or die(mysqli_error());
 ?>

<html>
 <head>
<title>Update Data</title>
<link rel="stylesheet"  href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" >
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
</head>
 <body>
<h1>Product list update</h1>
<hr>
<table style=" width:'50%'!important " class='table'>
  <tr bgcolor='#CCCCCC'>
   // Table 
      <th>SKU</th>
      <th>Name</th>
      <th>Price</th>
      <th>Type</th>
      <th>Details</th>
  </tr>

 <?php
 while ($res = mysqli_fetch_array($result)){

 echo "<tr><form action=update.php method=post>";
 echo "<td><input type=text name=sku_p value='".$res['sku_product']."'></td>";
 echo "<td><input type=text name=name_p value='".$res['name_product']."'></td>";
 echo "<td><input type=text name=price_p value='".$res['price_product']."'></td>";
 echo "<td><input type=text name=type_p value='".$res['type_product']."'></td>";
 echo "<td>
      Size for DVD: <input type=text name=sizedvd_p value='".$res['sizedvd_product']."'><br>
      Weight for Book: <input type=text name=weightbook_p value='".$res['weightbook_product']."'><br>
      For Furniture<br>
      H: <input type=text name=heightfurn_p value='".$res['heightfurn_product']."'><br>
      W: <input type=text name=widthfurn_p value='".$res['widthfurn_product']."'><br>
      L: <input type=text name=lengthfurn_p value='".$res['lengthfurn_product']."'><br></td>";

  echo "<input type=hidden name=id_p value='".$res['id_product']."'>";

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

  }
  ?>
 <a href="index.php">Home</a><br>
 <a href="update.php"><input type="submit" name="update" value="Submit"></a>
   </table>
   </body>
 </html>

这是 update.php 文件,无法将我的数据更新到数据库

<?php 
// connect to the database

$conn = mysqli_connect('localhost', 'root', '');

//Select DB
mysqli_select_db($conn, 'product_list');


// Update (in this case i have problems, which can't give an opportunity to change data in DB)

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

 $result = mysqli_query($conn, "UPDATE products SET name_product='$_POST[name_p]', price_product='$_POST[price_p]', sku_product='$_POST[sku_p]', type_product='$_POST[type_p]',
    sizedvd_product='$_POST[sizedvd_p]', weightbook_product='$_POST[weightbook_p]', heightfurn_product='$_POST[heightfurn_p]', widthfurn_product='$_POST[widthfurn_p]', lengthfurn_product='$_POST[lengthfurn_p]'
    WHERE id_product='$_POST[id_p]' ");

}
   echo "Not Update";

?>

最佳答案

尝试下面的代码,看看它是否有效。让我知道您遇到了什么错误。

<?php
// connect to the database
include('mysql_connect.php');
// get results from database
$result = mysqli_query($conn,"SELECT * FROM products");
or die(mysqli_error());
?>
<html>
<head>
    <title>Update Data</title>
    <link rel="stylesheet"  href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" >
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
</head>
<body>
    <h1>Product list update</h1>
    <hr>
    <table style=" width:'50%'!important " class='table'>
        <tr bgcolor='#CCCCCC'>
            <th>SKU</th>
            <th>Name</th>
            <th>Price</th>
            <th>Type</th>
            <th>Details</th>
        </tr>
        <?php while ($res = mysqli_fetch_array($result)) { ?>
        <tr>
            <form action="update.php" method="post">
                <td><input type="text" name="sku_p" value="<?php echo $res['sku_product']; ?>"></td>
                <td><input type="text" name="name_p" value="<?php echo $res['name_product']; ?>"></td>
                <td><input type="text" name="price_p" value="<?php echo $res['price_product']; ?>"></td>
                <td><input type="text" name="type_p" value="<?php echo $res['type_product']; ?>"></td>
                <td>
                    Size for DVD: <input type="text" name="sizedvd_p" value="<?php echo $res['sizedvd_product']; ?>"><br>
                    Weight for Book: <input type="text" name="weightbook_p" value="<?php echo $res['weightbook_product']; ?>"><br>
                    For Furniture<br>
                    H: <input type="text" name="heightfurn_p" value="<?php echo $res['heightfurn_product']; ?>"><br>
                    W: <input type="text" name="widthfurn_p" value="<?php echo $res['widthfurn_product']; ?>"><br>
                    L: <input type="text" name="lengthfurn_p" value="<?php echo $res['lengthfurn_product']; ?>"><br>
                </td>
                <input type="hidden" name="id_p" value="<?php echo $res['id_product']; ?>">
                <input type="submit" name="update" value="Submit">
            </form>
        </tr>
        <?php } ?>
        <a href="index.php">Home</a>
    </table>
</body>
</html>

和你的update.php

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

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

if (isset($_POST['update'])) {
    $sql = "UPDATE products SET name_product = '$_POST[name_p]', price_product = '$_POST[price_p]', sku_product = '$_POST[sku_p]', type_product = '$_POST[type_p]', sizedvd_product = '$_POST[sizedvd_p]', weightbook_product = '$_POST[weightbook_p]', heightfurn_product = '$_POST[heightfurn_p]', widthfurn_product = '$_POST[widthfurn_p]', lengthfurn_product = '$_POST[lengthfurn_p]', WHERE id_product = '$_POST[id_p]'";
} else {
    echo "Nothing was posted";
}

if (mysqli_query($conn, $sql)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

关于php + mysql 编辑\更新表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50595154/

相关文章:

php - Laravel 在特征启动期间设置/修改模型属性

php - 连接和转义字符后输出不完整

php - PDOException 与 DROP TABLE IF EXISTS

php - 使用AJAX从MySQL数据库更新PHP页面:用于计算最近添加的数据的算法

mysql - 如何让ELMAH登录MySQL?

php - 来自 GoDaddy 的电子邮件在 Gmail 中显示为 'via'

php - $stmt->close() 与 $stmt->free_result()

java - MySQL连接在线数据库错误

mysql - mysql UPDATE 比 INSERT INTO 快吗?

sql - SHOW CREATE TABLE 中的 'KEY' 是否意味着列已索引?