0

I have a radio button-form which sends post requests with ajax. Post data should be inserted into two different tables.

My problem is that my $ID variable always work with table1, but only say 3 out of 10 attempts on table2.

Ajax code:

post_radio();

function post_radio() {
  $('.ok').change(function() {
    var current_element = $(this);
    var id = $(this).attr('id');
    var result = $("input[name='result']:checked").val();
    var table_id = "<?php echo $table_id?>";
    $.post('posta.php', {
      "ID": id,
      "result": result,
      "table_ID": table_id
    }, function(data) {
      $('.total_status').html(data);
    });
  });
}

Post PHP script:

$result = $_POST['result'];
$ID = $_POST['ID'];
$table_ID= $_POST['table_ID'];

$sql="UPDATE table1 
      SET result='$result', touched='yes' timestamp=now() 
      WHERE ID='$ID'";
mysql_query($sql, $dbconnection);

$sql2="SELECT * 
       FROM table1 
       WHERE table_ID='$table_ID' 
       AND touched='yes'";

$result2 = mysql_query($sql2, $dbconnection);
$rows = mysql_num_rows($result2);
$complete = ROUND(($rows/189)*100,0);

$sql3="UPDATE table2 
       SET complete='$complete', last_ID='$ID' 
       WHERE table_ID='$table_ID'";

mysql_query($sql3, $dbconnection);

$sql works every time

$sql2 works every time ($complete correctly calculated)

$sql3 works every time (complete column is correctly filled with $complete)

Problem is that column "last_ID" should be filled with $ID, but this seem to only work randomly

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • Do you get any error in the logs when the failure occurs? – Jay Blanchard May 08 '17 at 12:42
  • 3
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 08 '17 at 12:43
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard May 08 '17 at 12:43
  • [Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server?](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard May 08 '17 at 12:43
  • `last_ID` is filled where `table_ID=$table_id` – Edwin May 08 '17 at 12:47

0 Answers0