lundi 1 juin 2015

Updating a database entry using hyperlinks

For some reason, the delete method works, but the update method just turns all the textboxes blank, but puts age to 0. The database shows the same. What should I do in order to to corretly change the database when I click the Update link.

PHP:

if($_GET['type']=='delete'){
                    $sql = "DELETE FROM addresses WHERE id ='$_GET[id]'";
                    mysql_query($sql,$conn);
                    header("Location: form.php");
}if ($_GET['type']=='update'){
                //works but it doesn't seem to just update like before.
                $sql = "UPDATE addresses SET firstname='$_GET[firstname]', lastname='$_GET[lastname]', age='$_GET[age]' WHERE id='$_GET[id]'";
                mysql_query($sql,$conn);
                header("Location: form.php");
}

And here is the HTML to do the table and links.

<?php
while($row = mysql_fetch_array($retreve, MYSQL_ASSOC)){

//echo"<form action='form.php' method='post' name='form'>";
echo "<tr>";
echo "<td><input type='text' name='firstname' value='".$row['firstname']."'> </td>";
echo "<td><input type='text' name='lastname' value='".$row['lastname']."'> </td>";
echo "<td><input type='text' name='age' value='".$row['age']."'> </td>";
echo "<td><input type='hidden' name='id' value='".$row['id']."'> </td>";
//links insead of buttons
echo "<td><a href = 'form.php?type=update&id={$row['id']}&age={$_GET['age']}' id='update'> Update</a> </td>";
echo "<td><a href = 'form.php?type=delete&id={$row['id']}' id='delete'> Delete</a> </td>";
echo"</form>";
}
echo "</table>";




Aucun commentaire:

Enregistrer un commentaire