我用的是WAMP5,用phpmyadmin可以连接数据库并正常操作,但是在php里就不行,怎么回事啊?我新手。连接代码如下:
$conn =mysql_connect("localhost", "root", "") or die("数据库链接错误");
$result=mysql_select_db("info", $conn);
mysql_query("set names 'GBK'"); //使用GBK中文编码;
if($result){echo "建立完成";
}else{echo "建立错误".mysql_error();}在另一个php文件中插入数据代码如下:
<?php
/*
* Created on 2011-6-9
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
include("conn.php");
if($_POST['submit']){
$sql="insert into test (id,user,password) values('','$_POST[user_name]','$_POST[PW]')";
mysql_query($sql);
echo "成功";
}
?>
<form action="add_user.php" method="post">
用户:<input type="text" name="user_name" /><br/>
密码:<input type="text" name="PW" /><br/>
<input type="submit" name="submit" value="登陆"/>
</form>既不显示连接成功也不显示错误,这怎么回事啊?
$conn =mysql_connect("localhost", "root", "") or die("数据库链接错误");
$result=mysql_select_db("info", $conn);
mysql_query("set names 'GBK'"); //使用GBK中文编码;
if($result){echo "建立完成";
}else{echo "建立错误".mysql_error();}在另一个php文件中插入数据代码如下:
<?php
/*
* Created on 2011-6-9
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
include("conn.php");
if($_POST['submit']){
$sql="insert into test (id,user,password) values('','$_POST[user_name]','$_POST[PW]')";
mysql_query($sql);
echo "成功";
}
?>
<form action="add_user.php" method="post">
用户:<input type="text" name="user_name" /><br/>
密码:<input type="text" name="PW" /><br/>
<input type="submit" name="submit" value="登陆"/>
</form>既不显示连接成功也不显示错误,这怎么回事啊?
error_reporting(E_ALL);另外,代码中这一句显然有问题:
$sql="insert into test (id,user,password) values('','$_POST[user_name]','$_POST[PW]')";
//↓
$sql="insert into test (id,user,password) values('','$_POST[\"user_name\"]','$_POST[\"PW\"]')";
有对应的库和表吗?
可以echo下$conn,$result看看库有没有连上。
还有你的变量名$conn和文件名同名,可能会有问题
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING。
如果不改的话,就没有问题。
问题估计处在你的'".$_POST[user_name]."'上了。 ser_name要加单引号。PW也是同理、、改一下,试试看。还有你的mysql_query($sql) 这么写好像是不对的。、
你改成$sel=mysql_query("select * from test where user='".$_POST['user_name']."' and password=$_POST['PW']");
$rows=mysql_num_rows($sel);
if($rows!=0)
{
echo "<script>alert('插入成功');history.go(-1)</script>";
exit();
}
else {
echo "<script>alert('插入失败');history.go(-1)</script>";
exit();
} 我也是新手,没学多长时间、、仅个人建议,你试试、、
就是 id user password的顺序
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
是编译时刻错误,表示你的串中有语法错误,与运行无关
2、你说没有显示,是否包括 建立完成 ?
3、提交后才会有数据库操作,你提交了吗?