Php填充表和更新

时间:2013-12-01 17:24:29

标签: php html forms loops

伙计我不明白为什么我的代码只会更新表格的最后一行。它将使用包含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>

2 个答案:

答案 0 :(得分:0)

见这里:

使用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
}

答案 1 :(得分:0)

这是我的错误

小时和小时,一切都是

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