2015-11-06 72 views
1

我是PHP5中的新成員,我想使用AJAX和jQuery將數據插入到我的SQL中。我嘗試了太多的代碼,但沒有得到任何積極的迴應。如何使用PHP5將數據插入到MySQL中AJAX

任何人都可以幫我解決我的問題嗎?

PHP代碼:

<?php 
class Crud{ 

private $host="localhost"; 
private $username="root"; 
private $password=""; 
private $db_name="comment-system"; 
public $mysqli; 

public $data; 

public function __construct(){ 

    $this->mysqli = new mysqli($this->host, $this->username, $this->password, $this->db_name); 

    if(mysqli_connect_errno()) { 

     echo "Error: Could not connect to database."; 

    exit; 

    } 
    /*else{ 
     echo"Your Database successfully connected"; 
    }*/ 

} 

public function __destruct(){ 
    $this->mysqli->close(); 
} 

public function read(){ 

    $query="SELECT * FROM test"; 

    $result= $this->mysqli->query($query); 

    $num_result=$result->num_rows; 


    if($num_result>0){ 
     while($rows=$result->fetch_assoc()){ 

      $this->data[]=$rows; 

      //print_r($rows); 

     } 

     return $this->data; 
    } 
} 

public function insert($name){ 

$query="INSERT INTO post SET post='$name'"; 

    $result= $this->mysqli->query($query) or die(mysqli_connect_errno()."Data cannot inserted"); 

    if($result){ 
     header('location:index.php'); 
    } 
} 
} 

    //$obj=new Crud("localhost","root","","oop_crud"); 

//$obj->read(); 
?> 

HTML代碼

<script src="jquery-1.8.3.js"></script> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<html> 
<head> 
<title></title> 
<meta name="" content=""> 
</head> 
<body> 
<div id="maindiv"> 
<h3>post Detail:</h3> 
<h3><textarea id="txtarea" >my name is khan</textarea></h3> 
<h3><button id="save" title="post">post</button></h3> 

</div> 
</body> 

jQuery的& AJAX

<script> 
$("#save").click(function(){ 
    var name =$("#txtarea").val(); 
    $.ajax({ 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
        url: "Crud/insert", 
      data: "{'name':'" + name + "'}", 
       async: false, 
       success: function (responseText) { 
       alert(name); 
      } 
      }); 
}); 
</script> 

</html> 
+0

將alert(name)更改爲alert(responseText);這會給出什麼結果? – Alex

+0

將腳本放在腳本的某處以便檢查腳本是否正在運行,並且在firefox中還有螢火蟲,以便您可以檢查控制檯,一個D也「插入後('後')值('」。$ name。「');」 –

回答

-1

你的問題是INSERT SQL的語法。

請通過$ name = $ _POST [「name」]獲取名稱的值;

它應該工作:

$query="INSERT INTO post (`post`) VALUES ('$name')"; 

請檢查SQL插入語句的詳細信息: INSERT SQL SYNTAX

+0

我的代碼工作正常,插入時,我發送值,但沒有通過AJAX工作 –

0

我相信,數據庫操作是工作的罰款你的PHP代碼。這裏是HTML和AJAX代碼,

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<html> 
<head> 
    <title></title> 
    <meta name="" content=""> 
</head> 
<body> 
    <div id="maindiv"> 
     <h3>post Detail:</h3> 
     <h3><textarea id="txtarea" >my name is khan</textarea></h3> 
     <h3><button id="save" title="post">post</button></h3> 

    </div> 

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
    <script> 
    $(document).ready(function(){ 
     $(document).on('click','#save',function(){ 
      var name = $("#txtarea").val(); 
      var param = {name: name}; 

      $.ajax({ 
       type: 'POST', 
       url: 'ajax_page.php', 
       cache: 'false', 
       data: param, 

       beforeSend: function(){ 
        // function to perform before sending data 
       }, 

       success: function(data){ 
        alert(data); 
       }, 

       error: function(){ 
        // function to perform if unexpected error occurs 
       } 
      }); 
     }); 
    }); 
    </script> 
</body> 
</html> 

Ajax代碼發送數據/參數ajax_page.php頁面,您可以使用$ _ POST趕上參數[「名」],做你的數據庫操作。

希望這會幫助你。

相關問題