用于编辑产品的PHP脚本

时间:2016-06-11 19:47:35

标签: php

我想要一个脚本来编辑产品,但我无法编写或找到可行的脚本。我已设法创建脚本以从托管站点为PhpMyAdmin的数据库中删除产品。

  <?php      
    function renderForm($id = '', $name = '', $description = '', $genre = '', $price = '', $image = '', $error = '')
     {
     ?>
     <!DOCTYPE HTML >
     <html>
     <head>
     <title>New Product</title>
     </head>
     <body>
     <?php 

     if ($error != '')
     {
     echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
     }
     ?> 

      <form action="" method="post">
     <div>
     <strong>id: </strong> <input type="text" name="id" value="<?php echo $id; ?>" /><br/>
     <strong>name: </strong> <input type="text" name="name" value="<?php echo $name; ?>" /><br/>
      <strong>description: </strong> <input type="text" name="description" value="<?php echo $description; ?>" /><br/>
     <strong>genre: </strong> <input type="text" name="genre" value="<?php echo $genre; ?>" /><br/>
      <strong>price: </strong> <input type="text" name="price" value="<?php echo $price; ?>" /><br/>
      <strong>image: </strong> <input type="text" name="image" value="<?php echo $image; ?>" /><br/>


                 <th>id</th>
                 <th>name</th>
                 <th>description</th>
                 <th>genre</th>
                 <th>price</th>
                 <th>image</th>


     <p>ALL FIELDS ARE REQUIRED!</p>
     <input type="submit" name="submit" value="Submit">
     </div>
      <p><a href='view.php'>Back</a></p>
     </form> 
     </body>
     </html>
     <?php 
     }

     include('connect-db.php');


     if (isset($_POST['submit']))
     { 
     // get form data, making sure it is valid
    $id = mysql_real_escape_string(htmlspecialchars($_POST['id']));
    $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
    $description = mysql_real_escape_string(htmlspecialchars($_POST['description']));
    $genre = mysql_real_escape_string(htmlspecialchars($_POST['genre']));
    $price = mysql_real_escape_string(htmlspecialchars($_POST['price']));
    $image = mysql_real_escape_string(htmlspecialchars($_POST['image']));


     // check to make sure the fields are entered
     if ($id == '' || $name == '' ||  $description == '' || $genre == '' || $price == '' || $image == '')
     {
     // generate error message
     $error = 'ERROR: Please fill in all required fields!';

     renderForm($id = '', $name = '', $description = '', $genre = '', $price = '', $image = '', $error = '');
     }
     else
     {
     // save the data to the database

     mysql_query("INSERT product SET id='$id', name='$name', description='$description', genre='$genre', price='$price', image='$image'")
     or die(mysql_error()); 

     // once saved, redirect back to the view page
     header("Location: view.php"); 
     }
     }
     else
     {
     renderForm('','','');
     }
    ?>

编辑脚本的目的是节省必须删除产品并在网站上再次添加产品的时间。

1 个答案:

答案 0 :(得分:0)

更改此内容:

mysql_query("INSERT product SET id='$id', name='$name', description='$description', genre='$genre', price='$price', image='$image'")
     or die(mysql_error()); 

对于类似的事情:

mysql_query("UPDATE product SET id='$id', name='$name', description='$description', genre='$genre', price='$price', image='$image' WHERE id='$id'")
     or die(mysql_error());