2013-03-27 50 views
0

我有這個php文件,它將更新mysql數據庫中的表。更新MySQL數據庫和id和圖像相關的ID?

它工作到一定程度。它會更新產品的名稱(product_name),它也會更新(date_added)。但它並沒有更新ID,因此使用該ID上傳的圖像沒有顯示!

這裏是代碼:

<?php 
// Parse the form data and add inventory item to the system 
if (isset($_POST['product_name'])) { 

    $product_name = mysql_real_escape_string($_POST['product_name']); 
    // See if that product name is an identical match to another product in the system 
    $sql = mysql_query("SELECT id FROM tomProduct WHERE product_name='$product_name' LIMIT 1"); 
    $productMatch = mysql_num_rows($sql); // count the output amount 
    if ($productMatch > 0) { 
     echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="index.php">click here</a>'; 
     exit(); 
    } 
    // Add this product into the database now 
    $sql = mysql_query("UPDATE tomProduct SET product_name='$product_name', date_added=now()") or die (mysql_error()); 
    $pid = mysql_insert_id(); 
    // Place image in the folder 
    $newname = "$pid.jpg"; 
    move_uploaded_file($_FILES['fileField']['tmp_name'], "../tom/$newname"); 
    header("location: verify_members.php"); 
    exit(); 
} 
?> 

,這裏是形式:

<form action="verify_members.php" enctype="multipart/form-data" name="myForm" id="myform" method="post"> 
<table width="90%" border="0" cellspacing="0" cellpadding="6"> 
    <tr> 
    <td width="20%" align="right">Product Name</td> 
    <td width="80%"><label> 
     <input name="product_name" type="text" id="product_name" size="64" /> 
    </label></td> 
    </tr> 
    <tr> 
    <td align="right">Product Image</td> 
    <td><label> 
     <input type="file" name="fileField" id="fileField" /> 
    </label></td> 
    </tr> 
     <tr>  
    <tr> 
    <td>&nbsp;</td> 
    <td><label> 
     <input type="submit" name="button" id="button" value="Add This Item Now" /> 
    </label></td> 
    </tr> 
</table> 
</form> 

人知道爲什麼會這樣?

感謝

+0

爲什麼你使用'UPDATE'來'INSERT'記錄到數據庫中?該更新查詢將分別將所有product_name和date_added字段設置爲「$ product_name」和「now()」。 – Crontab 2013-03-27 13:30:50

+0

我沒有在任何地方看到實際的INSERT。 – isotrope 2013-03-27 13:31:26

+5

**不要在新代碼中使用'mysql_ *'函數**([why?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php )),它們是 [已棄用](https://wiki.php.net/rfc/mysql_deprecation)。使用[PDO或MySQLi](http://php.net/manual/en/mysqlinfo.api.choosing.php)代替 – kero 2013-03-27 13:32:23

回答

2

mysql_insert_id()返回最後INSERT記錄,不UPDATE

+0

那我該用什麼來代替? – 2013-03-27 13:42:21

+0

沒關係我解決了這個問題。謝謝 – 2013-03-27 13:46:17