我对这段代码有疑问:
代码
<?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
}
?>
如果我运行它会显示此错误消息:
Warning: mysql_query() expects parameter 2 to be resource, object given in /Applications/XAMPP/xamppfiles/htdocs/add/add-course.php on line 29 Could not enter data: Access denied for user ''@'localhost' to database 'ecom'
最佳答案
把mysql改成mysqli,mysql和mysqli不能同时使用。
$retval= mysqli_query($conn, $sql);
关于php - 用户访问被拒绝 & mysql_query() 期望参数 2 为资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36001018/