Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php &引用;“未选择任何数据库”;_Php_Mysql_Sql_Database - Fatal编程技术网

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编写它