如何使用PHP5将数据插入到MySQL中AJAX

问题描述:

我是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。“');” –

你的问题是INSERT SQL的语法。

请通过$ name = $ _POST [“name”]获取名称的值;

它应该工作:

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

请检查SQL插入语句的详细信息: INSERT SQL SYNTAX

+0

我的代码工作正常,插入时,我发送值,但没有通过AJAX工作 –

我相信,数据库操作是工作的罚款你的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赶上参数[“名”],做你的数据库操作。

希望这会帮助你。