I am trying to insert data in mysql table using Jquery ajax. My jquery codes is as follows.
$("#join-form-submit").on("click", function(){
if($("#join-fullname").val() && $("#join-username").val() && $("#join-email").val() && $("#join-phone").val() && $("#join-location").val()){
var formData = new FormData();
var fullname = $("#join-fullname").val();
formData.append('fullname', fullname);
var username = $("#join-username").val();
formData.append('username', username);
var email = $("#join-email").val();
formData.append('email', email);
var phone = $("#join-phone").val();
formData.append('phone', phone);
var location = $("#join-location").val();
formData.append('location', location);
$.ajax({
url : 'new_sales.php',
type : 'POST',
data : formData,
contentType : false,
processData : false,
success : function(data){
alert("Success");
console.log(fullname);
console.log(username);
console.log(email);
console.log(phone);
console.log(location);
}
});
} else {
$(".join-form-error").slideDown().delay(3000).slideUp();
}
});
This part is working fine and I am getting my input data in console.log after clicking the button but the data is not inserted in mysql server.
the code at new_sales.php is as follows
<?php
$fullname = $_POST["fullname"];
$user_name = $_POST["username"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$location = $_POST["location"];
$conn = new mysqli("localhost", "zzz", "xxxx", "wedoinst_main");
$sql = "INSERT INTO new_sales (fullname, user_name, email, phone, location) VALUES ('$fullname', '$user_name', '$email', '$phone', '$location')";
?>
Please point out the mistake I am doing here.
Thanks
You need to execute the query.
$conn->query($sql);
Also you need to use prepared statements to prevent sql injection
$sql = "INSERT INTO new_sales (fullname, user_name, email, phone, location) VALUES (?,?,?,?,?)";
$result = $conn->prepare($sql);
$result->bind_param('sssss',$fullname,
$user_name,$email, $phone, $location);
echo $result->execute() === true ? 'success' : 'query failed '. $conn->error;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments