MySQL 查询显示 PHP 中的死错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10930415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:23:53  来源:igfitidea点击:

MySQL query shows die error in PHP

phpmysql

提问by mano

The following is my test.php page which gets redirected from registration.php page. everything over here works very fine except sql query. All the alert tags show session variable values perfectly well, but this page gives 'Error, insert query failed' error. Tried all possibilities like quotes to variables in query, etc., But nothing is working.. Please someone help..

以下是我从 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>

回答by Gustav Westling

mysql_query() or die(mysql_error());

回答by Lweek

Very interesting formula. However, I would rather recommend something like this:

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

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

If You are interested in why it died, then:

如果您对它死亡的原因感兴趣,那么:

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

But beware, this kind of error shall not be visible for users.

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

回答by afarazit

$sql= "insert into login values 
  ('$uname','xxxx',$ser,'$comp',$yr,'$addr','$per',$no,'$email')";

Remove the simple quotes from your variables

从变量中删除简单的引号

  $sql= "insert into login values 
      ($uname, xxxx, $ser, $comp, $yr, $addr, $per, $no, $email)";

回答by Seti

$sql= "insert into login (field1, field2, ....) values

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

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

Also please use BIND and not unfiltered data from user. Another Also: please use mysqli_*instead of old mysql_*

另外请使用 BIND 而不是来自用户的未经过滤的数据。另一个另外:请使用mysqli_*而不是旧的mysql_*