1

I'm relatively new to coding.

Here are a couple links I've used:

What I'm trying to do is load a html table once an option is selected in the form. Using ajax append the value of the option to the 'formscript.php?n=' URL. Then use that value to load the correct rows matching that value.

When the table is being populated in a while loop I've put some if statements in for when I want the queries to be executed; the problem is that it will execute the $uplift but won't execute the $delete_outdated and neither will it populate the table.

How can I get the $uplift and $delete_outdated to execute and populate the table? Or is there a better way to do this correctly?

php script:

$area = $_GET['a'];
$stmt = $pdo->prepare("SELECT * FROM stage1 WHERE area = :area");
$stmt->bindParam(':area', $area, PDO::PARAM_STR);
$stmt->execute();

$sql1 = "INSERT INTO stage2 SELECT * FROM stage1 WHERE target_date < CURRENT_DATE(); AND id = :id";
$uplift  = $pdo->prepare($sql1);
$uplift->bindParam(':id', $id, PDO::PARAM_STR);

$sql2 = "DELETE FROM stage1 WHERE target_date < CURRENT_DATE() AND id = :id";
$delete_outdated = $pdo->prepare($sql2);
$delete_outdated->bindParam(':id', $id, PDO::PARAM_STR);

$sql3 = "INSERT INTO originator SELECT * FROM stage1 WHERE completion=100;";
$signoff = $pdo->prepare($sql3);

$sql4 = "DELETE FROM stage1 WHERE completion=100; ";
$delete_completions = $pdo->prepare($sql4);

echo "<table'><thead><tr><th scope='col'>ID</th><th scope='col'>Area</th><th scope='col'>Target Date</th><th scope='col'>Completion</th></tr></thead><tbody>";


while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
$target_date = $row['target_date'];
$id = $row['id'];

if($target_date < date('Y-m-d')){

    $uplift->execute();
  //   $uplift->debugDumpParams();

    if($affected1 = $uplift->rowCount() == 1){
        $delete_outdated->execute();
      //   $delete_outdated->debugDumpParams();
    };
};

if($row['completion'] == 100){

    $signoff->execute();

    if($affected2 = $signoff->rowCount() == 1){
        $delete_completions->execute();
    };
};

echo "<tr>";
echo "<th scope='row'>" . $row['id'] . "</td>";
echo "<td>" . $row['area'] . "</td>";
echo "<td>" . $row['target_date'] . "</td>";
echo "<td>" . $row['completion'] . "%</td>";
echo "</tr>";
 };

echo "</tbody></table>";
halfer
  • 19,824
  • 17
  • 99
  • 186
cookies
  • 67
  • 9
  • 2
    `target_date < CURRENT_DATE(); AND id = :id` - remove the semi-colon after `CURRENT_DATE()`. You should test the return value of `prepare` to ensure the statement object has been created ( it returns a boolean so you can do `if($stmt){}else{}` etc ) – Professor Abronsius Apr 05 '18 at 07:01
  • Is this your real code? It would seem that `$sql1` is not valid sql so I don't see how the first sql gets executed corrrectly. – jeroen Apr 05 '18 at 07:02
  • 2
    Another thing, it seems that all your tables have exactly the same structure. Instead of using 3 different tables, you should probably use only 1 and add a status column or something similar. – jeroen Apr 05 '18 at 07:06
  • @RamRaider Thank you, the ; semi-colon was preventing the table from being populated. Each statement object is being created, using the if(){}else{}. – cookies Apr 05 '18 at 07:20
  • 2
    @jeroen Thank very much for your suggestion, all works well now, much appreciated!!! (I changed from having 3 different tables to one with a status column, this made it unnecessary to have $sql1 etc). – cookies Apr 05 '18 at 11:29
  • That's good to hear! – jeroen Apr 05 '18 at 11:29

0 Answers0