1

使用 _POST 方法在数据库中插入数据时,PHP 代码会插入空白记录,但是当我使用 _GET 时,一切正常。

感谢提前。

<?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);
?>

任何建议如何避免这种情况......

4

6 回答 6

0

它适用于 _REQUEST 我认为它会做

<?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);
?>
于 2013-10-16T04:56:37.940 回答
0

原因是你很可能做的帖子和 get 不一样

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

和所有其他人一样。您必须使用引号来获取这些值而没有引号,您的索引就像textnames对于GETPOST

于 2013-10-16T04:10:44.463 回答
0

您可以像这样更改代码。关键是您在访问 post 变量时错过了单引号。

<?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);

    }

}
?>
于 2013-10-16T11:34:35.667 回答
0

首先检查您的表单是否通过

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

      }
 also in form try using following code
 <form methode = "POST" action = "">
 </form>
于 2013-10-16T04:30:43.990 回答
0

尝试将 $_POST 初始化为局部变量并使用如下查询

    <?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);
?>
于 2013-10-16T04:22:54.897 回答
0

一个好的经验法则是使用 phpinfo 检查 PHP 变量的可用性(这与您的 Web 服务器配置有关)。

尝试添加:

echo phpinfo();
exit;

就在你之前

 if (!mysqli_query($con,$sql))
于 2013-10-16T03:59:41.647 回答