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

<?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, $street, $city, $contactno, $email, $error)
 {
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <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>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">
 </div>
 </form> 
 </body>
 </html>
 <?php 
 }
 
 
 

 // connect to the database
 include('connect-db.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
 $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);
 }
 else
 {
 // 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"); 
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','','','','','');
 }
?>

you forgot singleqoute at cust_email='$cust_email. change it to cust_email='$cust_email'

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

Member Avatar for Zagga

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']));

Zagga

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.