【问题标题】:Php Populate Table and UpdatePHP 填充表和更新
【发布时间】:2013-12-01 17:24:28
【问题描述】:

伙计们,我不明白为什么我的代码只会更新表格的最后一行。它将使用来自 phpAdmin 的信息的表格填充整个 HTML 页面。然后我可以在 html 页面上更改此信息。如果只有一条记录,则一切正常,不再是一条,并且仅对最后一行生效。我对这一切都很陌生,所以请原谅代码,这里是......

<html>

<?php


$con = mysql_connect("localhost","root");
if(!$con){
    die("Cant get there Bren: " . mysql_error());
}
mysql_select_db("Web_Data",$con);


if (isset($_POST['update'])){



    $UpdateQuery = "UPDATE Vehicles SET Vehicle_Id='$_POST[Vehicle_Id]',
    Registration='$_POST[Registration]',Make='$_POST[Make]',Model='$_POST[Model]',
    Classification='$_POST[Classification]',Rental_Price='$_POST[Rental_Price]',
    Current_Status='$_POST[Current_Status]',Mileage='$_POST[Mileage]' 
    WHERE Vehicle_Id='$_POST[hidden]'";

    echo "<center> Vechicle Id '$_POST[hidden]' succesfully VEHICLE UPDATED </center>";
    mysql_query($UpdateQuery, $con);

};

$sql = "Select * From Vehicles";
$myData = mysql_query($sql,$con);
echo" <center> <table border = 3>
<tr>
<th>Vehicle_Id</th>
<th>Registration</th>
<th>Make</th>
<th>Model</th>
<th>Classification</th>
<th>Rental_Price</th>
<th>Current_Status</th>
<th>Mileage</th>
</tr></center>";
while($record = mysql_fetch_array($myData)){
    echo "<form action = UpdateWD.php method=post>"; 
    echo "<tr>";
    echo "<td>" . "<input type = text name = Vehicle_Id value=" . $record['Vehicle_Id'] . " </td>";
    echo "<td>" . "<input type = text name = Registration value=" . $record['Registration'] . " </td>";
    echo "<td>" . "<input type = text name = Make value=" . $record['Make'] . " </td>";
    echo "<td>" . "<input type = text name = Model value=" . $record['Model'] . " </td>";
    echo "<td>" . "<input type = text name = Classification value=" . $record['Classification'] . " </td>";
    echo "<td>" . "<input type = text name = Rental_Price value=". $record['Rental_Price'] . " </td>";
    echo "<td>" . "<input type = text name = Current_Status value=" . $record['Current_Status'] . " </td>";
    echo "<td>" . "<input type = text name = Mileage value=" . $record['Mileage'] . " </td>";
    echo "<td>" . "<input type = hidden name = hidden value=" . $record['Vehicle_Id'] . " </td>";
    echo "<td>" . "<input type = submit name = update value= update" . " </td>";
    echo "</from>";


}

echo"</table>";
mysql_close($con);


?>
<br><br><br>
<footer>
        Copyright © 2013 ABU LTD
<a href="about.html">About</a> -
<a href="policy.html">Privacy Policy</a> -
<a href="contact.html">Contact Us</a>
<a href="index.php" class="menu">Logout</a>
</footer> <!--footer--> 

</html>

【问题讨论】:

  • 您需要一个循环来更新您有更新查询的所有记录,但为什么要一次更新所有记录?

标签: php html forms loops


【解决方案1】:

看这里:

使用 while 循环显示所有获取的记录。

<?php
// Make a MySQL Connection
$sql = "Select * From Vehicles";
$myData = mysql_query($sql,$con) or die(mysql_error());

while($row = mysql_fetch_array($myData)){
    echo $row['Vehicle_Id'];
}
?>

而且mysql_query不能使用多个查询。

最简单的方法是单独运行它们。我相信你可以做多查询,但我还没有尝试过。

了解如何使用 foreach 循环运行多个查询。

$updateArray = array(21=>300,23=>200,24=>100);
foreach($updateArray as $id=>$value)
{
    $query = "UPDATE cart SET cart_qty='$value' WHERE cart_id = '$id'";
    mysql_query($query,$link);// $link is specified above
}

【讨论】:

  • 我很抱歉 Surinder,我只是不明白你的回答。问题出在 if (isset($_POST['update'])){ $UpdateQuery = "UPDATE Vehicles SET Vehicle_Id='$_POST[Vehicle_Id]', Registration='$_POST[Registration]',Make='$ _POST[Make]',Model='$_POST[Model]', Classification='$_POST[Classification]',Rental_Price='$_POST[Rental_Price]', Current_Status='$_POST[Current_Status]',Mileage='$ _POST[里程]' WHERE Vehicle_Id='$_POST[hidden]'"; echo "
    车辆 ID '$_POST[hidden]' 已成功更新车辆
    "; mysql_query($UpdateQuery, $con); };
  • 我也是stackoverflow的新手,cmets发布的方式似乎很奇怪。
  • 这是我的错误时间和时间,都是 echo "" 。 “”;回声“”;回声“”;回声“”;将“表单”拼写为“来自”并且没有关闭
【解决方案2】:

这是我的错误

几小时又一小时,就是这样

echo "<td>" . "<input type = 'submit' name = 'update' value= 'update'" . " />";
    echo "</td>";
    echo "</tr>";
    echo "</form>";
had "form" spelt "from" & didnt close the </tr>

【讨论】:

    猜你喜欢
    • 2018-12-12
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多