not inserting into database

Hi

I’m trying out submission form and after clicking submit button the data is not inserted and the same page shows up with no errors. I assume that everything after if ($rateErr && $comErr && $catErr&& $linkErr && $priceErr == “”) is not executed. If i print the POST variables it shows all the proper array. Here’s the code:
[php]

<?php // define variables and initialize with empty values $nameErr = $comErr = $catErr =$priceErr =$linkErr =""; $name = $description = $category = $price = $link = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { //print_r($_POST); if ($_POST["name"] == "") { $nameErr = "Name the app"; } else { $name= $_POST["name"]; } if ($_POST["price"] == "") { $priceErr = "Price the app"; } else { $price= $_POST["price"]; } if ($_POST["link"] == "") { $linkErr = "Link the app"; } else { $link= $_POST["link"]; } if ($_POST["category"] == "") { $catErr = "Missing"; } else { $category = $_POST["category"]; } if (empty($_POST["description"])) { $comErr = "Missing"; } else { $description= $_POST["description"]; } if ($rateErr && $comErr && $catErr&& $linkErr && $priceErr == "") { try { $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $sql = "INSERT INTO apps (app_name, category, price, link, description, date_added) VALUES (:name, :category, :price, :link, :description, :date)"; $stmt = $con->prepare( $sql ); $stmt->bindValue( ":name", $name); $stmt->bindValue( ":category", $category); $stmt->bindValue( ":price", $price); $stmt->bindValue( ":link", $link); $stmt->bindValue( ":description", $description); $stmt->bindValue( ":date", now()); $stmt->execute(); echo "Submitted successfully"; }catch( PDOException $e ) { echo $e->getMessage(); } } } ?>

[/php]

Why Assume?

Test it…

if ($rateErr && $comErr && $catErr&& $linkErr && $priceErr == “”) {
echo ‘Got Here’;
exit;

I also think this is wrong…

[php] if ($rateErr && $comErr && $catErr&& $linkErr && $priceErr == “”) {[/php]

I’m not really a php coder, but I read that statement as…

if (true and true and true and true and true and $pricerr == “”)

Shouldn’t it be…

[php]if (($rateErr == “”) && ($comErr =="") && ($catErr == “”) && ($linkErr=="") && ($priceErr == “”)) {
[/php]

yeah, right. I changed the statement
It prints the message fine but it still means the try {} catch{} are nor executed
Is it smth with the query then?

Did you test to see if it entered your try block?

Not wanting to step on Topcoder’s toes, but, another test to try is to just print your query.
You create the query into variable $sql in line 45.

Create a temporary line at 46 which says: die($sql);

Then run the page and see what you are actually creating for the query.
You might see something very simple and verify it by checking your database tables for the data
you queried against. I normally create my errors list in a different manner.

I check each test and set the error message into a text variable. If the variable makes it as “” then there are no errors and then I process the query. One simple check, not several.

Good luck…

Sponsor our Newsletter | Privacy Policy | Terms of Service