Home > Backend Development > PHP Problem > How to debug sql statement errors in php

How to debug sql statement errors in php

藏色散人
Release: 2023-03-11 12:46:01
Original
2476 people have browsed it

How to debug sql statement errors in php: 1. Debug PHP SQL errors through the die statement; 2. Debug PHP SQL errors through the "mysql_error" statement.

How to debug sql statement errors in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to debug sql statement errors in php?

Debug PHP SQL errors through die and mysql_error statements

1. Code

<!--处理文件代码-->
<?php 
$conn=mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;root&#39;) or die("与服务器连接失败!");
  mysql_select_db(&#39;db_database22&#39;,$conn) or die("没有找到数据库!");
  if($_POST[Submit]=="提交"){
$admin=$_POST[admin];//获取提交的用户名
$pass=$_POST[pass];//获取提交的密码
$dates=date("Y-m-d H:i:s");//定义当前时间
$query="insert into user (admin,password,dates) values(&#39;$admin&#39;,&#39;$pass&#39;,&#39;$dates&#39;)";//编写添加语句
$result=mysql_query($query,$conn) or die(mysql_error()); //执行添加语句,并返回错误信息
    if($result){
   echo "<script>alert(&#39;添加成功!&#39;); window.location.href=&#39;index.php&#39;;</script>";
}else{ 
echo "添加失败!!"; 
}    
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>SQL语句错误</title>
</head>
<style type="text/css">
<!--
.STYLE1 {color: #FF0000}
-->
</style>
<body>
<!--表单提交页,设置的表单元素-->
<form name="form1" method="post" action="index.php">
<table width="240" border="1" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#FF0000">
  <tr>
    <td width="80" align="right" bgcolor="#FFFFFF"><span class="STYLE1">管理员:</span></td>
    <td width="147" bgcolor="#FFFFFF"><input name="admin" type="text" id="admin" size="20"></td>
  </tr>
  <tr>
    <td align="right" bgcolor="#FFFFFF"><span class="STYLE1">密码:</span></td>
    <td bgcolor="#FFFFFF"><input name="pass" type="password" id="pass" size="20"></td>
  </tr>
  <tr>
    <td colspan="2" align="center" bgcolor="#FFFFFF"><input type="submit" name="Submit" value="提交"></td>
  </tr>
</table>
</form>
</body>
</html>
Copy after login

2. Running results

Unknown column &#39;password&#39; in &#39;field list&#39;
Copy after login

Recommended learning: "PHP video tutorial"

The above is the detailed content of How to debug sql statement errors in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template