What's the problem with data that doesn't go into database؟

Where do I put this, bro

echo "C is $c";

You can explain this line more?

“before your prepare”
echo "C is $c";

The “prepare” is referring to the PDO function. The $c is referring to the variable. It is implied that for testing purposes the echo should be temporarily put before the prepare(s) that use the $c variable so that you can see what the value of that variable is at that point in the code flow.

i.e. If the value is not what you expect it to be, a problem exists prior to that bit of code. If it looks OK then there is likely a problem with the PDO code.

1 Like

i did it

Notice: Undefined variable: c in C:\xampp\htdocs\rt\new_result.php on line 65
C is

I’m assuming then that the next step would be to go back to this bit of code

if (!empty($stm)) {

   $result_id=@output($_POST['result_id']);
   $c=@output($_POST['c']);
… 

If so, the next thing you should look at is why “output” is not assigning the value of $_POST['c'] to the $c variable.

Perhaps an easy way would be to temporarily remove the error suppressor (the @) so you can see the error.

1 Like

Did we get an answer about what the browser network diagnostics shows the form data to be?

There’s no point trying to get results from PHP if the data isn’t there, and if the data is there then there’s no point in fiddling with the JavaScript stuff.

Gaining an understanding first about the information that’s passed from the form to the PHP code is a vital first practice, that seems to have been skipped over.

1 Like

The problem is in this line javascript code, not php code

  <option value="<?php echo $row['id'] ?>" data-json='<?php echo json_encode($row) ?>'><?php echo $row['subject_code'].' | '.ucwords($row['subject']) ?></option>

2021-02-22_115906

Well not quite, I just went about it a different way. But, fair enough. More information please OP! (More information is always good)

When I click insert button, the data goes correctly.
look at this


ok
But at this time, click the Add button, the problem that’s happening

78

because these two cells go into the database empty, I think that’s the problem.

In this bit of code:

if (!empty($stm)) {

  $result_id=@output($_POST['result_id']);
  $c=@output($_POST['c']);
  $mark=@output($_POST['mark']);
  $result_id = $db->lastInsertId();
  //$id = $db->insert_id;
  $stm2 = $db->prepare("INSERT INTO result_items (id,result_id,subject_id,mark) VALUES (NULL,:result_id,:c,:mark);");
  //$stm2->bindParam(":id", $id, PDO::PARAM_STR);
  $stm2->bindParam(":result_id", $result_id, PDO::PARAM_STR);
  $stm2->bindParam(":c", $c, PDO::PARAM_STR);
  $stm2->bindParam(":mark", $mark, PDO::PARAM_STR);
  //$stm2->execute();
  if(!$stm2->execute()){ echo "errrr2"; }
  }

you don’t provide a date_created value, but in your table definition in post #20 it clearly shows that column cannot be null, and has no default value. Won’t that cause a problem? If it’s always a date, you’d be better off using a DATE column type as it will make it much easier to select rows by date range, or ORDER BY the date, if the database knows it is a date.

Also, where you are inserting values and you have an id column which is auto-increment, there’s no need to name it in the query and pass NULL to it, you can just leave it out and it will apply the default.

Thank for your help , it’s not yet resolved. I have to find another way.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.