Jump to content

doesn't validate or insert data into database with a form


Kiciana

Recommended Posts

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>
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by Kiciana
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.