hi guys, pls can someone pls help look into this code..
i'm trying to create a code that will add data into the database..
after inputing, its giving me the "ERROR: Please fill in all required fields!" error

 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, $street, $city, $contactno, $email, $error)
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <title>New Record</title>
 // 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">
 <strong>First Name: *</strong> <input type="text" name="cust_name" value="<?php echo $first; ?>" /><br/>
 <strong>Last Name: *</strong> <input type="text" name="cust_lname" value="<?php echo $last; ?>" /><br/>
 <strong>Street: *</strong> <input type="text" name="cust_street" value="<?php echo $street; ?>" /><br/>
 <strong>City: *</strong> <input type="text" name="cust_city" value="<?php echo $city; ?>" /><br/>
 <strong>Contact Number: *</strong> <input type="text" name="cust_contactno" value="<?php echo $contactno; ?>" /><br/>
 <strong>Email: *</strong> <input type="text" name="cust_email" value="<?php echo $email; ?>" /><br/>
 <p>* required</p>
 <input type="submit" name="submit" value="Submit">

 // connect to the database
 // 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
 $cust_lname = mysql_real_escape_string(htmlspecialchars($_POST['cust_lname']));
 $cust_lname = mysql_real_escape_string(htmlspecialchars($_POST['cust_fname']));
 $cust_street = mysql_real_escape_string(htmlspecialchars($_POST['cust_street']));
 $cust_city = mysql_real_escape_string(htmlspecialchars($_POST['cust_city']));
 $cust_contactno = mysql_real_escape_string(htmlspecialchars($_POST['cust_contactno']));
 $cust_email = mysql_real_escape_string(htmlspecialchars($_POST['cust_email']));
 // check to make sure both fields are entered
 if ($cust_lname == '' || $cust_fname == '' || $cust_street == '' || $cust_city == '' || $cust_contactno == '' || $cust_email == '')
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';
 // if either field is blank, display the form again
 renderForm($cust_lname, $cust_fname, $cust_street, $cust_city, $cust_contactno, $cust_email,  $error);
 // save the data to the database
 mysql_query("INSERT customer SET cust_lname='$cust_lname', cust_fname='$cust_fname', cust_street='$cust_street', cust_city='$cust_city', cust_contactno='$cust_contactno', cust_email='$cust_email")
 or die(mysql_error()); 
 // once saved, redirect back to the view page
 header("Location: view.php"); 
 // if the form hasn't been submitted, display the form

Edited by abbyadnez: n/a

7 Years
Discussion Span
Last Post by Zagga

it shows that some field is empty in your form. echo the fields which one is empty. try to change nonempty.


Hi abbyadnez,

You have an error on line 55

$cust_[B]l[/B]name = mysql_real_escape_string(htmlspecialchars($_POST['cust_fname']));

Should be

$cust_[B]f[/B]name = mysql_real_escape_string(htmlspecialchars($_POST['cust_fname']));


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.