我遇到错误,没有选择任何数据库,任何人都可以更正代码。
我正在尝试学习php和mysql。所以我尝试使用
建立数据库
phpmyadmin并将其与我的php连接。这是一个简单的示例,在这里我尝试查看数据库是否正常工作。
<?php
//Get values passe from form in donateform.html.
$link;
$Name = $_POST['Name'];
$Mobile = $_POST['Mobile'];
$Email = $_POST['Email'];
$Donating =isset($_POST['Donating']);
$Address = isset($_POST['Address']);
//To prevent mysql injection
$Name = stripcslashes($Name);
$Mobile = stripcslashes($Mobile);
$Email = stripcslashes($Email);
$Donating = stripcslashes($Donating);
$Address = stripcslashes($Address);
//connect to the server and select database.
$link=mysqli_connect("localhost", "gooddeeds", "");
mysqli_select_db($link,"donaters");
$Name = mysqli_real_escape_string($link,$Name);
$Mobile = mysqli_real_escape_string($link,$Mobile);
$Email = mysqli_real_escape_string($link,$Email);
$Donating = mysqli_real_escape_string($link,$Donating);
$Address = mysqli_real_escape_string($link,$Address);
//Query the database for user
$result =mysqli_query($link,"INSERT INTO donaters (Name, Mobile, Email, Donating, Address) VALUES ('$Name', '$Mobile', '$Email', '$Donating', '$Address')")
or die("Failed to query daabase".mysqli_error($link));
if(mysqli_query($result)){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. ".mysqli_error($link);
}
?>
我得到
Database query failed::: No database selected
这意味着这部分代码
//connect to the server and select database.
$link=mysqli_connect("localhost", "gooddeeds", "");
mysqli_select_db($link,"donaters");
不起作用(如果有,我会在其中分别添加不同数量的“:”。将不胜感激!谢谢!
答案 0 :(得分:0)
您可以尝试使用以下作为databasename.tablename(gooddeeds.donaters)
INSERT INTO gooddeeds.donaters (Name, Mobile, Email, Donating, Address) VALUES ('$Name', '$Mobile', '$Email', '$Donating', '$Address')
答案 1 :(得分:0)
这是php文档中的第一个示例。尝试使用条件检查连接。
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}