2013-03-21 44 views
-1

我有我的更新功能在PHP上的問題,我有代碼,使功能工作,它永遠只更新最後的功能。多個更新函數,PHP的SQL,只更新最後的功能

<html> 
<head> 
<title>Update a Record in MySQL Database</title> 
</head> 
<body> 

<?php 
if(isset($_POST['update'])) 
{ 
$dbhost = ''; 
$dbuser = ''; 
$dbpass = ''; 
$conn = mysql_connect($dbhost, $dbuser, $dbpass); 
if(! $conn) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

$stock_1 = $_POST['stock_1']; 
$stock_2 = $_POST['stock_2']; 
$stock_3 = $_POST['stock_3']; 
$stock_4 = $_POST['stock_4']; 
$stock_5 = $_POST['stock_5']; 
$stock_6 = $_POST['stock_6']; 

$sql = "UPDATE products ". 
     "SET instock = $stock_1 ". 
     "WHERE productid = 1" ; 

$sql = "UPDATE products ". 
     "SET instock = $stock_2 ". 
     "WHERE productid = 2" ; 
$sql = "UPDATE products ". 
     "SET instock = $stock_3 ". 
     "WHERE productid = 3" ; 

$sql = "UPDATE products ". 
     "SET instock = $stock_4 ". 
     "WHERE productid = 4" ; 

$sql = "UPDATE products ". 
     "SET instock = $stock_5 ". 
     "WHERE productid = 5" ; 

$sql = "UPDATE products ". 
     "SET instock = $stock_6 ". 
     "WHERE productid = 6" ; 


mysql_select_db('db_k0903037'); 
$retval = mysql_query($sql, $conn); 
if(! $retval) 
{ 
    die('Could not update data: ' . mysql_error()); 
} 
echo "Updated data successfully\n"; 
mysql_close($conn); 
} 
else 
{ 
?> 
<form method="post" action="<?php $_PHP_SELF ?>"> 
<table width="400" border="0" cellspacing="1" cellpadding="2"> 
<tr> 
<td width="100">3.5" Seagate SATA 2TB</td> 
<td><input name="stock_1" type="text" id="stock_1"></td> 
</tr> 
<tr> 
<td width="100">Samsung 2.5" SATA Hard Drive</td> 
<td><input name="stock_2" type="text" id="stock_2"></td> 
</tr> 
<tr> 
<td width="100">8gb Kingston DDR3 RAM 1333mhz</td> 
<td><input name="stock_3" type="text" id="stock_3"></td> 
</tr> 
<tr> 
<td width="100">Apple MacBook Ram 8GB</td> 
<td><input name="stock_4" type="text" id="stock_4"></td> 
</tr> 
<tr> 
<td width="100">Gigabyte GA-970A-DS3</td> 
<td><input name="stock_5" type="text" id="stock_5"></td> 
</tr> 
<tr> 
<td width="100">Asus P8Z77-V PRO </td> 
<td><input name="stock_6" type="text" id="stock_6"></td> 
</tr> 
<tr> 
<td width="100"> </td> 
<td> </td> 
</tr> 
<tr> 
<td width="100"> </td> 
<td> 
<input name="update" type="submit" id="update" value="Update"> 
</td> 
</tr> 
</table> 
</form> 
<?php 
} 
?> 
</body> 
</html> 

多數民衆贊成我的代碼和即時通訊努力想知道爲什麼它不會更新所有這些?顯然我輸入了正確的用戶名和密碼!

任何幫助將不勝感激。

+2

你把一些水在玻璃,然後你用牛奶在玻璃代替水,然後你又用酒代替牛奶,然後喝下去,你最終會喝酒? – 2013-03-21 10:21:17

回答

0

你爲什麼不在循環中這樣做?

for($i=1;$i<=6;++$i){ 
    ${'stock_'.$i} = $_POST['stock_'.$i]; 
    $sql = "UPDATE products SET instock = ".${'stock_'.$i}." WHERE productid = ".$i ; 
    $retval = mysql_query($sql, $conn); 
    // ... 
} 

或只是

for($i=1;$i<=6;++$i){ 
    $var = $_POST['stock_'.$i]; 
    $sql = "UPDATE products SET instock = ".$var." WHERE productid = ".$i ; 
    $retval = mysql_query($sql, $conn); 
    // ... 
} 
+0

這個很好用!想到一個循環,但不知道如何,謝謝! – jstables 2013-03-21 10:41:51

1

mysql_select_db('db_k0903037');必須在代碼的頂部,然後讓每個$sql = "...";後做

$retval = mysql_query($sql, $conn); 
if(! $retval) 
{ 
    die('Could not update data: ' . mysql_error()); 
} 

你這樣做的方式,你是否設置$sql字符串爲每次不同的東西,但你並沒有真正執行該查詢。 mysql_query確實執行:)