簡體   English   中英

在while循環PHP中更新MySql中的所有行

[英]Update all rows in MySql in while loop PHP

我有個問題。 我想用while循環更新MySql中的值。 到目前為止我的代碼:這里我從數據庫中獲取行:

$sql = mysql_query("SELECT * FROM Price WHERE idCar='$idCar'");
while($row = mysql_fetch_array($sql)){
$i = $i+1;
$price = $row["price"];
$newdate= $row["newdate"];
$idCar = $row["idCar"];
$idPrice = $row["idPrice"];
$sqlsearch .= '  <tr>
    <td height="31" align="left" valign="middle"><div align="right">Dato:</div></td>
    <td height="31" align="left" valign="middle">
    <fieldset id="example_1"><input type="text" name="newdate" class="field" cols="42" rows="8" id="datepicker'. $i .'" value='. $newdate.' /> 
    &emsp;&emsp;Pris:&nbsp;<input type="text" name="price" class="field" cols="42" rows="8" id="price" value='. $price .' />&emsp;&emsp;&emsp;&emsp;
    Priceid:&nbsp;<input type="text" name="idPrice" class="field" cols="42" rows="8" id="idPrice" value='. $idPrice .' />
    </fieldset></td>
    </tr>';

在這里,我想更新新值:

$idCar = $_GET['idCar'];

$sqlupdate = mysql_query("UPDATE Price SET newdate='$newdate', price='$price' WHERE idPrice='".$idPrice."'");

while($row = mysql_query($sqlupdate)){
    $price = $row["price"];
    $newdate = $row["newdate"];
    $idPrice = $row["idPrice"];
    }

調用SQL更新查詢時,您不需要循環它。 您是否嘗試再次檢索更新的值? (你已經在PHP變量中使用它們了)。

你的第一個代碼片段看起來很好(除了在mysql_*被棄用的時候這里的破紀錄,使用mysqli_*等...),但你的第二個:

$sqlupdate = mysql_query("UPDATE Price SET newdate='$newdate', price='$price' WHERE idPrice='".$idPrice."'");

此行將使用idPrice = {contents of idPrice}更新所有記錄(如果您只希望更新一條記錄,那么將LIMIT 1置於不會對您造成傷害)。 你不需要循環它,我看不到你在做什么。

然后,您應該能夠確定查詢是否成功:

if($sqlupdate)
    echo 'Success';
else
    echo 'Failed: ' . mysql_error();

我假設這個代碼將從示例一進入當前循環?

--edit--示例:

// get info from db
$sql = mysql_query("SELECT * FROM Price WHERE idCar='$idCar'");
while($row = mysql_fetch_array($sql)){
    $i = $i+1;
    $price = $row["price"];
    $newdate= $row["newdate"];
    $idCar = $row["idCar"];
    $idPrice = $row["idPrice"];

    // display info
    $sqlsearch .= '  <tr>
    <td height="31" align="left" valign="middle"><div align="right">Dato:</div></td>
    <td height="31" align="left" valign="middle">
    <fieldset id="example_1"><input type="text" name="newdate" class="field" cols="42" rows="8" id="datepicker'. $i .'" value='. $newdate.' /> 
    &emsp;&emsp;Pris:&nbsp;<input type="text" name="price" class="field" cols="42" rows="8" id="price" value='. $price .' />&emsp;&emsp;&emsp;&emsp;
    Priceid:&nbsp;<input type="text" name="idPrice" class="field" cols="42" rows="8" id="idPrice" value='. $idPrice .' />
    </fieldset></td>
    </tr>';

    $newdate = 'Your value to update';
    $price = 'Your price to update';
    // update data
    $sqlupdate = mysql_query("UPDATE Price SET newdate='$newdate', price='$price' WHERE idPrice='".$idPrice."'");
    // exit with error message if there was an error
    if(!$sqlupdate) die('Error:' . mysql_error());
}

mysql_query()返回resultValue,而不是數據。

然后你會抓住以下行:

  $myRv = mysql_query("SQL GOES HERE");

  while($row=mysql_fetch_assoc($myRv)) {
    // do somehting with $row
  }

但是更新/插入不會返回帶有mysql_fetch_assoc或其他函數的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM