接收数据库中的数据时遇到问题
我试图通过制作一个html表单,然后通过$ _GET方法添加接收数据来接收数据。每当我尝试提交我的数据时,我都会收到一个错误,说'你的文件没有找到'在我的Chrome浏览器中,所以我试图通过在地址栏中输入“localhost/....”来打开我的php页面,并在那里显示'数据库未找到'接收数据库中的数据时遇到问题
这里是我的php代码: -
<html>
<?php
$user_name = "root";
$password = "";
$database = "mysql"; //mysql is name of my database
$server = "localhost";
$db_handle = mysql_connect($server , $user_name, $password,"addresbook"); //adressbook is where all the tabels are
$db_found = mysql_select_db($database, $db_handle);
if (!$db_found) {
print "Database Found ";
$x=$_GET['fname'];
$y=$_GET['sname'];
$z=$_GET['address'];
$sql="INSERT INTO addresbook(First_Name,Surname,Address) VALUES('".$x."','".$y."','".$z."')";
mysql_query($sql,$db_handle);
}
else {
print "Database NOT Found ";
}
?>
</html>
这里是山的html代码: -
<form action="practic.php"method="get">
Firstname:<input type="text" name="fname"><br>
Lastname:<input type="text" name="sname"><br>
Address:<input type="text" name="address"><br>
<input type="submit">
</form>
顺便说一句我正在使用wamp server.Thanks提前。
使用mysqli_connect
,因为现在不推荐使用mysql_connect
。
$db_handle = mysqli_connect($server , $user_name, $password,$database);
这里指用于连接
$db_found
会成功真的,所以你的情况应该是
if ($db_found) { // make DB changes etc.
,并切换到库MySQLi或PDO和使用准备好的语句已经提到,参考手册:
嗨改变你的代码,使用MysqLi
或使用PDO
<?php
$user_name = "root";
$password = "";
$database = "mysql"; //mysql is name of my database
$server = "localhost";
$con = mysqli_connect($server,$user_name,$password,'adresbook');//adresbook is where all the tabels are
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
// Change database to "adresbook"
$db_found = mysqli_select_db($con,'adresbook');
if ($db_found) {
print "Database Found ";
$x=$_GET['fname'];
$y=$_GET['sname'];
$z=$_GET['address'];
$sql="INSERT INTO addresbook(First_Name,Surname,Address) VALUES('".$x."','".$y."','".$z."')";
mysqli_query($con, $sql);
}
else {
print "Database NOT Found ";
}
?>
你phpMyadmin
应该像这样与数据库addresbook
用单s
。
我试过这个,它给了我这个错误“无法连接到MySQL :未知数据库'addresbook'“。 – Pawan
@Pawan错误表示没有名为“addresbook”的数据库,数据库不存在或您拼错数据库名称。如果数据库存在,那么它应该是'addressbook'而不是'addresbook'。 – julekgwa
我检查了拼写,它是addresbook.May是我混淆与数据库的名称,所以这里是我在想什么名字的数据库is.when我从我的phpadmin页面登录在左边有一堆选项其中我选择了mysql并创建了一个新的数据库名称'adresbook'.So这里我假设addresbook是我的数据库的名称。当我选择adresbook时,我有4个tabels(即'ID',' First_Name','Surname'和'Address')。因此,任何想法我正在做什么错误。谢谢回复:-) – Pawan
细节略显不足。特别是将mysql_connect()更改为mysqli_connect()时,将不得不更改**所有其他数据库访问调用以及使其工作** – RiggsFolly