i am facing problem with my code
when i click on add record button page refresh but new record not saved in sql database
help me please
thanks
- Code: Select all
<?php
//conenction file
include "connection.php";
//cleaning string
function cleanstring ($string)
{
$string=htmlspecialchars (trim($string));
return $string;
}
//function for new form
function print_Form() {
//new form var
$newform = "<form method=\"POST\"action=\"{$_SERVER['PHP_SELF']}\">
<table border=1 cellpadding=3 cellspacing=3>
<tr><td> Complaint Number </td>
<td><input type=\"number\"name=\"Complaint number\"></td></tr>
<tr><td>Name</td>
<td><input type=\"text\"name=\"Name\"></td></tr>
<tr><td> Address </td>
<td><input type=\"text\"name=\"Address\"></td></tr>
<tr><td>CNIC</td>
<td><input type=\"number\"name=\"CNIC\"></td></tr>
<tr><td>Comments</td>
<td><input type=\"text\"name=\"Commetns\"></td></tr>
<tr><td colspan=2><input type=\"submit\"value=\"Add Record\">
<input type=\"hidden\"value=\"true\"name=\"POST\">
</td></tr></table></form>";
return $newform;
}
//saving record
function save_Record() {
$Complaint_Number = cleanString ($_POST['Complaint Number']);
$Name = cleanString ($_POST['Name']);
$Address = cleanString ($_POST['Address']);
$CNIC = cleanString ($_POST ['CNIC']);
$Comments = cleanString ($_POST['Comments']);
//checking for empty values
if(empty($Complaint_Number) || empty ($Name) || ($Address) || empty ($CNIC) || ($Comments) ) {
error_Message ("Cannot insert empty record");
}
//query
$query = "INSERT INTO complaint_cell(Complaint Number,Name,Address,CNIC,Comments)
VALUES ('$Complaint_Number','$Name','$Address','$CNIC','$Comments')";
//execute query
if(mysql_query ($query)) {
print "<div style='background:#FFFFCC;border:dashed 1px #CCCCCC'>
Record added successfully </div>";
print print_Form(); //show form again
}else{
error_Message (mysql_error()); //if error in query
}
}
//error message
function error_Message ($error) {
$msg = "<div style='background:#FFFFCC;border:dashed 1px #CCCCCC'>
$error <a href='javascript:history.go (-1) '> Back</a></div>";
die($msg);
}
//what to do
if(isset($_POST
[
'post'])) {
// if form is POSTED
save_Record();
}else{
//first time print the form
print print_Form();
}
?>