1

I make a php form and Insert the form entries into mysql database. php script and form relies on same page. Form also used validation rules. After fill all form entries finally when I hit submit I got no any error but when I checked my database there is no insert entry, what is the problemb? How to solve it? Here is my complete code..

<?php
// define variables and set to empty values
$regErr = $nameErr = $misErr = $vbErr = $statErr = $tmErr = "";
$reg = $name = $mis = $vb = $stat = $tm = "";


if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["Reg"]))
     {$regErr = "Reg ID is required";}
     else
     {
     $reg = $_POST["Reg"];
}
   if (empty($_POST["Name"]))
     {$nameErr = "Name is required";}
     else
     {
     $name = $_POST["Name"];

          // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name))
     {
       $nameErr = "Only letters and white space allowed"; 
       }
   }

   if (empty($_POST["Mis"]))
     {$misErr = "Please Enter Marks";}
   else
     {
     $mis = $_POST["Mis"];
     // check if value entered is Numeric
     if(!is_numeric($mis)) {
    $misErr = "Data entered was not numeric";
     }
     }

   if (empty($_POST["Vb"]))
     {$vbErr = "Please Enter Marks";}
   else
     {
     $vb = $_POST["Vb"];
     // check if value entered is Numeric
     if(!is_numeric($vb)) {
    $vbErr = "Data entered was not numeric";
     }
     }

     if (empty($_POST["Stat"]))
     {$statErr = "Please Enter Marks";}
   else
     {
     $stat = $_POST["Stat"];
     // check if value entered is Numeric
     if(!is_numeric($stat)) {
    $statErr = "Data entered was not numeric";
     }
     }

     if (empty($_POST["Tmarks"]))
     {$tmErr = "Please Enter Marks";}
   else
     {
     $tm = $_POST["Tmarks"];
     // check if value entered is Numeric
     if(!is_numeric($tm)) {
    $tmErr = "Data entered was not numeric";
     }
     }     
}
// 1. Creating Database Connection
    $connection = mysql_connect("localhost","root");
    if ($connection) {

print "Database Found";

}
else {

print "Database NOT Found";

}
    if (!$connection){
    die("Database connection failed:".mysql_error());
    }
    // 2. Select a Database to use
    $db_select = mysql_select_db("csresult",$connection);       
    if (!$db_select) {
        die ("Database Selection failed:".mysql_error());
            }

$sql= mysql_query("INSERT INTO record ('reg', 'name', 'mis', 'vb', 'stat', 'total') VALUES ('".$reg."', '".$name."', '".$mis."', '".$vb."', '".$stat."', '".$tm."')",$connection);
echo $sql;  

echo "1 record added";  

mysql_close($connection);

?>



<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>"> 
<table width=600 cellspacing=1 cellpadding=2 style='color:white;'>
<tr>
<td>Registration#:</td>
<td><input type="text" name="Reg"><span class="error">* <?php echo $regErr;?></span></td>
</tr>
<tr>
<td>Student Name:</td>
<td><input type="text" name="Name"> <span class="error">* <?php echo $nameErr;?></span></td>
</tr>
<tr>
<td>MIS Marks:</td>
<td><input type="text" name="Mis"><span class="error">*<?php echo $misErr;?></span></td>
</tr>
<tr>
<td>VB.NET Marks:</td>
<td><input type="text" name="Vb"><span class="error">*<?php echo $vbErr;?></span></td>
</tr>
<tr>
<td>Stat Marks:</td>
<td><input type="text" name="Stat"><span class="error">*<?php echo $statErr;?></span></td>
</tr>
<tr>
<td>Subject Total Marks:</td>
<td><input type="text" name="Tmarks"><span class="error">*<?php echo $tmErr;?></span></td>
</tr>
</table><br>
<p style='margin-left:300px;'><input type="submit" name="submit" value="Submit"></p>
</form>
3
  • 1- Change your INSERT query to INSERT INTO record (`reg`, `name`, `mis`, `vb`, `stat`, `total`) VALUES .., -> change the ' single quotes to ` backticks. 2- use or die(mysql_error() -> mysql_query(...) or die(mysql_error() when testing / in doubt to get your error message. 3- mysql_* functions are deprecated, update to mysqli_ or PDO. Commented Feb 1, 2014 at 17:55
  • Please don't use the mysql_ functions. They are deprecated. Instead, use mysqli_ or PDO. Commented Feb 1, 2014 at 17:55
  • $sql= mysql_query("INSERT INTO record ('reg', 'name', 'mis', 'vb', 'stat', 'total') VALUES ('$reg', '$name', '$mis', '$vb', '$stat', '$tm')",$connection); Commented Feb 1, 2014 at 17:58

4 Answers 4

1

You're putting single-quotes around your column names. Single-quotes are for string literals and date literals.

$sql= mysql_query("INSERT INTO record ('reg', 'name', 'mis', 'vb', 'stat', 'total') ...

Should be:

$sql= mysql_query("INSERT INTO record (reg, name, mis, vb, stat, total) ...

Some people are suggesting using back-ticks but that's not necessary unless your column names contain SQL reserved words or special characters.

You should always check the return value from mysql_query(), because it returns false if there's an error. If this is the case, you have to report the error message yourself.

$sql = mysql_query(...);
if ($sql === false) { die(mysql_error()); }

Other tips:

  • You're writing code using the ext/mysql functions, which are deprecated. You should use mysqli or PDO. These functions also return false if there's an error.

  • You're writing code in an unsafe manner. Before you put any of your code on the internet, please read about SQL injection: What is SQL injection? and How can I prevent SQL injection in PHP?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answering @Bill Karwin .. I used your second suggestion, since I am not familiar with mysqli_ syntax. Now adding mysql_error function I got this error "Database FoundUnknown column '' in 'field list'" . I checked very carefully all fields in database their names i can't find any issue of my declared columns. I don't know where is problem.
0

Use backtick instead of ' or remove ' from column names. if you used any reserved words as your column name then you should use backtick

Try this,

 $sql= mysql_query("INSERT INTO record (`reg`, `name`, `mis`, `vb`, `stat`, `total`) 
       VALUES ('".$reg."', '".$name."', '".$mis."', '".$vb."', '".$stat."', '".$tm."')") 
      or die(mysql_error());

instead of

 $sql= mysql_query("INSERT INTO record ('reg', 'name', 'mis', 'vb', 'stat', 'total') VALUES ('".$reg."', '".$name."', '".$mis."', '".$vb."', '".$stat."', '".$tm."')",$connection);

5 Comments

@ Krish R no any error comes, but after implementing Bill Karwin suggest I got this error "Unknown column '' in 'field list'" . I checked very carefully all fields in database their names i can't find any issue of my declared columns. I don't know where is problem.
did you implemented my code? i have added the mysql error synatx. and can you post your table structure
Unknown column 'stat' in 'field list'
May be in your table column name stat has some white space either front or after. Can you rename and check it
Thanks its working Now .. in my database the name of field state instead of stat.
0

Dont use mysql, use mysqli. Anyways use like

$query_insert = "INSERT INTO record (reg, name, mis, vb, stat, total) VALUES ('$reg', '$name', '$mis', '$vb', '$stat', '$tm')";
mysql_query($query_insert,$connection);

Comments

0

I also experienced this issue and found answer by myself. You only need to <form method="post" remove this method="post" and retype it again. Or also you can change the entire POST methods to GET, it will also work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.