Reputation: 275
Hello I am having a problem
I am sending a javascript variable to my php script and attemping to store that variable in mysql db but it just does not work.
Here is my code:
js:
<script type="text/javascript">
var myData = "Hello";
function AJAXAction () {
$.ajax({
url: 'test.php',
data: { myPhpData: myData },
success: function (response) {
alert (response);
}
});
}
AJAXAction();
</script>
PHP:
<?php
$link = mysqli_connect("localhost","root","","testt") or die("Error " . mysqli_error($link));
function goDoIt ($link) {
$why = $_GET['myPhpData'];
$sql = "INSERT INTO test_table (id) VALUES '$why'";
mysqli_query($link, $sql);
echo "booooom";
}
goDoIt ($link);
mysqli_close($link);
?>
The result alerts "boooom" but it does not store my $why variable in my table
Upvotes: 0
Views: 112
Reputation: 2783
$sql = "INSERT INTO test_table (id) VALUES ('".$why."');
you can also do this
Upvotes: 1
Reputation: 179
Try it:
$why = $_GET['myPhpData'];
$sql = "INSERT INTO test_table (id) VALUES '$why'";
if(mysqli_query($link, $sql)){
echo "booooom";
}else{
echo "error";
}
Then you can get if the query is correct or not.
Upvotes: 2
Reputation: 3101
Variable should be enclosed in {} plus you need to enclose it in ()
$sql = "INSERT INTO test_table (id) VALUES ('{$why}')";
Upvotes: 1