add, update and delete in one page
Moderator: General Moderators
add, update and delete in one page
Hello I have a problem, Im creating a page that adds, edit and delete but its giving me lots of errors....
Please help Im a newBie in php
-
litebearer
- Forum Contributor
- Posts: 194
- Joined: Sat Mar 27, 2004 5:54 am
Re: add, update and delete in one page
It is helpful if you display the code and the errors you are receiving
Re: add, update and delete in one page
Code: Select all
<html>
<body>
<?php
//This is my table I have added the code and the errors below
/* CREATE TABLE `contacts` (
`id` int(10) NOT NULL auto_increment,
`first_name` varchar(30) NOT NULL default '',
`last_name` varchar(50) NOT NULL default '',
`email` varchar(75) default NULL,
`contact_status` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM */
$db = mysql_connect("localhost","root","");
mysql_select_db("kids",$db);
if (isset($_POST['submit'])) {
// here if no ID then adding else editing
if ($id) {
$sql = "update contacts set first_name = '$fname', last_name = '$lname', email = '$email', contact_status = '$status' where id = $id";
} else {
$sql = "insert into contacts (first_name, last_name, email, contact_status) values('$fname','$lname','$email',$status)";
}
// run SQL against the DB
$result = mysql_query($sql);
echo "Record updated/edited!<p>";
} elseif ($delete) {
// delete a record
$sql = "DELETE FROM contacts WHERE id = '$id' ";
$result = mysql_query($sql);
echo "$sql Record deleted!<p>";
} else {
// this part happens if I don't press submit
if (!$id) {
// print the list if there is not editing
$result = mysql_query("SELECT * FROM contacts",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?id=%s\">%s %s</a> \n", $PHP_SELF, $myrow["id"], $myrow["id"], $myrow["first_name"]);
printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF, $myrow["id"]);
}
}
?>
<P>
<a href="<?php echo $PHP_SELF?>">ADD A DOWNLOAD</a>
<P>
<form method="post" action="<?php echo $PHP_SELF?>">
<?php
if ($id) {
// editing so select a record
$sql = "SELECT * FROM contacts WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$id = $myrow["id"];
$first_name = $myrow["first_name"];
$last_name = $myrow["last_name"];
$email = $myrow["email"];
$status = $myrow["status"];
// print the id for editing
?>
<input type=hidden name="id" value="<?php echo $id ?>">
<?php
}
?>
FirstName:
<textarea name="first_name" cols="50" rows="1"><?php echo $first_name ?></textarea>
<br>
LastName:
<textarea name="last_name" cols="50" rows="5"><?php echo $last_name ?></textarea>
<br>
Email:
<textarea name="email" cols="50" rows="1"><?php echo $email ?></textarea>
<br>
Status:
<textarea name="status" cols="50" rows="1"><?php echo $status ?></textarea>
<br>
<input type="Submit" name="submit" value="Enter">
</form>
<?php
}
?>
</body>
</html>
Notice: Undefined variable: delete in C:\Program Files\EasyPHP-5.3.3\www\project4\monkey.php on line 16
Notice: Undefined variable: id in C:\Program Files\EasyPHP-5.3.3\www\project4\monkey.php on line 23
Notice: Undefined variable: PHP_SELF in C:\Program Files\EasyPHP-5.3.3\www\project4\monkey.php on line 27
Undefined variable: PHP_SELF in C:\Program Files\EasyPHP-5.3.3\www\project4\monkey.php on line 28
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: add, update and delete in one page
The errors seem pretty clear. You have not defined $delete or $id. I assume you need to get them from $_GET or $_POST. And it is $_SERVER['PHP_SELF'].
(#10850)