使用PHP更新MySQL不起作用

时间:2014-01-17 07:09:47

标签: php mysql

if(isset($_POST['name']))
{ 
$productID = $_POST['pid'];
$cat_id = $_POST['cat_id']; 
$name = $_POST['name'];  
$picture = $_POST['picture'];  
$description = $_POST['description'];  
$me_level = $_POST['me_level'];  
$pe_level = $_POST['pe_level']; 
$runs = $_POST['runs'];
$copy = $_POST['copy'];  
$stock_level = $_POST['stock_level'];  
$price = $_POST['price'];

//INSERT   
$query2 = "UPDATE products SET cat_id =' $cat_id', name =' $name', picture =' $picture', description =' $description', me_level =' $me_level',  pe_level =' $pe_level', run =' $runs', copy =' $copy', stock_level =' $stock_level', price =' $price' WHERE serial=$productID"; 
$result = mysql_query($query2); 

if( $result ){
echo '<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">x</button>
<strong>Success!</strong> Product has been updated.
</div>';
}
else{
    echo "<div class='alert alert-error'>
<button type='button' class='close' data-dismiss='alert'>x</button>
<strong>Error!</strong> failed again ".$productID."
</div>";
}
};

我已经插入了一张表 - 工作正常 - 但我无法正常工作。

3 个答案:

答案 0 :(得分:1)

此处WHERE serial=$productID。它应该是WHERE serial='$productID'

答案 1 :(得分:0)

将您的查询更改为:

$query2 = "UPDATE products SET cat_id ='". $cat_id ."', name ='". $name ."', picture ='". $picture ."', description ='". $description. "', me_level ='". $me_level ."',  pe_level ='". $pe_level ."', run ='". $runs ."', copy ='". $copy ."', stock_level ='". $stock_level ."', price ='". $price ."' WHERE serial= '". $productID. "'";

答案 2 :(得分:0)

使用此查询

$query2 = "UPDATE products SET cat_id ='$cat_id', name ='$name', 
picture ='$picture', description ='$description',
me_level ='$me_level',  pe_level ='$pe_level', run ='$runs', 
copy ='$copy', stock_level ='$stock_level', price ='$price' 
WHERE `serial`=$productID"; 

字段名serial引用(`)。这是必需的,因为“serial”是MySQL中的关键字。