Hi Ben,
I am totally new to php & found this which has helped me greatly to understand it.Thanks.
I have used your MySQL first example in MySQL basic to see how it all works but am having issues with the "delete" & the "edit".
I was hoping you could help me with it?
The "delete" is just not doing anything when it is clicked.
<?php
// connect to the database
include('connect-db.php');
// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['cust_no']) && is_numeric($_GET['cust_no']))
{
// get id value
$cust_no = $_GET['cust_no'];
// delete the entry
$result = mysql_query("DELETE FROM customer WHERE id=$cust_no")
or die(mysql_error());
// redirect back to the view page header("Location: view.php");
}
else
// if id isn't set, or isn't valid, redirect back to view page
{
header("Location: view.php");
}
?>
In the "edit.php" I am getting a:
Parse error: syntax error, unexpected T_ELSE on line 96, which would appear to be the last "else" in the code below.
(Hope I have laid this out right & in the proper order, if not, my appologies.
the table is: customer.
the elements of the table are: cust_no, name,address, phone_no.
<?php
// creates the edit record form
function renderForm($cust_no, $name, $address, $phone_no, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Edit Record</title>
</head>
<body>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="cust_no" value="<?php echo $cust_no; ?>"/>
<div>
<p><strong>Customer No:</strong> <?php echo $cust_no; ?></p>
<strong>Name: *</strong> <input type="text" name="name" value="<?php echo $name; ?>"/><br/>
<strong>Address: *</strong> <input type="text" name="address" value="<?php echo $address; ?>"/><br/>
<strong>Phone No: *</strong> <input type="text" name="phone_no" value="<?php echo $phone_no; ?>"/><br/>
<p>* Required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
include('connect-db.php');
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['cust_no']))
{
// get form data, making sure it is valid
$cust_no = $_POST['cust_no'];
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
$phone_no = mysql_real_escape_string(htmlspecialchars($_POST['phone_no']));
// check that fields are filled in
if ($name == '' || $address == '' || $phone_no == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($cust_no, $name, $address, $phone_no, $error);
}
else
{
// save the data to the database
mysql_query("UPDATE customer SET name='$name', address='$address', phone_no='$phone_no' WHERE cust_no='$cust_no'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view.php");
}
}
else
{
// if the 'id' isn't valid, display an error
echo 'Error!';
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checking that it is numeric/larger than 0) if (isset($_GET['cust_no']) && is_numeric($_GET['cust_no']) && $_GET['cust_no'] > 0)
{
// query db
$cust_no = $_GET['cust_no'];
$result = mysql_query("SELECT * FROM customer WHERE cust_no=$cust_no")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$name = $row['name'];
$address = $row['address'];
$phone_no = $row['phone_no'];
// show form
renderForm($cust_no, $name, $address, $phone_no, '');
}
else
// if no match, display result
{
echo "No results";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error';
}
}
?>
If you can help with these issues, I would be very thankful.