3

以下是我从registration.php 页面重定向的test.php 页面。除了 sql 查询,这里的一切都很好。所有警报标签都很好地显示了会话变量值,但此页面给出了“错误,插入查询失败”错误。尝试了所有可能性,例如查询中变量的引号等,但没有任何效果..请有人帮忙..

<?php
include "connect.php";
session_start();

$ser= $_SESSION['service'];
$comp=$_SESSION['comp'];
$yr= $_SESSION['year'];
$addr= $_SESSION['addr'];
$per= $_SESSION['per'];
$no= $_SESSION['no'];
$email= $_SESSION['email'];
$uname= $_SESSION['uname']; 
  echo "<script>alert('$ser')</script>";
  echo "<script>alert('$comp')</script>";
  echo "<script>alert('$yr')</script>";
  echo "<script>alert('$no')</script>";
  echo "<script>alert('$addr')</script>";
if($ser=='designing'){
   $ser=1;
}else if($ser=='webdev'){
   $ser=2;
}else{
   $ser=3;
}
echo "<script>alert('$ser')</script>";
$sql= "insert into login values 
      ('$uname','xxxx',$ser,'$comp',$yr,'$addr','$per',$no,'$email')";
mysql_query($sql) or die('Error, insert query failed');
?>

<span style="font-family:Tahoma; font-weight:bold; font-size:11px;color:#666666" 
      align=center>Your Registration is successful...<br/>Our team will contact you at
      the earliest...
</span>
4

4 回答 4

8
mysql_query() or die(mysql_error());
于 2012-06-07T11:09:03.683 回答
0

非常有趣的公式。但是,我宁愿推荐这样的东西:

mysql_query($sql);
if (mysql_error()) die('Error, insert query failed');

如果您对它为何死亡感兴趣,那么:

mysql_query($sql);
if ($error = mysql_error()) die('Error, insert query failed with:' . $error);

但请注意,这种错误对用户来说是不可见的。

于 2012-06-07T11:03:08.177 回答
0
$sql= "insert into login values 
  ('$uname','xxxx',$ser,'$comp',$yr,'$addr','$per',$no,'$email')";

从变量中删除简单引号

  $sql= "insert into login values 
      ($uname, xxxx, $ser, $comp, $yr, $addr, $per, $no, $email)";
于 2015-01-31T16:50:30.027 回答
0
$sql= "insert into login (field1, field2, ....) values

('$uname','xxxx',$ser,'$comp',$yr,'$addr','$per',$no,'$email')";

另外请使用 BIND 而不是来自用户的未过滤数据。另外还有:请用mysqli_*代替旧的mysql_*

于 2015-01-31T16:52:49.157 回答