I am still new in PHP and right now I'm trying to send data to an SQL database. Let's say for form its called cust_form.php. All the data submitted from there I send it to cust_details.php. My problems right now are -
1) How to validate the data before it been sent to database.
2) How to display the error in cust_form.php and the incorrect data will not been sent / saved to database.
Error occured in email field although correct email been given
I already google for this kind of problem but all of it simply send the data to PHP_SELF while mine send it to other file. It's kinda frustrating.
cust_form.php (which basically text) :
<form action = "cust_detail.php" method = "post">
<table border="1">
<tr>
<td colspan = "2">
<font size = "5">
Customer Details :
</font>
<br>
<b>
#All fields are compulsory.
</b>
</tr>
<tr>
<td>
New Customer Email :
<td>
<input name = "email"
size = "30"
type = "text">
</tr>
<tr>
<td>
Mobile Phone :
<td>
<input name = "tel"
type = "text">
</tr>
<tr>
<td>
First Name :
<td>
<input name = "fname"
type = "text">
</tr>
<tr>
<td>
Last Name :
<td>
<input name = "lname"
type = "text">
</tr>
<tr>
<td>
Identification / Passport Number :
<td>
<input name = "ic"
type = "text">
</tr>
<tr>
<td>
Address :
<td>
<textarea name = "address"
cols = "25"
rows = "2">
</textarea>
</tr>
<tr>
<td>
City :
<td>
<input name = "city"
type = "text">
</tr>
<tr>
<td>
State :
<td>
<input name = "state"
type = "text">
</tr>
<tr>
<td>Country:
<td>
<select name = "country">
<option name = "default"
value = "default">
----Please choose your
</option>
</select>
</tr>
<tr>
<td>
Postal Code :
<td>
<input name = "code"
type = "text">
enter code here
</tr>
<tr>
<td colspan = "2">
<b>
<font size = "3">
<u>
INFO :
</u>
</font>
<br>
* Identification/passport number needed for verification during redemption on booth.
<br>
* For phone number, fill including International code (e.g. +601234567890)
</b>
</tr>
<tr>
<td align = "right"
colspan = "2">
<button name = "submit"
type = "submit">
Submit
</button>
</tr>
</table>
</form>
And for cust_details.php...
<?php
include( 'global.php' );
session_start();
$fname = '';
$lname = '';
$email = '';
$address = '';
$code = '';
$state = '';
$country = '';
$tel = '';
$ic = '';
$fname_error = '';
$lname_error = '';
$tel_error = '';
$email_error = '';
if( isset( $_POST[ 'submit' ] ) )
{
//validate first name
if ( empty( $_POST[ 'fname' ] ) )
{
$fname_error = "First name is required";
$_SESSION[ 'errormsg' ] = $fname_error;
}
else
{
if ( !preg_match( "/^[a-zA-Z]*$/",
$fname ) )
{
$fname_error = "Only letters and white space allowed.";
$_SESSION[ 'errormsg' ] = $fname_error;
}
$fname = input_test( $_POST[ 'fname' ] );
}
//validate last name
if ( empty( $_POST[ 'lname' ] ) )
{
$lname_error = "Last name is required";
$_SESSION[ 'errormsg' ] = $lname_error;
}
else
{
if ( !preg_match( "/^[a-zA-Z]*$/",
$lname ) )
{
$lname_error = "Only letters and white space allowed.";
$_SESSION[ 'errormsg' ] = $lname_error;
}
$lname = input_test( $_POST[ 'lname' ] );
}
//validate email
if ( empty( $_POST[ 'email' ] ) )
{
$email_error = "Email is required";
$_SESSION[ 'errormsg' ] = $email_error;
}
else
{
if ( !filter_var( $email,
FILTER_VALIDATE_EMAIL ) )
{
$email_error = "Invalid email";
$_SESSION[ 'errormsg' ] = $email_error;
}
else
$email = input_test( $_POST[ 'email' ] );
}
//validate phone no
if ( empty( $_POST[ 'tel' ] ) )
{
$tel_error = "Phone number is required";
$_SESSION[ 'errormsg' ] = $tel_error;
}
else
{
if ( preg_match( "/^[0-9-]+$/",
$tel ) )
{
$tel_error = "Invalid phone number";
$_SESSION[ 'errormsg' ] = $tel_error;
}
$tel = input_test( $_POST[ 'tel' ] );
}
$address = input_test( $_POST[ 'address' ] );
$country = input_test( $_POST[ 'country' ] );
$code = input_test( $_POST[ 'code' ] );
$state = input_test( $_POST[ 'state' ] );
$ic = input_test( $_POST[ 'ic' ] );
if ( isset( $_SESSION[ 'errormsg' ] ) )
{
echo '############################################################\n\n';
echo '<br><br><br>\n\n';
echo $error = ( $_SESSION[ 'errormsg' ] ) .
'\n\n';
unset ( $_SESSION[ 'errormsg' ] );
echo '<br><br>\n\n';
echo '############################################################\n";
}
}
function input_test( $datatest )
{
$datatest = trim( $datatest );
$datatest = stripslashes( $datatest );
$datatest = htmlspecialchars( $datatest );
return $datatest;
}
//send data
$send_db = "INSERT INTO customer_details ( first_name,
last_name,
email,
address,
post_code,
state,
country,
no_phone,
ic )
VALUES ( '$fname',
'$lname',
'$email',
'$address',
'$code',
'$state',
'$country',
'$tel',
'$ic' )";
if ( $con -> query( $send_db ) === TRUE )
echo "<br><br>Records inserted successfully\n";
else
echo "Error : " .
$con -> error;
$con -> close();
?>