2016-09-26 186 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()函数查看查询中的错误。