2012-04-04 91 views
0

我正在開發一個應用程序,它根據從另一個表中選擇的值更新表中的數據。我搜查了網絡,並試圖修改一些樣本以適應我的需要,但仍然無法正常工作。使用Php和MySQL更新表數據時循環使用

下面是我的一些代碼:是越來越填充

$sync_2 = " 
    select pin,ddt_1 
    from purchases 
    where (amount_to_repay-amount_repaid)>0 
    and status like 'U%' 
    and counter=1 
"; 
$sync_2_res = mysqli_query($link, $sync_2); 
$row = mysqli_fetch_assoc($sync_2_res); 
$data = mysqli_num_rows($sync_2_res); 

$i; 
for ($i = 0; $i <= $data; $i++) { 
    $pin = $row['pin']; 
    $ddt = $row['ddt_1']; 
    $sql = " 
    update deductions 
    set amount = '$ddt_1' 
    where pin = '$pin'; 
    $result = mysqli_query($link,$sql); 
} 

沒有。我必須改變循環的類型嗎?

+0

你錯過了'「''上$行[」滴滴涕']',這將導致腳本給出一個錯誤。 – Nick 2012-04-04 12:43:56

+1

上面有幾個語法錯誤,當你縮進合理編碼時,這會更加明顯。 – DaveRandom 2012-04-04 12:48:55

+0

我改變了它。仍然沒有發生。 – greatak 2012-04-04 12:48:55

回答

2

試試這個代碼,它應該做在單個查詢整個操作:

$query = " 
    UPDATE `deductions` `d` 
    JOIN `purchases` `p` ON `p`.`pin` = `d`.`pin` 
    SET `d`.`amount` = `p`.`ddt_1` 
    WHERE 
    (`p`.`amount_to_repay` - `p`.`amount_repaid`) > 0 
    AND `p`.`status` LIKE 'U%' 
    AND `p`.`counter` = 1 
"; 
$result = mysqli_query($link, $query); 
+0

+1不錯的完整SQL選項 – Baba 2012-04-04 13:01:10

0

AS你給可能有問題,你的變量變化$ ddt_1至$ DDT在SQL語句

for($i=0;$i<=$data;$i++) 

{ 
$pin=$row['pin']; 
$ddt=$row['ddt_1]; 
$sql="update 
    deductions 
set amount='$ddt' 
where pin='$pin'; 
$result=mysqli_query($link,$sql); 
    } 
0

改進PHP例如

$mysqli = new mysqli ("localhost", "root", "", "test"); 
$sql = "SELECT pin , ddt_1 from purchases WHERE amount_to_repay-amount_repaid > 0 AND status like 'U%' AND counter = 1"; 
$sql2 = "update deductions set amount = '%s' where pin = '%s'; "; 
$result = $mysqli->query($sql); 
$row = null ; 
while ($row = $result->fetch_assoc()) 
{ 
    $mysqli->query(sprintf ($sql, $row ['pin'], $row ['ddt_1'])); 
} 

舊帖子

更換

$ddt=$row['ddt_1]; 

隨着

$ddt=$row['ddt_1']; 

更換

set amount='$ddt_1' 

隨着

set amount='$ddt' 

我希望這有助於

感謝

:)

+0

感謝您的回覆...更改了它...但不工作 – greatak 2012-04-04 12:55:23

+0

是因爲您沒有關閉大部分字符串..我只是爲您添加了改進的PHP選項 – Baba 2012-04-04 12:59:59

0

首先要注意的事情是,mysqli_fetch_assoc()只取一行,該行的地方遊標是。然後它將光標移動一行。當光標到達數據末尾mysqli_fetch_assoc()返回NULL

因此,要通過一組結果的迭代人們通常使用while循環,因爲這是最巧妙:

$sync_2_res = mysqli_query($link, $sync_2); 

if($sync_2_res == null) { 
    echo "The sync2 query failed: " . mysqli_error($link); 
    exit(); 
} 

while($row = mysqli_fetch_assoc($result) { 
    // do something with $row 
} 

而且它是一個好主意,檢查該查詢工作,返回的結果,而不是FALSE 。

接下來,我認爲在第二個查詢中存在拼寫錯誤:$dtt_1應該是$dtt,您確定amount字段應該設置爲字符串嗎?

反正這裏是我會怎麼重寫代碼:

$sync_2= "select pin,ddt_1 from purchases " 
    . "where (amount_to_repay-amount_repaid)>0 " 
    . "and status like 'U%' and counter=1"; 

$sync_2_res=mysqli_query($link,$sync_2); 

// check the query worked 
if(!$sync_2_res) { 
    echo "The sync2 query failed: " . mysqli_error($link); 
    exit(); 
} 

while($row = mysqli_fetch_assoc($result) { 
    // do something with $row 
    $pin=$row['pin']; 
    $ddt=$row['ddt_1']; 
    // note change from $ddt_1 to $dtt 
    $sql="update deductions set amount='$ddt' where pin='$pin';"; 

    $result=mysqli_query($link,$sql); 
    // check query worked 
    if(!$result) { 
     echo "The 2nd query failed: " . mysqli_error($link); 
    } 
}