2016-11-05 170 views
0

我試圖從HTML表單和PHP腳本插入數據到MySQL數據庫。使用PHP插入數據到MySQL數據庫獲取錯誤

我能夠從腳本連接到MySQL服務器,但我無法連接到數據庫和表。所以我不能插入數據。

從PHP腳本觸發的錯誤。錯誤是'數據庫未被選中!'。但是我已經正確地聲明瞭這個名字。

這裏我上傳了full solution

下面我添加了HTML表單和PHP腳本。

如果可能,請您嘗試解決問題。

HTML表單

<html> 
<head> 
    <title>Registration Form</title> 
</head> 
<body> 
    <br /> 
    <h2 align="center">Register a new customer</h2> 
    <h3 align="center">Fill all fields</h3> 
    <form action="insertdata.php" method="post"> 
     <table align="center"> 
      <tr> 
       <td>First Name:</td> 
       <td><input type="text" id="fname" name="fname" /></td> 
      </tr> 
      <tr> 
       <td>Middle Name:</td> 
       <td><input type="text" id="mname" name="mname" /></td> 
      </tr> 
      <tr> 
       <td>Last Name:</td> 
       <td><input type="text" id="lname" name="lname" /></td> 
      </tr> 
      <tr> 
       <td>NIC/Driving License No:</td> 
       <td><input type="text" id="NICNo" name="nicno" /></td> 
      </tr> 
      <tr> 
       <td>Permanent Address Name:</td> 
       <td><input type="text" id="paddress" name="paddress" /></td> 
      </tr> 
       <tr> 
       <td>E-Mail Address:</td> 
       <td><input type="text" id="email" name="email" /></td> 
      </tr> 
       <tr> 
       <td>Telephone:</td> 
       <td><input type="text" id="telno" name="telno" /></td> 
      </tr> 
      <tr> 
       <td>Mobile No:</td> 
       <td><input type="text" id="mobileno" name="mobileno" /></td> 
      </tr> 
       <tr> 
       <td>Date of Registration:</td> 
       <td><input type="text" id="regdate" name="regdate" /></td> 
      </tr> 
       <tr><td></td></tr> 
       <tr><td></td></tr> 
       <tr><td></td></tr> 
       <tr><td></td></tr> 
       <tr><td></td></tr> 
       <tr><td></td></tr> 
      <tr> 
      <td align="center"><input type="submit" />&nbsp;&nbsp;&nbsp; <input type="reset" /></td> 

      </tr> 
     </table> 
    </form> 

    </body> 

InsertData.php

<?php 

    $dbserver = 'localhost'; 
    $username = 'root'; 
    $password = ''; 

    $dbname = 'car_rental'; 

    $con = mysqli_connect($dbserver, $username, $password); 

    if(!$con) 
{ 
    echo "Not connected to server !"; 
    echo 'ERROR : '.mysql_error(); 

}else{ 
    echo "Server connected !"; 
} 

    if(!mysqli_select_db($con, $dbname)); 
    { 
     echo "Database not selected !"; 
     echo 'ERROR : '.mysql_error(); 
    } 

    $fname = $_POST["fname"]; 
    $mname = $_POST["mname"]; 
    $lname = $_POST["lname"]; 
    $nicno = $_POST["nicno"]; 
    $paddress = $_POST["paddress"]; 
    $email = $_POST["email"]; 
    $telno = $_POST["telno"]; 
    $mobileno = $_POST["mobileno"]; 
    $regdate = $_POST["regdate"]; 

    $sql = "INSERT INTO new_customers (FirstName, MiddleName, LastName, NICNo, PermanentAddress, EmailAddress, Telephone, Mobile, RegDate) VALUES ('$fname','$mname','$lname','$nicno','$paddress','$email','$telno','$mobileno','$regdate')"; 

if (!mysqli_query($con,$sql)) 
    { 
     echo "Data not inserted !"; 
     echo 'ERROR : '.mysql_error(); 
    } 
    else 
    { 
    echo "Data inserted !"; 
    } 

    header("refresh:300; url=form.php"); 

    ?> 
+0

您可以使用mysqli OOPS概念示例:http://php.net/manual/en/mysqli.insert-id.php –

+1

1.修復錯誤檢測,以便不使用過時的和不推薦使用的mysql _...( )'函數,堅持'mysqli _ :: :()'函數。 – arkascha

+0

2.檢查你的http服務器錯誤日誌文件,看看實際發生了什麼。 – arkascha

回答

1

嘗試

mysqli_connect($dbserver, $username, $password, $dbname);

0

嘗試這種

$ PDO =新PDO( 'MySQL的:主機=本地主機; DBNAME = [數據庫名稱]', '根', '');

0

嘗試:

<?php 

$dbserver = 'localhost'; 
$username = 'root'; 
$password = ''; 
$dbname = 'car_rental'; 

$con = mysqli_connect($dbserver, $username, $password); 

if(!$con) { 
    echo "Not connected to server !"; 
    echo 'ERROR : '.mysqli_error($con); 
} else { 
    echo "Server connected !"; 
} 

if(!mysqli_select_db($con, $dbname)) { 
    echo "Database not selected !"; 
    echo 'ERROR : '.mysqli_error($con); 
} 

$fname = $_POST["fname"]; 
$mname = $_POST["mname"]; 
$lname = $_POST["lname"]; 
$nicno = $_POST["nicno"]; 
$paddress = $_POST["paddress"]; 
$email = $_POST["email"]; 
$telno = $_POST["telno"]; 
$mobileno = $_POST["mobileno"]; 
$regdate = $_POST["regdate"]; 

$sql = "INSERT INTO new_customers (FirstName, MiddleName, LastName, NICNo, PermanentAddress, EmailAddress, Telephone, Mobile, RegDate) VALUES ('$fname','$mname','$lname','$nicno','$paddress','$email','$telno','$mobileno','$regdate')"; 

if (!mysqli_query($con,$sql)) { 
    echo "Data not inserted !"; 
    echo 'ERROR : '.mysqli_error($con); 
} else { 
    echo "Data inserted !"; 
} 

header("refresh:300; url=form.php"); 

?> 

我沒有測試它。