1

PHP code is inserting blank records when inserting data in the database with the _POST Method, However when I use _GET everything works fine.

Thanks in Advace.

<?php
$con=mysqli_connect("localhost","root","*******","student");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO Student (textnames, fathername, mom, occu, homenum, paddress, offcontact, Course, District, State, pincode, emailid, dob, mobileno)
VALUES
('$_POST[textnames]','$_POST[fathername]','$_POST[mom]','$_POST[occu]','$_POST[homenum]','$_POST[paddress]','$_POST[offcontact]','$_POST[Course]','$_POST[District]','$_POST[State]','$_POST[pincode]','$_POST[emailid]','$_POST[dob]','$_POST[mobileno]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "Successfully Added Record";

mysqli_close($con);
?>

Any suggestions how to avoid this.....

CrashOverride
  • 338
  • 3
  • 10
  • 22

6 Answers6

0

a good rule of thumb is checking PHP Variables availability with phpinfo (this is related to your web server configuration).

Try and add:

echo phpinfo();
exit;

just before your

 if (!mysqli_query($con,$sql))
Augusto
  • 2,125
  • 18
  • 27
0

The reason is that you are most probably doing the post not same as get

'$_POST[textnames]'// is wrong
'$_POST["textnames"]'// is correct

and same with all others. You must have used quotes to get these values without quotes your index like textnames would be incorrect both for GET and POST

Sami
  • 8,168
  • 9
  • 66
  • 99
0

try to initialise the $_POST to a local variable and use the query as follows

    <?php
$a=$_POST['textname'];
$b=$_POST['fathername'];
$c=$_POST['mom'];'
$d=$_POST['occu'];
$e=$_POST['homenum'];
$f=$_POST['paddress'];
$g=$_POST['offcontact'];
$i=$_POST['Course'];
$j=$_POST['District'];
$k=$_POST['State'];
$l=$_POST['pincode'];
$m=$_POST['emailid'];
$o=$_POST['dob'];
$p=$_POST['mobileno']
$con=mysqli_connect("localhost","root","*******","student");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO Student (textnames, fathername, mom, occu, homenum, paddress, offcontact, Course, District, State, pincode, emailid, dob, mobileno) VALUES('$a','$b','$c','$d','$e','$f','$g','$i','$j','$k','$l','$m','$o','$p')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "Successfully Added Record";

mysqli_close($con);
?>
Arun
  • 119
  • 1
  • 9
0

First check whether your form is sending value by

if(isset($_POST["submit"]))
     {
       Your insert query here

      }
 also in form try using following code
 <form methode = "POST" action = "">
 </form>
rohitr
  • 371
  • 2
  • 11
  • i have checked this it is working for me if you are not having submit button try with some different value in post e.g. if(isset($_POST["textnames"])) {} also try to insert values with double quotes eg. $_POST["tesxtnames"] – rohitr Oct 16 '13 at 06:11
0

It works with the _REQUEST i think it'll do

<?php
$con=mysqli_connect("localhost","root","********","student");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO Student (textnames, fathername, mom, occu, homenum, paddress, offcontact, Course, District, State, pincode, emailid, dob, mobileno)
VALUES
('$_REQUEST[textnames]','$_REQUEST[fathername]','$_REQUEST[mom]','$_REQUEST[occu]','$_REQUEST[homenum]','$_REQUEST[paddress]','$_REQUEST[offcontact]','$_REQUEST[Course]','$_REQUEST[District]','$_REQUEST[State]','$_REQUEST[pincode]','$_REQUEST[emailid]','$_REQUEST[dob]','$_REQUEST[mobileno]')";
if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "Successfully Added Record";

mysqli_close($con);
?>
CrashOverride
  • 338
  • 3
  • 10
  • 22
0

You can change your code like this. The point is you missed the single quotes when accessed the post variables.

<?php
$con=mysqli_connect("localhost","root","*******","student");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO Student (textnames, fathername, mom, occu, homenum, paddress, offcontact, Course, District, State, pincode, emailid, dob, mobileno)
VALUES
('".$_POST['textnames']."','".$_POST['fathername']."','".$_POST['mom']."','".$_POST['occu']."','".$_POST['homenum']."','".$_POST['paddress']."','".$_POST['offcontact']."','".$_POST['Course']."','".$_POST['District']."','".$_POST['State']."','".$_POST['pincode']."','".$_POST['emailid']."','".$_POST['dob']."','".$_POST['mobileno']."')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "Successfully Added Record";

mysqli_close($con);

    }

}
?>
Sanjay Kumar N S
  • 4,653
  • 4
  • 23
  • 38