I've been searching around the internet, and of course Stackoverflow for answers on how to execute a PHP command when a link is clicked. Here is some basic code I have that's trying to either 'Update' or 'Delete' the data within the form.
if(isset($_GET['Delete'])){
$sql = "DELETE FROM addresses WHERE id ='$_POST[id]'";
mysql_query($sql,$conn);
header("Location: form.php");
};
if (isset($_GET['update'])){
$sql = "UPDATE addresses SET firstname='$_POST[firstname]', lastname='$_POST[lastname]', age='$_POST[age]' WHERE id='$_POST[id]'";
mysql_query($sql,$conn);
header("Location: form.php");
};
?>
<?php
while ($row = mysql_fetch_array($retreve, MYSQL_ASSOC)) {
echo "<form action=form.php method=post>";
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 = # id='update'> Update</a></td>";
echo "<td><a href = # id='delete'> Delete</a> </td>";
}
I have above the functions I'm trying to call whenever I click the links "Update" and "Delete". What am I suppose to do in order to get the PHP to execute.
NOTE: The database connection is not shown but it is connected.
echo "<td><a href = 'form.php?type=delete&id=99' id='delete'> Delete</a> </td>";
{$row['id']}
for 99 in your particular case then:
if($_GET['type']=='delete'){
$sql = "DELETE FROM addresses WHERE id ='$_GET[id]'";
mysql_query($sql,$conn);
header("Location: form.php");
}elseif ($_GET['type']=='update'){
//
}
if you are using the same form then check for your form method.Your form method is post an your conditions contains $_GET['Delete']
change it to $_POST['Delete']