Hello:
I'm new to PHP. I was experimenting with the PHP and MySQL tutorial I found while searching the net to see how PHP and MySQL interact with one another.
The add entry script is working. I'm having problems with the listing of records, editing of records, and deleting of records. I'm not sure what I'm doing wrong. The listing script is calling the add entry script. So I add the record, the database is updated, and nothing else happens. I don't get a list of records from that table.
When I did SQL programming off the mainframe, you did a simple select statement. I'm not sure why the PHP list script is calling the add entry script.
This is my Listing script:
<h2>List Records</h2>
<a href='addentry2.php'>Add Record</a><br>
<?php
include("connect.php");
$sql = "SELECT * FROM customer WHERE ID='$id'";
$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
while($result = mysql_fetch_array($query)) {
$FirstName = stripslashes($result["FirstName"]);
$LastName = stripslashes($result["LastName"]);
$ID = $result["ID"];
echo "$FirstName $LastName [<a href='editentry.php?id=$ID'>Edit</a> | <a href='delete.php?id=$ID'>Delete</a>]<br>";
}
?>
This is my edit script:
<?php
include("connect.php");
if(!empty($fname)) {
$fname = addslashes($fname);
$lname = addslashes($lname);
$email1 = addslashes($email1);
$email2 = addslashes($email2);
$address = addslashes($address);
$city = addslashes($city);
$state = addslashes($state);
$zipcode = addslashes($zipcode);
$homephone = addslashes($homephone);
$workphone = addslashes($workphone);
$company = addslashes($company);
$sql = "UPDATE customer SET FirstName='$fname', LastName='$lname', Address='$address', City='$city', Company='$company',
State='$state', zipcode='$zipcode', HomePhone='$homephone', WorkPhone='$workphone', PrimaryEmail='$email1',
SecondEmail='$email2' WHERE ID='$id'";
$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
echo "Database Updated.";
} else {
$sql = "SELECT * FROM customer WHERE ID='$id'";
$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
$result = mysql_fetch_array($query);
$FirstName = stripslashes($result["FirstName"]);
$LastName = stripslashes($result["LastName"]);
$PrimaryEmail = stripslashes($result["PrimaryEmail"]);
$Address = stripslashes($result["Address"]);
$City = stripslashes($result["City"]);
$State = stripslashes($result["State"]);
$zipcode = stripslashes($result["zipcode"]);
$SecondEmail = stripslashes($result["SecondEmail"]);
$HomePhone = stripslashes($result["HomePhone"]);
$WorkPhone = stripslashes($result["WorkPhone"]);
$Company = stripslashes($result["Company"]);
?> <b>Edit Address</b>
<form name="customer" method="post" action="<?php echo $PHP_SELF; ?>">
First Name: <input type="text" name="fname" value="<?php echo $FirstName; ?>">
<br>
Last Name:
<input type="text" name="lname" value="<?php echo $LastName; ?>">
<br>
Company:
<input type="text" name="company" value="<?php echo $Company; ?>">
<br>
Address:
<input type="text" name="address" value="<?php echo $Address; ?>">
<br>
City:
<input type="text" name="city" value="<?php echo $City; ?>">
<br>
State:
<input type="text" name="state" value="<?php echo $State; ?>">
<br>
Zip Code:
<input type="text" name="zipcode" value="<?php echo $zipcode; ?>">
<br>
Home Phone:
<input type="text" name="homephone" value="<?php echo $HomePhone; ?>">
<br>
Work Phone:
<input type="text" name="workphone" value="<?php echo $WorkPhone; ?>">
<br>
Primary Email:
<input type="text" name="email1" value="<?php echo $PrimaryEmail; ?>">
<br>
Secondary Email:
<input type="text" name="email2" value="<?php echo $SecondEmail; ?>">
<br>
<input type="submit" name="Submit" value="Submit">
<input type="hidden" name="id" value="<?php echo $id; ?>">
</form>
<?php
}
?>
Here is my delete script:
<?php
include("connect.php");
$sql = "DELETE FROM customer WHERE ID='$id'";
$query = mysql_query($sql) or die("Cannot delete record.<br>" . mysql_error());
echo "Record Deleted!";
?>
Finally, here is the add entry script in case someone needs it for reference:
<?php
include("connect.php");
if(!empty($fname)) {
$fname = addslashes($fname);
$lname = addslashes($lname);
$email1 = addslashes($email1);
$email2 = addslashes($email2);
$address = addslashes($address);
$city = addslashes($city);
$state = addslashes($state);
$zipcode = addslashes($zipcode);
$homephone = addslashes($homephone);
$workphone = addslashes($workphone);
$company = addslashes($company);
$sql = "INSERT INTO customer SET FirstName='$fname', LastName='$lname', Address='$address', City='$city', Company='$company',
State='$state', zipcode='$zipcode', HomePhone='$homephone', WorkPhone='$workphone', PrimaryEmail='$email1',
SecondEmail='$email2'";
$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
echo "Database Updated.";
} else {
?>
<b>Add Address</b>
<form name="customer" method="post" action="<?php echo $PHP_SELF; ?>">
First Name: <input type="text" name="fname" size=30 maxlength=75>
<br>
Last Name:
<input type="text" name="lname" size=30 maxlength=75>
<br>
Company:<br>
<input type="text" name="company" size=30 maxlength=75>
<br>
Address:<br>
<input type="text" name="address" size=30 maxlength=75>
<br>
City:<br>
<input type="text" name="city" size=30 maxlength=75>
<br>
State:<br>
<input type="text" name="state">
<br>
Zip Code:<br>
<input type="text" name="zipcode">
<br>
Home Phone:<br>
<input type="text" name="homephone">
<br>
Work Phone:<br>
<input type="text" name="workphone">
<br>
Primary Email:
<input type="text" name="email1" size=30 maxlength=150>
<br>
Secondary Email:<br>
<input type="text" name="email2" size=30 maxlength=150>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
}
?>
Can someone help out a new person in trying to understand how the scripts should flow from add to list to edit to delete?
Thank you so much in advance.
I know this is a lot. I thought more is better than less.