php + mysql编辑\更新表单

时间:2018-05-30 01:29:17

标签: php mysql

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

  <?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文件,无法将我的数据更新到DB

<?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";

?>

2 个答案:

答案 0 :(得分:0)

查看下面的代码,看看它是否有效。让我知道你得到了什么错误。

java -jar youProjectName.jar

和你的update.php

<?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>

答案 1 :(得分:0)

您必须在while循环之外使用<form>标记。 <form>标记仅使用一次(不在循环中)。

您的HTML代码应为:

<?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>

<form action=update.php method=post>

<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)){

 echo "<tr>";
 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 "</tr>";

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

 </table>

 </form>
 </body>
 </html>

.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]'";

     mysqli_query($conn, $sql);

} else {
    echo "Nothing was posted";
}

mysqli_close($conn);

?>