2016-11-25 63 views
1

我试图通过制作一个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提前。

回答

-1

使用mysqli_connect,因为现在不推荐使用mysql_connect

$db_handle = mysqli_connect($server , $user_name, $password,$database); 

这里指用于连接

http://www.w3schools.com/php/func_mysqli_connect.asp

+0

细节略显不足。特别是将mysql_connect()更改为mysqli_connect()时,将不得不更改**所有其他数据库访问调用以及使其工作** – RiggsFolly

-1

嗨改变你的代码,使用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

enter image description here

+0

我试过这个,它给了我这个错误“无法连接到MySQL :未知数据库'addresbook'“。 – Pawan

+0

@Pawan错误表示没有名为“addresbook”的数据库,数据库不存在或您拼错数据库名称。如果数据库存在,那么它应该是'addressbook'而不是'addresbook'。 – julekgwa

+0

我检查了拼写,它是addresbook.May是我混淆与数据库的名称,所以这里是我在想什么名字的数据库is.when我从我的phpadmin页面登录在左边有一堆选项其中我选择了mysql并创建了一个新的数据库名称'adresbook'.So这里我假设addresbook是我的数据库的名称。当我选择adresbook时,我有4个tabels(即'ID',' First_Name','Surname'和'Address')。因此,任何想法我正在做什么错误。谢谢回复:-) – Pawan

相关问题