无法输入数据:未选择数据库

时间:2017-05-26 09:58:41

标签: php database connect

以下是我收到错误的代码无法输入数据:未选择数据库 我尝试在localhost上执行此代码。我无法弄清楚问题,因为我也创建了数据库mb以及表格。

<HTML> <HEAD>
<TITLE>Sample Site</TITLE> </HEAD>
<BODY>
<body text="Blue" bgcolor="Bisque"> <h1 align="center">User query...!
</h1> <?php
if(isset($_POST['add'])) {
$conn = mysqli_connect('localhost','','');

if(! $conn ) {
die('Could not connect: ' . mysqli_error($conn));
}
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$messege = $_POST['messege'];
$sql = "INSERT INTO users ". "(name,email,subject,messege) ". 
"VALUES('$name','$email','$subject', '$messege')";
$conn->select_db('mb');
$retval = mysqli_query($conn, $sql); if(! $retval ) {
die('Could not enter data: ' . mysqli_error($conn)); }
echo "Entered data successfully\n";
mysqli_close($conn); }else {
?>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<table width = "400" border = "0" > <tr>
 <td width = "100">Name</td> <td><input name = "name" type = "text"
id = "name"></td> </tr>
<tr>
<td width = "100">Email</td> <td><input name = "email" type = "text"
id = "email"></td> </tr>
<tr>
<td width = "100">Subject</td> <td><input name = "subject" type = "text"
id = "subject"></td> </tr>
<tr>
<td width = "100">Messege</td> <td><input name = "messege" type = "text"
id = "messege"></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 user">
</td> </tr>
</table> </form>
<?php }
?> </BODY>
</HTML>

6 个答案:

答案 0 :(得分:0)

$conn = mysqli_connect("localhost", "my_user", "my_password", "my_database_name");

或建立连接后,您可以选择具有单独功能的数据库:

mysqli_select_db($conn, "my_database_name");

答案 1 :(得分:0)

mysqli_connect包含4个参数,您必须添加第四个参数作为数据库名称,即

  

mysqli_connect(&#34; 127.0.0.1&#34;,&#34; my_user&#34;,&#34; my_password&#34;,&#34; my_db&#34;);

答案 2 :(得分:0)

' ab'功能出错。 它应该是

mysqli_connect

而不是

$conn = mysqli_connect("localhost","root","","databaseName");

答案 3 :(得分:0)

您的mysqli_connect存在问题。尝试使用以下代码连接

// Create connection
$conn = mysqli_connect('servername', 'username', 'password', 'dbname');
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

答案 4 :(得分:0)

除了提供的答案外,您还应该使用准备好的陈述。您正在使用MYSQLI。

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// prepare and bind
$stmt = $conn->prepare("INSERT INTO `users`(`name`, `email`, `subject`,`messege`) VALUES (?, ?, ?,?)");//use placeholders
$stmt->bind_param("ssss", $name,$email, $subject, $message);//bind placeholders to values.no need to escape. 
if($stmt->execute() == true){
  echo 'Successfully saved using prepared statements';
} else {
  echo 'Error!Because: '.$stmt->error;
  exit;

}

答案 5 :(得分:0)

添加您的用户名和数据库名称

 $conn = mysqli_connect('localhost','username here','','databasename here');