I am making a very simple address book. The following script 1) shows the data 2) edits existing data 3) inserts data into a database.
I can use the script to edit the existing data in the database but I can not add new data. For some reason it is not working at the (isset($_REQUEST['id']))
Where the $id is data from a simple POST HTML form. If $id = 'true' then it edits an existing record, else it adds a new address.
Any tips would be much appreciated. Ta!
<?php
mysql_connect("...","...","...");
mysql_select_db("...");
/////////////////////////
// Submit Information //
/////////////////////////
if(isset($_POST['dataCall'])) {
// if ID = 'true' then we're editing an existing record, else we're adding a new address
if (isset($_REQUEST['id'])) {
$id= $_REQUEST['id'];
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$select = $_REQUEST['select'];
$message = $_REQUEST['message'];
$sql = "UPDATE hgw_addressbook SET addressName = '$name', addressEmail = '$email', addressSelect = '$select', addressMessage = '$message' WHERE addressID = '$id'";
} else {
//create URL extension
$url = md5(uniqid(rand(), true)); // creates unique 32 digit identifier
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$select = $_REQUEST['select'];
$message = $_REQUEST['message'];
$id= $_REQUEST['id'];
$sql = "INSERT INTO hgw_addressbook (addressName, addressEmail, addressSelect, addressMessage, addressURL) VALUES ('$name', '$email', '$select', '$message', '$url')";
}
// run SQL against the DB
$result = mysql_query($sql);
echo "<p>Address updated/edited!<//p>";
echo "<p><a href=\"ecard_admin.php\">Back</a></p>";
/////////////////////////
// Delete Information //
/////////////////////////
} elseif (isset($_GET['delete'])) {
// delete a record
$sql = "DELETE FROM hgw_addressbook WHERE addressID=$id";
$result = mysql_query($sql);
echo "$sql Record deleted!<p>";
echo "<p><a href=\"ecard_admin.php\">Back</a></p>";
/////////////////////////
// Default Information //
/////////////////////////
} else {
// this part happens if we don't press submit
if (!isset($_GET['id'])) {
// print the list if there is not editing
//open table
echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
//Headings row
echo "<tr><td>Name</td><td>Address</td><td>Message</td><td>URL Extension</td><td>Selected</td><td>Edit</td><td>Delete</td></tr>";
$result = mysql_query("SELECT * FROM hgw_addressbook ORDER by addressName");
while ($myrow = mysql_fetch_array($result)) {
echo "<tr>";//New row
printf("<td><b>%s</b></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>", $myrow["addressName"], $myrow["addressEmail"], $myrow["addressMessage"], $myrow["addressURL"], $myrow["addressSelect"]);
printf("<td><a href=\"%s?id=%s\">(Edit)</a></td>", $PHP_SELF, $myrow["addressID"]);
printf("<td><a href=\"%s?id=%s&delete=yes\">(Delete)</a></td>", $PHP_SELF, $myrow["addressID"]);
echo "</tr>";//close row
}
//close table
echo "</table>";
}
"<p><a href=\"coin5_upload.php\">Add Address</a></p>"
?>
<form method="POST" action="<?php echo $PHP_SELF?>">
<?php
/////////////////////////
// Edit Details //
/////////////////////////
if (isset($_GET['id'])) {
echo "<p><a href='ecard_admin.php'>Back to Addressbook</a></p>";
// editing so select a record
$sql = "SELECT * FROM hgw_addressbook WHERE addressID=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$id = $myrow["addressID"];
$name = $myrow["addressName"];
$email = $myrow["addressEmail"];
$select = $myrow["addressSelect"];
$message = $myrow["addressMessage"];
} // Close ID
?>
<input type=hidden name="id" value="<?php echo $id ?>">
<input type=hidden name="dataCall" value="true">
<p> Name: <input name="name" type="Text" value="<?php echo $name ?>"><br>
Email: <input name="email" type="Text" value="<?php echo $email ?>"><br>
Message: <input name="message" type="Text" value="<?php echo $message ?>"></p>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
}// Close Default Information
?>
</body>
</html>