警告:mysql_query()期望参数1为字符串,资源在

时间:2014-08-20 12:41:11

标签: php html

当我点击提交按钮时,我的代码会给我一个错误。

它给了我"警告:mysql_query()期望参数1是字符串,资源在第31行&#34中的F:\ xampp \ htdocs \ phpprjct \ Untitled-1.php中给出;

在其他行中,我的代码显示" 1条记录已添加"

但是当我刷新数据库时,没有找到记录。

<!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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="#" method="post">
UserName: <input type="text" name="username"><br />
Passward: <input type="text" name="passward"><br />
Name: <input type="text" name="name">
<input type="submit" name="Submit" value="Sent">
</form>


<?php 
$con=mysql_connect("localhost","root","","firstphp");

if(isset($_POST['Submit']))
 {
$UserName = $_POST['username'];
$Password = $_POST['passward'];    
$Name = $_POST['name'];


$sql="INSERT INTO users (username, passward, name)
VALUES ($UserName, $Password, $Name, NOW())";
$result = mysql_query($sql);

if (mysql_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo "1 record added";

/*if($result){
echo "pass";
}
else {
echo "ERROR" . mysql_error($con);
}*/

mysql_close();
}
?>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

见下文

    if (mysql_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo "1 record added"

echo“1 record added”超出了if条件,因此它将在任何条件下打印。 虽然您使用的mysql_query格式有点错误。 从PHP手册,下面是正确的方法,

mixed mysql_query ( string $query [, resource $link_identifier = NULL ] )

因此,您应使用mysql_query($con, $sql)

而不是mysql_query($sql, $con)

希望它有所帮助。如果您需要任何进一步的知识,请按照以下网址 PHP Manual for mysql_qeury

答案 1 :(得分:0)

首先你多次执行mysql_query,当你使用mysql_query时,你只给出了你提供连接的字符串,如果你想给连接看这个代码

$result = mysql_query($string,$conn);

if ( $result ) {
   $getID = mysql_insert_id();
} else {
  die('Error'.mysql_error());
}

并且plz使用mysqli insted of mysql因为mysql被折旧

答案 2 :(得分:-1)

你需要将$ con传递给mysql_query()。 请将您的代码更改为以下 -

$result = mysql_query($sql);
========>>>>>
$result = mysql_query($sql, $con);

这应该有效。检查并告诉我们.....