Home > Backend Development > PHP Tutorial > 求大神相助,谢谢

求大神相助,谢谢

WBOY
Release: 2016-06-23 13:43:06
Original
1059 people have browsed it

<?phprequire_once("conn.php");if(mysql_query("insert into name set name='$name',tel='$tel',email='$email',QQ='$QQ',sex='$sex',brandname='$brandname',commoditybrand='$commoditybrand',productID='$productID',ordername='$ordername',shopname='$shopname',shopaddr='$shopaddr',invoicenumber='$invoicenumber',purchasingdate='$purchasingdate'",$conn)){echo "添加成功!";} else{	echo '添加失败!';}?>
Copy after login


报错 :
Notice: Undefined variable

Warning: mysql_query() expects parameter 2 to be resource, null given in


回复讨论(解决方案)

Notice: Undefined variable 后面的错误提示怎么不贴全?

第二句是说mysql_query 的第二个参数要是个连接资源,而你给的是null。

require_once("conn.php"); 换成include试试,,是不是$conn 链接句柄没有引进来?

第二个参数是空值,第二个插入数据库的变量是空值,检查插入的数据库的变量是否符合数据库的结构设定的要求,比如某个字段不允许空。而插入变量却是空,就会出错。很简单,第一要在插入之前做验证判断。另外就是把数据库结构里尽量设置的宽泛一点。也就是说不必要“非空”就设置默认空值就好。

Notice: Undefined variable: conn in G:\wamp\www\WWW\WWW\post.php on line 47

= - 第一个错误提示 完全版

$conn 变量未定义
请检查 conn.php 文件定义及加载情况

我自己改了一下 又出现一个错误

Warning: mysql_query(): 4 is not a valid MySQL-Link resource in G:\wamp\www\WWW\WWW\post.php on line 47

require_once("conn.php");if(mysql_query("insert into name set name='$name',tel='$tel',email='$email',QQ='$QQ',sex='$sex',brandname='$brandname',commoditybrand='$commoditybrand',productID='$productID',ordername='$ordername',shopname='$shopname',shopaddr='$shopaddr',invoicenumber='$invoicenumber',purchasingdate='$purchasingdate'",$con)){echo "添加成功!";} else{	echo "添加失败!";}?>
Copy after login

把conn.php文件内容贴出来。顺便在检查一下你的require_once里面路径对不对

<?phpheader('Content-Type: text/html; charset=gbk');  $con = mysql_connect("localhost", "root", "");if (!$con)  {  die('Could not connect: ' . mysql_error());  }$db_selected = mysql_select_db("root", $con);if (!$db_selected)  {  die ("无法连接到数据库 : " . mysql_error());  }mysql_close($con);?>
Copy after login


conn.php 内容

$con = mysql_connect("localhost", "root", "");

定义的是 $con,使用的是 $conn
这能不出错吗?

require_once("conn.php");if(mysql_query("insert into name set name='$name',tel='$tel',email='$email',QQ='$QQ',sex='$sex',brandname='$brandname',commoditybrand='$commoditybrand',productID='$productID',ordername='$ordername',shopname='$shopname',shopaddr='$shopaddr',invoicenumber='$invoicenumber',purchasingdate='$purchasingdate'",$conn)){echo "添加成功!";} else{	echo "添加失败!";}?>
Copy after login


<?phpheader('Content-Type: text/html; charset=gbk');  $conn = mysql_connect("localhost", "root", "");if (!$conn)  {  die('Could not connect: ' . mysql_error());  }$db_selected = mysql_select_db("root", $conn);if (!$db_selected)  {  die ("无法连接到数据库 : " . mysql_error());  }mysql_close($conn);?>
Copy after login


= - 全部重新定义 还是这个报错
Warning: mysql_query(): 4 is not a valid MySQL-Link resource in G:\wamp\www\WWW\WWW\post.php on line 47

你在 conn.php 中关闭了 mysql 连接:mysql_close($conn);
那么其后的代码怎么操作数据库呢?

我把 mysql_close($conn); 这个删掉 果然就没报错了 非常感谢
那我把mysql_close($conn); 这个加在

equire_once("conn.php");  if(mysql_query("insert into name set name='$name',tel='$tel',email='$email',QQ='$QQ',sex='$sex',brandname='$brandname',commoditybrand='$commoditybrand',productID='$productID',ordername='$ordername',shopname='$shopname',shopaddr='$shopaddr',invoicenumber='$invoicenumber',purchasingdate='$purchasingdate'",$conn)){echo "添加成功!";} else{    echo "添加失败!";}  ?>
Copy after login

这个后面可以吗  = - 还有我都成功了 但是数据库里没有我添加的表单数据

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