I'm trying to post some values to backend table on regular intervals, but data is not getting saved to table.
Here is how I'm trying to save:
index.php
<script>
// timer code starts here ---
//var init2 = 50;
var s;
function timePicker(vr) {
if (vr > 0)
{
if (vr > 1) {
$('#timer').html('');
} else {
$('#timer').html('');
}
vr--;
s = setTimeout('timePicker(' + vr + ')', 1000);
} else {
clearInterval(s);
$.post('data.php',{txt_name:$('#rec_name').val(),txt_title:$('#rec_title').val()},function(r){
$('#upd_div').html("Last Updated: "+r);
$('#timer').html('Recipe Contents Saved.. ');
s = setTimeout('timePicker(' + 10 + ')', 5000);
return false;
});
}
}
</script>
<div class="content">
<div class="form-group2">
<label>Rec Name:</label>
<input type="text" name="rec_name" id="rec_name" class="form-group2" />
<br/><br/>
<div class="form-group">
<label>Rec Title:</label>
<input type="text" name="rec_title" id="rec_title" class="form-group2" />
data.php
<?php
$con=mysqli_connect("localhost","root","passwrd","DBname");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['txt_name']) && $_POST['txt_name'] != '') {
$user_id=2;
$recipe_name=mysqli_real_escape_string($con,$_POST['txt_name']);
$recipe_title=mysqli_real_escape_string($con,$_POST['txt_title']); print_r($recipe_title);
$recipe_type=3;
$cooking_time=20;
$preparation_time=20;
$serving_to=5;
$recipe_desc="test desc";
$recipe_photo='photo1';
$published_at=date('Y-m-d H:i:s');
$qry = "Select count(*) as cnt from spen_recipe";
$res = mysqli_query($con,$qry);
$row = mysqli_fetch_array($res);
$date_last_modified = date('Y-m-d H:i:s');
if ($row['cnt'] == 0) {
$qry_ins = "INSERT INTO `spen_recipe` (`user_id`,`recipe_id`,`recipe_name`,`recipe_title`,`recipe_type`,`cooking_time`,`preparation_time`,`serving_to`,`recipe_desc`,`recipe_photo`,`created_at`,`modified_at`,`published_at`)VALUES('1','" . $user_id . "','" . $recipe_name . "','" . $recipe_title . "','" . $recipe_type . "','" . $cooking_time . "','" . $preparation_time . "','" . $serving_to . "','" . $recipe_desc . "','" . $recipe_photo . "','" . date('Y-m-d H:i:s') . "','" . $date_last_modified . "','" . $published_at . "');";
mysqli_query($con,$qry_ins);
} else {
$qry_upd = "Update `spen_recipe` set `user_id`='" . $user_id . "', `recipe_name`='" . $recipe_name . "',`recipe_title`='" . $recipe_title . "',`recipe_type`='" . $recipe_type . "',`cooking_time`='" . $cooking_time . "',`preparation_time`='" . $preparation_time . "',`serving_to`='" . $serving_to . "',`recipe_desc`='" . $recipe_desc . "',`recipe_photo`='" . $recipe_photo . "',`modified_at` ='" . $date_last_modified . "',`published_at` ='" . $published_at . "' where recipe_id=1";
mysqli_query($con,$qry_upd);
}
echo $date_last_modified;
}
?>
</div>
<br/><br/>
<br/><br/>
<div id="timer_upd_dev">
<div id="timer">x Secs </div>
<div id="upd_div"> </div>
</div>
</div>
Why is my data not getting saved to table, I tried only with 2 column in other dummy table, data gets saved properly, but why no tin this above query??
Is it because I have Not NULL set to column and I'm trying to insert only recipe_name value?