I have some problem with this tutorial. I made a bit change to suit my forum script I building. Heres what my changed files.
EDIT.PHP
<?php
//create_cat.php
include 'connect.php';
include 'header.php';
include 'modules/bbcode.php';
/*
EDIT.PHP
Allows user to edit specific entry in database
*/
// creates the edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($id, $firstname, $error)
{
?>
<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="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<strong>Content*</strong> <input type="text" name="firstname" value="<?php echo $firstname; ?>"/><br/>
<p>* Required</p>
<input type="submit" name="submit" value="Submit">
</div>
<a href="/settings.php">Back</a>
</form>
</body>
</html>
<?php
}
// connect to the database
// 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['id']))
{
// get form data, making sure it is valid
$id = $_POST['post_id'];
$firstname = mysql_real_escape_string(htmlspecialchars($_POST['post_content']));
// check that firstname/lastname fields are both filled in
if ($firstname == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($id, $firstname, $error);
}
else
{
// save the data to the database
mysql_query("UPDATE
posts
SET
post_content='$firstname'
WHERE post_id='$id'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: settings.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 (checing that it is numeric/larger than 0)
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// query db
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM posts WHERE post_id=$id")
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
$firstname = $row['post_content'];
// show form
renderForm($id, $firstname, '');
}
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!';
}
}
include 'footer.php';
?>
and NEW.PHP
<?php
//create_cat.php
include 'connect.php';
include 'header.php';
/*
NEW.PHP
Allows user to create a new entry in the database
*/
// creates the new record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($first, $last, $error)
{
?>
<html>
<head>
<title>New 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">
<div>
<strong>Content: *</strong> <input type="textarea" name="firstname" value="<?php echo $first; ?>" /><br/>
<p>* required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$firstname = mysql_real_escape_string(htmlspecialchars($_POST['post_content']));
// check to make sure both fields are entered
if ($firstname == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($first, $last, $error);
}
else
{
// save the data to the database
mysql_query("INSERT posts SET post_content='$firstname'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: settings.php");
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','');
}
include 'footer.php';
?>
In both Im getting error "ERROR: Please fill in all required fields" if I want to change something or add. The delete option is working.
Basic Php System: View/edit/delete/add Records
in PHP
Posted
I have some problem with this tutorial. I made a bit change to suit my forum script I building. Heres what my changed files.
EDIT.PHP
and NEW.PHP
In both Im getting error "ERROR: Please fill in all required fields" if I want to change something or add. The delete option is working.