0

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?

  • 1
    You check if `$_POST['txt_name'] != ''`, but you never send any `txt_name` in your request. Double check your code... – Dekel Nov 04 '16 at 13:42
  • You are open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of concatenating your queries.`mysqli_real_escape_string()` is not as secure as one would hope. – M. Eriksson Nov 04 '16 at 13:45
  • 1
    You are mixing `\`` and `'` around your column names.. **Example** _(your insert query):_ `'recipe_photo', \`created_at\`,...`. Only use back ticks `\`` around your column names. – M. Eriksson Nov 04 '16 at 13:48
  • You should also check if your queries were successful and log potential errors. It will help you debug your code more easily. – M. Eriksson Nov 04 '16 at 13:54
  • Have updated the code for quotes and changed to `` for all columns @Magnus, still i get no data posted to table – user7115281 Nov 04 '16 at 15:12
  • Have updated txt_name in $.post in index.php @Dekel – user7115281 Nov 04 '16 at 15:13
  • Update the question and the relevant data and make sure you keep **only** what is required for your question. – Dekel Nov 04 '16 at 15:14
  • [try reading this question](http://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-in-different-environments) – Martin Nov 04 '16 at 15:24
  • Then it's time to check your error log... – M. Eriksson Nov 04 '16 at 16:24
  • no error logs, one thing confusing me is if i try the same with dummy table with just 2 columns, data saves, but in this table its not saving...how to find out other root causes or logs – user7115281 Nov 04 '16 at 16:53
  • Ok got the solution, it was the Fk constraint issue, its matching table dint had the value ...Thanks guys – user7115281 Nov 05 '16 at 05:55

0 Answers0