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.
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('localhost','root','root') or die("与服务器连接失败!"); mysql_select_db('db_database22',$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('$admin','$pass','$dates')";//编写添加语句 $result=mysql_query($query,$conn) or die(mysql_error()); //执行添加语句,并返回错误信息 if($result){ echo "<script>alert('添加成功!'); window.location.href='index.php';</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>
2. Running results
Unknown column 'password' in 'field list'
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!