2016-06-11 34 views
0

我想要一個腳本來編輯產品,但我一直沒有能夠寫或找到一個腳本,將工作。我已經設法創建腳本以從託管站點爲PhpMyAdmin的數據庫中刪除產品。Php腳本來編輯產品

<?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('','',''); 
    } 
    ?> 

編輯腳本的目的是爲了節省必須刪除產品的時間,並在網站上再次添加產品。

+0

<形式行動= 「」 方法= 「POST」>需要添加的動作。例如'

<?php echo htmlspecialchars($ _ SERVER [「PHP_SELF」]); ?>應該強烈考慮使用至少mysqli_,因爲mysql_已被棄用。對帖子的檢查應該在html之上。 –

+0

首先使用Php錯誤報告和chk錯誤,這不是更新語句。 – devpro

+0

@devpro「更新」,你的意思是他們的'INSERT產品SET ...'?這是有效的語法。 http://dev.mysql.com/doc/refman/5.7/en/insert.html - 如果他使用了'WHERE'子句,那麼這將是一個不同的故事。 –

回答

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());