I'm trying to make a form that adds information to my MySQL database. To do that, I have four scripts:
<html>
<head><title>Insert Data Into MySQL: jQuery + AJAX + PHP</title></head>
<body>
<form id="myForm" action="user_info.php" method="post">
User_id: <input type="text" name="user_id" /><br />
Hash : <input type="text" name="hash" /><br />
<button id="sub">Save</button>
</form>
<span id="result"></span>
<script src="jquery-2.1.1.min.js" type="text/javascript"></script>
<script src="jcode.js" type="text/javascript"></script>
</body>
</html>
That's my main page, it mentions two other files, which are user_info.php and jcode.js (ignore the jQuery piece). This is user_info.php
<?php
include_once('db.php');
$user_id = $_POST['user_id'];
$hash = $_POST['hash'];
if(mysqli_query("INSERT INTO _test('user_id, 'hash') VALUES('$user_id', '$hash')"))
echo "Successfully Inserted";
else
echo "Insertion Failed";
?>
It points to db.php, so here's that:
<?php
$con=mysqli_connect(" **taken out** ","root","password","test");
?>
and finally, here's jcode.js:
$("#sub").click( function() {
$.post( $("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(info){ $("#result").html(info);
});
clearInput();
});
$("#myForm").submit( function() {
return false;
});
function clearInput() {
$("#myForm :input").each( function() {
$(this).val('');
});
}
For a while, it would say that the insertion was successful. But strangely,it would input blank rows in the database. I don't know what needs to be changed but now it just says "Insertion Failed". I'm fairly new to this technology, so I'm probably missing something obvious.