我收到此错误:
警告:mysql_select_db()期望参数1为string,object 在第11行的C:\ xampp \ htdocs \ apply \ insert.php中给出 未选择数据库未插入
<?php
$con = mysqli_connect('localhost','root','');
if (!$con)
{
echo 'Not connected to server';
}
if (!mysql_select_db($con,'staff'))
{
echo "Database not selected";
}
$Name = $_POST['username'];
$Email = $_POST['email'];
$sql = "INSERT INTO person (Name, Email) VALUES ('$Name','$Email')";
if(!mysqli_query($con,$sql))
{
echo 'Not inserted';
}
else {
echo 'Inserted';
}
header("refresh:2; url=index.html");
?>
答案 0 :(得分:1)
您正在将mysql_
与mysqli_
个功能混合使用。
将if (!mysql_select_db($con,'staff'))
更改为if (!mysqli_select_db($con,'staff'))
答案 1 :(得分:0)
编写如下代码:
<?php
$con = mysqli_connect('localhost','root','');
if (!$con)
{
echo 'Not connected to server';
}
if (!mysqli_select_db($con,'staff'))
{
echo "Database not selected";
}
$Name = $_POST['username'];
$Email = $_POST['email'];
$sql = "INSERT INTO person (Name, Email) VALUES ('$Name','$Email')";
if(!mysqli_query($con,$sql))
{
echo 'Not inserted';
}
else {
echo 'Inserted';
}
答案 2 :(得分:0)
像这样编写你的代码
<?php
$con = mysqli_connect('localhost','root','','staff');
if (!$con)
{
echo 'connection not established';
}
$Name = $_POST['username'];
$Email = $_POST['email'];
$sql = "INSERT INTO person (Name, Email) VALUES ('$Name','$Email')";
if(!mysqli_query($con,$sql))
{
echo 'Not inserted';
}
else {
echo 'Inserted';
}