Kiciana Posted May 11, 2014 Share Posted May 11, 2014 Hi I'm trying to post and validate a form but it doesn't seem to work and it doean't display any errors Here's the code <?php include("config.php"); session_start(); ?> <?php // define variables and initialize with empty values $rateErr = $comErr = ""; $rating = $comment = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if ($_POST["rating"] == "") { $rateErr = "Rate the app"; } else { $rating= $_POST["rating"]; } if (empty($_POST["comment"])) { $comErr = "Missing"; } else { $comment = $_POST["comment"]; } if ($rateErr == "" && $comErr == "") { try { $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $sql = "INSERT INTO reviews (rating, content, appID, user) VALUES(:rating, :comment, :appID, :username)"; $stmt = $con->prepare( $sql ); $stmt->bindValue( ":rating", $rating); $stmt->bindValue( ":comment", $comment); $stmt->bindValue( ":appID", $_GET['id']); $stmt->bindValue( ":username", $_SESSION['username']); $stmt->execute(); return "Submitted successfully"; }catch( PDOException $e ) { return $e->getMessage(); } } } ?> <html> <head> </head> <body> <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <select name="rating"> <option value=""></option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> <span class="error"><?php echo $rateErr;?></span> <br /> <textarea rows="4" cols="50" name="comment" value="<?php echo htmlspecialchars($comment);?>"> Enter text here...</textarea> <span class="error"><?php echo $comErr;?></span> <input type="submit" name="submit" value="Submit"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/288404-doesnt-validate-or-insert-data-into-database-with-a-form/ Share on other sites More sharing options...
Jacques1 Posted May 11, 2014 Share Posted May 11, 2014 Unfortunately, “doesn't work” doesn't work as a problem description. Have you thought about trying to actually find out where things go wrong? You know, using an echo to see which parts of the code get executed, using var_dump() to inspect variables? The reason why you're not getting any error messages is because you're doing everything to suppress them: You catch every single exception that might occur and replace them with a plain return (I guess you actually meant echo). This is something I just don't get: Why do people turn on exceptions, then clutter their code with try-catch blocks to immediately catch all those exceptions and throw them away and finally complain about not getting errors? What's the idea behind this? Quote Link to comment https://forums.phpfreaks.com/topic/288404-doesnt-validate-or-insert-data-into-database-with-a-form/#findComment-1479076 Share on other sites More sharing options...
Kiciana Posted May 11, 2014 Author Share Posted May 11, 2014 (edited) ok sorry, thought it would return the error and display it, it's a help thread so thx for pointing that out. So appareantly the problem is the $_GET parameter that i pass, error: Integrity constraint violation: 1048 Column 'appID' cannot be null I assume the parameter i pass in URL disappears after submission? At least it looks so, it's passed just fine and after I click submit button I get that error and there is no id in the URL Edited May 11, 2014 by Kiciana Quote Link to comment https://forums.phpfreaks.com/topic/288404-doesnt-validate-or-insert-data-into-database-with-a-form/#findComment-1479078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.