Reputation: 57
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>
Upvotes: 1
Views: 8856
Reputation: 1
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.
Upvotes: 0
Reputation: 22711
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);
Upvotes: 0
Reputation: 1583
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);
Upvotes: 0
Reputation: 562230
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?
Upvotes: 1