This will be similar to how we used AddToDB earlier, only this time, we want to query out all the people in the database and update the one we choose. First, let's output those names to the screen.
echo $row["id"]; "><?php echo $row["name"]; </a></td> <td><?php echo $row["age"]; </td> <td><?php echo $row["gender"]; </td> <td><?php echo $row["phone"]; </td> </tr> <?php } while ($row=mysql_fetch_assoc($results)); } else { ? > <tr> <td colspan="4">No data</td> </tr> <?php } ? > </table><br> I trust that the above code needs no in-depth explanation. It's simply connecting to the database using the MyDatabase class and pulling an associative array while looping through the results. Now let's alter our HTML form from above to be used for updating and not inserting.
<form action="<?php echo $_SERVER['PHP_SELF']; " method="post" name="myForm"> <p>Name: <input name="name" type="text" value="<?php echo $row["name"]; "></p> <p>Age: <input name="age" type="text" value="<?php echo $row["age"]; "></p> <p>Gender:<br> <input name="gender" type="radio" value="MALE"<?php echo ($row["gender"] == 'MALE') ? ' checked' : ''; > Male<br> <input type="radio" name="gender" value="FEMALE"<?php echo ($row["gender"] == 'FEMALE') ? ' checked' : ''; > Female</p> <p>Phone Number: <input name="phone" type="text" value="<?php echo $row["phone"]; "></p> <p><input name="action" type="submit" value="UpdateDB"> <input name="id" type="hidden" value="<?php echo $_GET["id"]; "></p> </form> <?php } $SQL->Disconnect(); ? > </body> </html> The only real difference here is the addition of: <input name="id" type="hidden" This is just a hidden form field to pass the id. You will also notice we are echoing out the data in the form fields like so: Alright, now we have got to make this beast actually update the data in the database table.
blog comments powered by Disqus |
|
|
|
|
|
|
|