html - Php Populate Table and Update

205

Guys I cant see why it is that my code will only Update the last row on the table. It will populate the entire HTML page with a table with the info from phpAdmin. I can then change this info, on the html page. It all works fine if there is only one record, anymore than one and it only takes effect on the last row. I am new to all this, so excuse the code, here it is......

<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>
564

Answer

Solution:

See here:

Use while loop to display all fetched records.

<?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'];
}
?>

And mysql_query can't use multiple queries.

The easiest thing is to just run them separately. I believe you can do multi query but I haven't tried it.

Just get the idea how you can run multiple queries using foreach loop.

$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
}
94

Answer

Solution:

here is my mistake

hours and hours and all it was

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

People are also looking for solutions to the problem: php - Pattern to match and replace words with upper and also lower case in them

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.