-2

I have two php files, form and the results page. Form page accepts user info and the result pages inserts the information into the database and displays it.

form page code:

    <html>

   <head>

     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Record Insertion Form</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>
    <?php

        echo "<div style='font-size: large; font-family: sans-serif'><center><h1>
              <p style='color: white; background-color: black'>Resume Generator</p></h1></center>      </div>";

echo "Mandotary fields with <font color='red'>*</font> are required!";
?>

<form id="form1" name="form1" method="post" action="resumeinsert.php">
<fieldset>
<legend>Personal Details</legend>
<input type="hidden" name="id" id="id" />
<br class="clear" /> 
<label for="name"><font color="red">*</font> Name:</label><input type="text" name="name" id="name" />
<br class="clear" /> 
<label for="gender"><font color="red">*</font> Gender:</label>
<input type="radio" name="gender" value="Male" id="gender_0" />Male
<input type="radio" name="gender" value="Female" id="gender_1" />Female
<br class="clear" /> 
<label for="dateofbirth"><font color="red">*</font> Date of birth:</label><input type="text" name="dateofbirth" id="dateofbirth" />
<br class="clear" /> 
<label for="placeofbirth"><font color="red">*</font> Place of birth :</label><input type="text" name="placeofbirth" id="placeofbirth" />
<br class="clear" /> 
<label for="address"><font color="red">*</font> Address:</label><textarea name="address" id="address" cols="45" rows="5"></textarea>
<br class="clear" /> 
</fieldset>
<fieldset>
<legend><b>Educational Details</b></legend>
<label for="schoolname"><font color="red">*</font> School name:</label><input type="text" name="schoolname" id="schoolname" size=50 />
<br class="clear" /> 
<label for="qualification"><font color="red">*</font> Qualification(s):</label><textarea name="qualification" id="qualification" cols="45" rows="5"></textarea>
<br class="clear" /> 
<label for="skills1"><font color="red">*</font> Skills 1:</label><textarea name="skills1" id="skills1" cols="45" rows="5"></textarea>
<br class="clear" /> 
<label for="skills2">Skills 2:</label><textarea name="skills2" id="skills2" cols="45" rows="5"></textarea>
<br class="clear" /> 
</fieldset>
<fieldset><legend><b>Other Details</b></legend>
<label for="awards">Awards:</label><textarea name="awards" id="awards" cols="45" rows="5"></textarea>
<br class="clear" /> 
<label for="Volunteer">Volunteer:</label><textarea name="Volunteer" id="Volunteer" cols="45" rows="5"></textarea>
<br class="clear" /> 
<input type="submit" name="submit" id="submit" value="Submit" />
<br class="clear" /> 
</fieldset>
</form>

</body>
</html>    

resumeinsert.php page code:

<?php
$mysqli = mysqli_connect("localhost", "hecton", "ccna", "joomladb");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
} else {

$id = $_POST['id'];  
$name = $_POST['name'];  
$gender = $_POST['gender'];  
$dateofbirth = $_POST['dateofbirth'];  
$placeofbirth = $_POST['placeofbirth'];  
$address = $_POST['address'];  
$schoolname = $_POST['schoolname'];  
$qualification = $_POST['qualification'];  
$skills1 = $_POST['skills1'];  
$skills2 = $_POST['skills2'];  
$awards = $_POST['awards'];  
$Volunteer = $_POST['Volunteer'];

     $query = " INSERT INTO j71mi_resumeinfo ( id, name, gender, dateofbirth, placeofbirth, address, schoolname, qualification, skills1, skills2, awards, Volunteer )  VALUES ( '$id', '$name', '$gender', '$dateofbirth', '$placeofbirth', '$address', '$schoolname', '$qualification', '$skills1', '$skills2', '$awards', '$Volunteer' ) "; 
 $result = mysqli_query($mysqli, $query); 


    if ($result === TRUE) {
        echo "A record has been successfully inserted into the database!."; 
        echo "<b><h1><center>My Resume</h1></b></center>";

        echo "<div style='font-size: large; font-family: sans-serif'>
              <p style='color: white; background-color: black'>Personal Details</p></div>";
              echo "<br>";


        echo "<b>Name:</b>".$_POST['name']; 
                echo "<br>";
        echo "<b>Gender:</b>".$_POST['gender'];
                echo "<br>";
        echo "<b>Date of Birth:</b>".$_POST['dateofbirth'];
                echo "<br>";
        echo "<b>Place of Birth:</b>".$_POST['placeofbirth'];
                echo "<br>";
        echo "<b>Home Address:</b>".$_POST['address'];
        echo "<div style='font-size: large; font-family: sans-serif'>
              <p style='color: white; background-color: black'>Educational Details</p></div>";
              echo "<br>";
        echo "<b>School Name:</b>".$_POST['schoolname'];
                echo "<br>";
        echo "<b>Qualification(s):</b>".$_POST['qualification'];
                echo "<br>";
        echo "<b>Skill 1:</b>".$_POST['skills1'];
                echo "<br>";
        echo "<b>Skill 2:</b>".$_POST['skills2'];
                echo "<br>";
                echo "<div style='font-size: large; font-family: sans-serif'>
              <p style='color: white; background-color: black'>Other Details</p></div>";
              echo "<br>";
              echo "<b>Award(s):</b>".$_POST['awards'];
                echo "<br>";
        echo "<b>Volunteer Activity:</b>".$_POST['Volunteer'];
        echo "<br>";
        echo "<br>";
        echo "<b><h5><center>End of Resume</h5></b></center>";



    } else {
        printf("Could not insert record: %s\n", mysqli_error($mysqli));
    }

    mysqli_close($mysqli);
}
?>

My problem is that user sometimes submit the form without even filling out all the fields and it displays " Name: ", which contains nothing.

  1. How can I make the users to fill out the missing required fields after they click submit button with a red text next to the missing fields ? So that empty fields are not sent to database.

2.Some fields are set to NULL in the database, which means the field is optional. How can I not display that specific field in display page if user enters nothing in the form?

Gordon
  • 13
  • 1
  • 4
  • 12
  • Basics http://php.net/manual/en/function.empty.php - http://php.net/manual/en/function.isset.php – Funk Forty Niner Oct 26 '14 at 13:34
  • Q1: add required="required" to the required inputs – baao Oct 26 '14 at 13:36
  • put validations using javascript & PHP before saving into DB. You need to apply some checks on fields before displaying. i.e. use `is_null()` or `empty()` – Apul Gupta Oct 26 '14 at 13:41
  • Q2: copy whole that is * to another table. and then drop that particular column.. For help follow http://stackoverflow.com/questions/729197/sql-exclude-a-column-using-select-except-columna-from-tablea – PrakashSharma Oct 26 '14 at 13:41
  • http://en.wikipedia.org/wiki/SQL_injection – baao Oct 26 '14 at 13:42
  • Q1: required="required" with not help with security issues like SQL injection. So do client and server side validation too. – PrakashSharma Oct 26 '14 at 13:44

1 Answers1

2

You have to validate the form. Use simple checks to determine if fields are filled correctly. Check for empty, alphanumeric, and what ever else you need. It would probably be best to check in the front end before you send the data over to the server. Here are some examples to get you started:

JS Form validation http://www.w3schools.com/js/js_form_validation.asp

JS Form validation http://www.tizag.com/javascriptT/javascriptform.php

PHP Form validation http://www.w3schools.com/php/php_form_validation.asp

Check out these examples and implement something similar to this. Even if you end up validating the form in JS don't forget to implement the bare minimum checks in PHP.

Kevin Pimentel
  • 2,056
  • 3
  • 22
  • 50
  • The w3schools site teaches to use ($_SERVER["PHP_SELF"]), however I want to have 2 seperate files of codes. Form and processing files. – Gordon Oct 26 '14 at 13:49
  • That's not a huge deal. You can simply point the file where ever you need, the validation and checks on the data still apply the same. If you want to use php i would say its better to use php self. Again you should use javascript to check and php as a fallback check to make sure you received the data you want to require into the database before inserting. If it fails send errors and return to the form page. – Kevin Pimentel Oct 26 '14 at 14:30