简体   繁体   English

PHP + MySQL编辑\\更新表格

[英]Php + mysql editing\update form

I have tried to edit my data in DB, but it crashed and said, that my variables are undefined. 我试图在数据库中编辑数据,但是崩溃了,并说我的变量未定义。 After putting command isset($_POST) my data not editing in DB with message "Not updated". 放置命令isset($ _ POST)后,我的数据未在数据库中通过消息“未更新”进行编辑。 Here is my edit file: 这是我的编辑文件:

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

And here is update.php file that can't update my data into DB 这是无法将我的数据更新到数据库的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";

?>

Tr the code below and see if it works. Tr下面的代码,看看它是否有效。 Let me know what error you get. 让我知道您遇到什么错误。

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

and your update.php 和您的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);
?>

You must use your <form> tag outside the while loop. 您必须在while循环之外使用<form>标记。 <form> tags are used only one time (not in loop). <form>标记仅使用一次(不循环)。

Your HTML code should be: 您的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>

And the .php file should be: .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);

?>

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

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