2016-03-15 47 views
0

我有问题,这样的代码:拒绝访问用户的mysql_query()预计参数2是资源

代码

<?php 
include('config.php'); 
?> 

<?php 
if(isset($_POST['add'])) 
{ 

$conn = mysqli_connect($servername, $username, $password); 
if(! $conn) 
{ 
die('Could not connect: ' . mysql_error()); 
} 

if(! get_magic_quotes_gpc()) 
{ 
$name_of_activity = addslashes ($_POST['name_of_activity']); 
$descriotion = addslashes ($_POST['descriotion']); 
} 
else 
{ 
$course_No = $_POST['name_of_activity']; 
$descriotion = $_POST['descriotion']; 
} 
$date = $_POST['date']; 

$sql = "INSERT INTO Activity". "(name_of_activity, descriotion, date)  ". "VALUES('$name_of_activity','$descriotion','$date')"; 
mysql_select_db('ecom'); 
$retval = mysql_query($sql, $conn); 
if(! $retval) 
{ 
die('Could not enter data: ' . mysql_error()); 
} 
echo "Entered data successfully\n"; 
mysql_close($conn); 
} 
else 
{ 
?> 
<form method="post" action="<?php $_PHP_SELF ?>"> 
<table width="400" border="0" cellspacing="1" cellpadding="2"> 
<tr> 
<td width="100">name_of_activity</td> 
<td><input name="name_of_activity" type="text" id="name_of_activity" />    </td> 
</tr> 
<tr> 
<td width="100"> descriotion </td> 
<td><input name="descriotion" type="text" id="descriotion" /></td> 
</tr> 
<tr> 
<td width="100">date </td> 
<td><input name="date" type="text" id="date" /></td> 
</tr> 
<tr> 
<td width="100"> </td> 
<td> </td> 
</tr> 
<tr> 
<td width="100"> </td> 
<td> 
<input name="add" type="submit" id="add" value="Add Activity" /> 
</td> 
</tr> 
</table> 
</form> 
<?php 
} 
?> 

如果我运行它表明此错误消息:

警告:mysql_query()期望参数2是资源,在第29行中的/Applications/XAMPP/xamppfiles/htdocs/add/add-course.php中给出的对象 无法输入数据:访问被拒绝用户'''localhost'到数据库'ecom'

+0

只是一个建议,但请使用'mysqli',因为'mysql'已经被弃用了。 – JanLeeYu

回答

0

将mysql更改为mysqli,你完全不能使用mysql和mysqli。

$retval= mysqli_query($conn, $sql); 
相关问题