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('','','','','','','');
 }
?>

Dani AI

Generated

There are several distinct problems in the posted script that together cause the "Please fill in all required fields!" message. already caught the missing quote in the SQL and spotted the duplicated/wrong assignment. In addition to those, the form input name for the first name does not match the PHP variable the script reads, and the INSERT statement is missing the INTO keyword. Fixing those four issues will make the validation behave correctly.

Key fixes to apply (in order)

  • Make form names and PHP variables consistent: either rename the first-name input to match the PHP variable you use, or change the PHP to read the form field you actually have. Do the same for every field so nothing is accidentally left empty.
  • Correct the variable assignments so each posted field is assigned to its own variable (avoid assigning the same variable twice).
  • Fix the SQL syntax: include INTO after INSERT and make sure all quotes are balanced around values.
  • Use a robust check for empty values (trim strings before testing) and consider checking the request method rather than relying solely on a named submit field.

Development and security recommendations

  • Enable full error reporting while debugging (E_ALL and display_errors). Print the contents of the POST array to verify exactly which names arrived from the form.
  • Replace deprecated mysql_* usage with prepared statements (PDO or mysqli) to avoid SQL injection and quoting problems — see PDO prepared statements: https://www.php.net/manual/en/pdo.prepared-statements.php.
  • Validate the email with a proper filter (see filter_var for email validation) and always trim inputs before validation.

Quick debug checklist: print_r the POST data, echo individual variables before validation, fix field-name mismatches, correct the INSERT syntax, and then re-run.

Recommended Answers

All 4 Replies

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 Member #671080

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.