0

when I echo the php variable it work properly , but when I try to insert the data into database it doesn't work , what is the solution please I get stuck

I got this error on console

  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 Answers 4

1

Please add the details of the error message you get.

Make little changes to your code so that it can show the query error if any

<?php 

include 'mysqldb.php';


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

$sql = "INSERT INTO `uss` (`first`, `last`) VALUES('{$name}','{$age}')";
if($conn->query($sql))
{
  echo "Record inserted";
}
else
{
 echo $conn->error;
}
?>

Sugesstions: Your query have the chances of the SQL Injection. Make it secure.

Sign up to request clarification or add additional context in comments.

1 Comment

I've edit the post I put the error from the console , have a look thanks
0

if you are using ajax , try the following,

<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);
 }
});
});

in your controller function,echo the result

<?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;
?>

Comments

0

jQuery Ajax

Form with 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 section

$(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 ;

?>

Comments

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 ;

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.