2016-09-26 184 views
-1

我試圖插入數據到MySQL,但它不工作。這裏沒有在這段代碼中顯示任何錯誤。我試圖插入數據到MySQL,沒有錯誤,但數據沒有插入?

<?php 

$connection = mysqli_connect("localhost", "root", ""); 
if(!$connection) { 
    echo 'Server Not Found'; 
} 
else { 
    echo 'Server Found'; 
} 

// Establishing Connection with Server 
$db = mysqli_select_db($connection,"colleges"); // Selecting Database from Server 
if (!$db) { 
    echo 'Databaje Not Found'; 
} 
else { 
    echo 'Databaje found'; 
} 

if(isset($_POST['submit'])) { // Fetching variables of the form which travels in URL 
    $name = $_POST['name']; 
    $email = $_POST['email']; 
    $contact = $_POST['contact']; 
    $address = $_POST['address']; 

    if($name !=''||$email !='') { 
     //Insert Query of SQL 
     $query = mysqli_query("insert into students(student_name, student_email, student_contact,student_adress) values ('$name', '$email', '$contact', '$address')", $connection); 

     echo "<br/><br/><span>Data Inserted successfully...!!</span>"; 
    } 
    else { 
     echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>"; 
    } 
} 

mysqli_close($connection); // Closing Connection with Server 

?> 
+1

你得到哪個迴應信息作爲迴應? – Ma3x

+0

猜''學生地址'應該''student_address' – Carl

+1

$連接第一次在mysqli_query() – nogad

回答

0

在您的$查詢中,先寫入$ connection,然後寫入插入查詢afyer並添加一個逗號。這將有所幫助。如果$ query仍然失敗,也可以使用mysqli_error()函數查看查詢中的錯誤。