Php &引用;“未选择任何数据库”;
因此,我无法将数据插入数据库。我当前的错误是“未选择数据库”错误,但我看不出问题出在哪里。我的代码是:Php &引用;“未选择任何数据库”;,php,mysql,sql,database,Php,Mysql,Sql,Database,因此,我无法将数据插入数据库。我当前的错误是“未选择数据库”错误,但我看不出问题出在哪里。我的代码是: <?php $con=mysqli_connect("localhost","root","","db_test"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $firstname=
<?php
$con=mysqli_connect("localhost","root","","db_test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$firstname=$_POST['firstnameReg'];
$surname=$_POST['surnameReg'];
$mypassword=$_POST['passwordReg'];
$email=$_POST['emailReg'];
$result= mysql_query("INSERT INTO tbl_customers (firstname, lastname, email, password) VALUES ('$firstname', '$surname', '$email', '$mypassword'");
echo "$firstname", "$surname", "$mypassword", "$email";
if($result)
{
echo "Success!";
}
else
{
die(mysql_error());
}
mysqli_close($con);
?>
您在代码中同时使用了mysqli
和mysql
。您可以而且应该只使用mysqli
在代码中同时使用mysqli
和mysql
。您可以而且应该只使用mysqli
而不是来自错误库的mysql\u query
,您应该使用
$result= mysql_query("INSERT INTO tbl_customers (firstname, lastname, email, password) VALUES ('$firstname', '$surname', '$email', '$mypassword'");
因此,在您的例子中,值中也缺少右括号:
$result = mysqli_query($con, "INSERT INTO tbl_customers (firstname, lastname, email, password) VALUES ('$firstname', '$surname', '$email', '$mypassword')");
借此机会,您还应该实现prepared语句以避免SQL注入:
$sql = "INSERT INTO tbl_customers (firstname, lastname, email, password) VALUES (?, ?, ?, ?)";
if (!$insert = $con->prepare($sql))
die('Query failed: (' . $con->errno . ') ' . $con->error);
if (!$insert->bind_param('ssss', $firstname, $surname, $email, $mypassword))
die('Binding parameters failed: (' . $insert->errno . ') ' . $insert->error);
if (!$insert->execute())
die('Execute failed: (' . $insert->errno . ') ' . $insert->error);
else
echo "Success!";
mysqli_close($con);
您应该使用
$result= mysql_query("INSERT INTO tbl_customers (firstname, lastname, email, password) VALUES ('$firstname', '$surname', '$email', '$mypassword'");
因此,在您的例子中,值中也缺少右括号:
$result = mysqli_query($con, "INSERT INTO tbl_customers (firstname, lastname, email, password) VALUES ('$firstname', '$surname', '$email', '$mypassword')");
借此机会,您还应该实现prepared语句以避免SQL注入:
$sql = "INSERT INTO tbl_customers (firstname, lastname, email, password) VALUES (?, ?, ?, ?)";
if (!$insert = $con->prepare($sql))
die('Query failed: (' . $con->errno . ') ' . $con->error);
if (!$insert->bind_param('ssss', $firstname, $surname, $email, $mypassword))
die('Binding parameters failed: (' . $insert->errno . ') ' . $insert->error);
if (!$insert->execute())
die('Execute failed: (' . $insert->errno . ') ' . $insert->error);
else
echo "Success!";
mysqli_close($con);
转到php.net,找到您想在mysqli\ucode>中使用的每个mysql\ucode>语法
您看到的每一个警告,即mysql\uuu
语法已被弃用,都会显示将要使用的函数
例如:mysqli_connect()
替换了这两个mysql_connect()
和mysql_select_db()代码>(在一个函数中要求服务器和数据库)
不要担心编辑,几乎所有的重要函数都(几乎)相同,所以你不必编辑那么多。转到php.net,找到你想在mysqli\uuu
中使用的每种mysqli\uu
语法
您看到的每一个警告,即mysql\uuu
语法已被弃用,都会显示将要使用的函数
例如:mysqli_connect()
替换了这两个mysql_connect()
和mysql_select_db()代码>(在一个函数中要求服务器和数据库)
不要担心编辑,几乎所有重要功能都(几乎)相同,因此您不必编辑那么多。我明白了。我是新手,所以我看了很多不同的教程。这个问题有没有简单的解决方法?mysql函数都有mysqli等价物。手册会告诉你每一个。他们几乎一模一样,太棒了。谢谢。我通过将$con添加到查询的开头来修复它。我明白了。我是新手,所以我看了很多不同的教程。这个问题有没有简单的解决方法?mysql函数都有mysqli等价物。手册会告诉你每一个。他们几乎一模一样,太棒了。谢谢。我把$con添加到了查询的开头。先生,你太棒了。这解决了所有问题。谢谢,先生,你真了不起。这解决了所有问题。谢谢。您是否使用过mysql\u select\u db(您的数据库名称);在你的代码里???在“(双引号)之前插入值后,请关闭一个括号。不要在代码中同时使用mysql和mysqli。请尝试在mysqli中使用mysql_select_db(您的数据库名称);在代码中使用mysql_select_db(您的数据库名称);在“(双引号)之前插入值后,请关闭一个括号。不要在代码中同时使用mysql和mysqli。尝试用mysqli和PDO编写它