No errors, but nothing works! Help!

Hello! I am trying to use post data to make decisions about what data to insert into my sql database and what products to reject, and inform the user. I do not get any syntax errors, or a 500 error. All i get is a blank html page and no change in the database. I am not sure how to troubleshoot this, but I am pretty sure I am doing something fundamentally wrong.
Please let me know if you need more info.
Thanks.

<?php
    $db_host = "host";
    $db_username = "username";
    $db_pass = "pass";
    $db_name = "name";
    $pdo = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $sql = "INSERT INTO product
        SELECT new_prod_id, name, company, image FROM add_new_product WHERE new_prod_ID = '".$key."'
        DELETE FROM add_new_product WHERE id = '".$key."';";
    $stmt = $pdo->prepare($sql);

    print_r($_POST);
        foreach($_POST as $data)
        $key = key($data);
        echo $key;
        $user_ID = "select user_ID from add_new_product where new_prod_ID ='".$key."' ";
        echo $user_ID;
        $username = "select username from users where usersID ='".$user_ID."' ";
        echo $username;
        $user_email = "select email from users where usersID = '".$user_ID."' ";
        echo $user_email;

        if ($data == "1"){
        $stmt->execute();
        echo "Data was 1";
        }
        else{
            $msg = "Hello '".$username."', you recently submitted a product to 4UM. We regret to inform you that your product was not approved, likely because of insufficent proof uploaded \nIf this was not accurate,
            please resubmit your product with improved proof so we may approve it. \nThank you for using 4UM!";
            $msg = wordwrap($msg,70);
            mail($user_email, "4UM New Product Rejection",$msg);
        }


    ?>

Well, first, is this file saved as a .php file? If you saved it by accident as a HTML or HTM page, it will not execute the PHP sections.

This section is badly formed. Normally, you want to use braces to insure a for or foreach handles the data correctly… Loosely like:
foreach($variable as $key=>$data) {
// handle each key/data combo as needed…
}
Your foreach will only get one line of data from $_POST… It does not continue to item#2.
This might be the issue.

Hi,
Is this what you meant? I am not super sure I understood your comment. Also yes, it is saved as php.
Attached is a screenshot of what I was getting outputted before I made some changes to add flag variables. Screenshot 2021-04-14 224501
$data = $_POST
foreach($data as $key=>$row)

Well,

So, first, if you want to display what is inside the POST array, you should do it this way:
echo “< pre>” . print_r($_POST, 1) . “< /pre>”; Or use a var_dump function. This will show you the array and how it is laid out in a better format. ( In my humble opinion! )
And, a foreach( …) without braces will only process the next line and continue on. Is that what you intended? If so, ignore the previous post. If you want to go through all of the POST data, you need to use braces to rerun all the lines for each item in POST. Hense FOREACH…

So with all that said, your example results is $key=21 and $data=1

Therefore, this will be looking for a new_prod_ID of 21 in the table add_new_product…
Is that what you want to do? You have no error checking of any type in the code. Normally, if you select data from the database, you check if there are any results found.

Perhaps we should go back to the beginning. You have users. They enter new products of some sort. In your form, which we can not see, it has some fields to get user data from. The which is a new_prod_ID. You pull the username and email from somewhere else. So, guessing that you just want to loop thru the posted new_prod_ID’s and add them to your msg. I am guessing this because you showed us the list of 21,25,26 with 1,1,0 as values. So, to handle this is impossible if it is a yes or no answer. You would have to do it in another way. Loosely something like this would work. Listing all inputs and the decisions made. (Or, send two emails one with these were excepted and these were not.) Easier to just show them all unless it is a very long list.
$msg="";
foreach($_post as $key=>$data) {
if($data==0) {
$msg .= “New Product#” . $key . " was NOT accepted.
";
} else {
$msg .= “Product#” . $key . " was accepted!
";
}
So, this creates a list of the yes/no options. You could split it into two lists one accepted and one not. It is set up for HTML emails. If you use TEXT emails, you would replace the
's with line-feeds instead!
Note that use of period-= to concatenate the lines one after the other inside the $msg string…
Is that what you need?

Hey ErnieAlex, first of all thank you for the long detailed post. I fixed the braces error but that doesn’t seem to be the issue. The reason I am not checking for the existence of the products is because that has already been handled on a previous page. The form is

<form action="/~yalevy/addition.php" method="POST" name="approval">
          <h2>
              <span>Products to be Evaluated</span>
          </h2>

          <table class="table">

              <tr>
                  <th>Product Id</th>
                  <th>User Id</th>
                  <th>Name</th>
                  <th>Company</th>
                  <th>Image</th>
                  <th>Confirm/Reject</th>
              </tr>

              <?php
		while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
			echo '<tr>';
			echo '<td>' . $row['new_prod_ID'] . '</td>';
			echo '<td>' . $row['user_ID'] . '</td>';
			echo '<td>' . $row['name'] . '</td>';
			echo '<td>' . $row['company'] . '</td>';
			echo "<td><img src=\"https://cgi.sice.indiana.edu/~team48/team-48/uploads/".$row['image']."\" width='300'></td>";
			echo '<td><input type="radio" id="' . $row['new_prod_ID'] . '"  name="' . $row['new_prod_ID'] . '" value="1"><label for="confirm"> Confirm</label><br><br><br><input type="radio" id="' . $row['new_prod_ID'] . '" name="' . $row['new_prod_ID'] . '" value="0"> <label for="reject"> Reject</label> </td>';
			echo '</tr>';
		}
              ?>
          </table>
          <input type="submit" />
      </form>

The idea is that the id is the id in the table of the product, and the value is 1 if the person has checked the radiobutton for confirm, or 0 if they checked deny. That way if it was confirmed, the statement should run to insert the item to the product table, and remove it from the add_product table. If it is denied, an email is sent. I am getting an error 500 in my console however, and no echo statements seem to be running. Still no syntax error.
Maybe this helps you help me…haha.

Where does the FORM post to? I mean, where is the ACTION clause? The same page? Or, a new one?
So, if a new one, you would need to have a check to see if the page was posted to. Like this:
if($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {
And, everything that has to do with the posted data would go between the braces…
Next, you need to look thru the posted values as we discussed before with a FOREACH loop.
The for-each would load each input field and add it to the report as we also discussed before.
Then, of course, send the email with report in it.

Now, Error 500 is almost always that you sent it to wrong page, sent too much data or one of many other errors. You can add these lines to the top of the sending (posting) page and the report page and they will force errors to be displayed. You can view the errors either on the screen or in the server logs.

error_reporting(E_ALL);
ini_set(“display_errors”, 1);

This basically turn on all errors and make them displayed on the browser depending on what kind of errors occur. Some errors must be viewed on the server. Hope all this helps

Sponsor our Newsletter | Privacy Policy | Terms of Service