BTDennis BTDennis - 3 months ago 15
MySQL Question

mysql update form using php issue keeps updating with old information not new updats

I am totally new to PHP and am working on an address book that contains companies and contacts for said companies. When click submit to update the data it just replaces it with the old data that was originally in there here is the companies html form:

<html>
<head><title>Update Records in MYSQL Database</title>
<style>
body {background-color: powderblue;}
form {border: 5px solid midnightblue;
padding: 40px;}
label {color: darkblue;}
form {border: 5px solid midnightblue;
padding: 40px;
</style>
</head>
<body>
<?php
//Connect to the Database
$link = mysqli_connect('localhost', '', '');
//Select the Database
mysqli_select_db($link, 'ADDRESS_BOOK_DB');
//Select Query
$sql = "SELECT * FROM Companies";
//Execute the Query
$records = mysqli_query($link, $sql);
?>
<form>
<table>
<tr>
<th>CompanyName</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>ZipCode</th>
<th>PhoneNumber</th>
</tr>
<?php
while($row = mysqli_fetch_array($records)) {
echo "<tr><form action=update.php method='post'></form>";
echo "<td><input type='text' name='CompanyName' value='" . $row['companyname1'] . "'></td>";
echo "<td><input type='text' name='address' value='" . $row['address'] . "'></td>";
echo "<td><input type='text' name='city' value='" . $row['city'] . "'></td>";
echo "<td><input type='text' name='state' value='" . $row['state'] . "'></td>";
echo "<td><input type='text' name='zipcode' value='" . $row['zipcode'] . "'></td>";
echo "<td><input type='text' name='phone' value='" . $row['phone'] . "'></td>";
echo "<td><input type=hidden name=companies_id value='" . $row['companies_id'] . "'>";
echo "<td><input type='submit'>";
echo "</form></tr>";
}
?>
</table>
</form>
</body>
</html>


and here is the actual updating php sql query that does not work or something any input would help get rid of this headache I have.. LOL

<?php
//Connect to the Database
$link = mysqli_connect('localhost', '', '');
//Select the Database
mysqli_select_db($link, 'ADDRESS_BOOK_DB');

//Update Query
$sql = "UPDATE Companies SET CompanyName= $_POST[companyname1], Address= $_POST[address], City= $_POST[city], State= $_POST[state], ZipCode= $_POST[zipcode], PhoneNumber= $_POST[phone]
WHERE companies_id= $_POST[companies_id]";

//Execute the Query
if(mysqli_query($link, $sql))
header("refresh:1; url=edit.php");
enter code here

else
echo "Not Updated";
?>

Answer

First put here condition with WHERE clause $sql = "SELECT * FROM Companies"; that which record from database do you want to update. It seems like you are fetching all the record from database inside form

Second you have closed your form tag at the begening of the while loop and keep rest of the input fields without form

Change this

while($row = mysqli_fetch_array($records)) {
        echo "<tr><form action=update.php method='post'></form>";
        echo "<td><input type='text' name='CompanyName' value='" . $row['companyname1'] . "'></td>";
        echo "<td><input type='text' name='address' value='" . $row['address'] . "'></td>";
        echo "<td><input type='text' name='city' value='" . $row['city'] . "'></td>";
        echo "<td><input type='text' name='state' value='" . $row['state'] . "'></td>";
        echo "<td><input type='text' name='zipcode' value='" . $row['zipcode'] . "'></td>";
        echo "<td><input type='text' name='phone' value='" . $row['phone'] . "'></td>";
        echo "<td><input type=hidden name=companies_id value='" . $row['companies_id'] . "'>";
        echo "<td><input type='submit'>";
        echo "</form></tr>";
    }

to

?>
<form action=update.php method='post'>
<?php
while($row = mysqli_fetch_array($records)) {

        echo "<td><input type='text' name='CompanyName' value='" . $row['companyname1'] . "'></td>";
        echo "<td><input type='text' name='address' value='" . $row['address'] . "'></td>";
        echo "<td><input type='text' name='city' value='" . $row['city'] . "'></td>";
        echo "<td><input type='text' name='state' value='" . $row['state'] . "'></td>";
        echo "<td><input type='text' name='zipcode' value='" . $row['zipcode'] . "'></td>";
        echo "<td><input type='text' name='phone' value='" . $row['phone'] . "'></td>";
        echo "<td><input type=hidden name=companies_id value='" . $row['companies_id'] . "'>";
        echo "<td><input type='submit'>";

    }

?>
</form>