当我尝试将数据插入到数据库

时间:2017-03-09 03:45:59

标签: php jquery ajax apache xmlhttprequest

当我回显php变量时它工作正常,但是当我尝试将数据插入数据库时​​它不起作用,解决方案是什么,我被卡住了

我在控制台上出现此错误

  POST http://localhost/validate.php 500 (Internal Server Error)

        send @ jquery-3.1.1.min.js:4
        ajax @ jquery-3.1.1.min.js:4
        (anonymous) @ jquery.PHP:26
        dispatch @ jquery-3.1.1.min.js:3
        q.handle @ jquery-3.1.1.min.js:3

HTML / JQUERY

<form action="" id="myForm">
    <input type="text" id="name" ><br/> 
    <input type="text" id="age" ><br/> 
    <input type="submit" value="Submit"> 
</form> 
<div id="result"></div>

   <script> 

   $(function() {
    $("#myForm").submit(function(e) {
        e.preventDefault();
        var name = $('#name').val(); 
        var age = $('#age').val();

        $.ajax({
        url: 'validate.php',
        method: 'POST',
        data: {postname:name, postage:age},
        success: function(res) {
            $("#result").append(res);
        } 
    });
    });
}); 
   </script>

PHP

 <?php 

include 'mysqldb.php';


$name = $_POST['postname']; 
$age =  $_POST['postage']; 


$sql = "insert into uss (first, last) values('".$name."','".$age."')";
$result = $conn->query($sql);

echo $result ;

?>

mysqldb.php

<?php

$conn = mysql_connect('localhost', 'root', 'password' , 'datab');

if (!$conn) {

    die("Connection failed: ".mysqli_connect_error());

}

?>

4 个答案:

答案 0 :(得分:2)

请添加您收到的错误消息的详细信息。

对您的代码进行少量更改,以便它可以显示查询错误(如果有)

React.render((
  <Router>
    <Route path="/" component={App} >
      <IndexRoute components={{main: Main, side: Side}}  />
    </Route>
  </Router>
), document.getElementById('app'))

Sugesstions:您的查询有机会进行SQL注入。确保安全。

答案 1 :(得分:0)

jQuery Ajax

  

ID为myFrom的表单

<form action="" id="myForm">
    <input type="text" id="name" ><br/> 
    <input type="text" id="age" ><br/> 
    <input type="submit" value="Submit"> 
</form> 
<div id="result"></div>
  

jQuery Ajax部分

$(function() {
    $("#myForm").submit(function(e) {
        e.preventDefault();
        var name = $('#name').val(); // getting name
        var age = $('#age').val();   // getting age 

        /* Ajax section */
        $.ajax({
        url: 'validate.php',
        method: 'POST',
        data: {postname:name, postage:age},
        success: function(res) {
            $("#result").append(res);
        } 
    });
    });
}); 
  

validate.php

<?php 

    include 'mysqldb.php';


    $name = $_POST['postname']; 
    $age =  $_POST['postage']; 

    //check ajax response by `echo $name` and `$age` 

    $sql = "insert into uss (first, last) values('".$name."','".$age."')";
    $result = $conn->query($sql);

    echo $result ;

?>

答案 2 :(得分:0)

如果您使用的是ajax,请尝试以下操作,

RewriteEngine on
RewriteRule !module /module%{REQUEST_URI} [L]
在控制器功能中

,回显结果

<form >
<input type="text" id="name" ><br/> 
<input type="text" id="age" ><br/> 
<input type="submit" value="Submit" id="submit"> 
</form> 
<div id="result"></div> 

 $("#submit").click(function(){
 var name = $('#name').val(); // getting name
 var age = $('#age').val();
 $.ajax({
 url : "validate.php",
 type: "POST",
 data: {name:name, age:age},
 success: function(data)
 {
 $("#result").html(data);
 }
});
});

答案 3 :(得分:0)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
 <form >
    <input type="text" id="name" ><br/> 
    <input type="text" id="age" ><br/> 
    <input type="button" value="Submit" onclick="postdata();"> 
    </form> 
    <div id="result"></div> 

    <script type="text/javascript"> 

    function postdata() { 
	alert("ashad");
    var name = $('#name').val(); 
    var age = $('#age').val();
    $.post('validate.php',{postname:name,postage:age},
    function(data){

    $('#result').html(data); 
         }); 
    } 
   </script>

   <?php 
	include 'mysqldb.php';
    $name = $_POST['postname']; 
    $age =  $_POST['postage']; 

    //check ajax response by `echo $name` and `$age` 
	if ($conn->connect_error) {
		die("Connection failed: " . $conn->connect_error);
	} else
	{
		$sql = "insert into uss(first, last) values('$name','$age')";
		$result = $conn->query($sql);
		
	}
    echo $result ;

?>