I'm trying to make a submission form. It returns errors if fields are empty but doesn't insert the data into table and after submission just reloads the page with header only. I don't know what's wrong as no errors are echoed
My validation
<?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 (empty($rateErr) && empty($comErr) && empty($catErr)&& empty($linkErr) && empty($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();
}
}
}
?>
And the form
<form method="POST"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
App name <input type="text" name="name" value="<?php echo htmlspecialchars($name);?>">
<span class="error"><?php echo $nameErr;?></span>
<br />
Price <input type="text" name="price" value="<?php echo htmlspecialchars($price);?>">
<span class="error"><?php echo $priceErr;?></span>
<br />
Download link <input type="text" name="link" value="<?php echo htmlspecialchars($link);?>">
<span class="error"><?php echo $linkErr;?></span>
<br />
<select name="category">
<option value=""></option>
<option value="maths">maths</option>
<option value="driving">driving</option>
<option value="languages">languages</option>
<option value="literature">literature</option>
<option value="science">science</option>
<option value="psychology">psychology</option>
<option value="psychology">biology</option>
<option value="IT">IT</option>
<option value="other">Other</option>
</select>
<span class="error"><?php echo $catErr;?></span>
<br />
<textarea rows="4" cols="50" name="description" value="<?php echo htmlspecialchars($description);?>">
Enter text here...</textarea>
<span class="error"><?php echo $comErr;?></span>
<input type="submit" name="submit" value="Submit">
</form>
You are not echoing any error. You are returning "Submitted successfully" but you are not inside a function. You should place echo "Submitted successfully" if you want to see this message.
it seems your code is not going through $_SERVER["REQUEST_METHOD"].
Try using if(isset($_POST)) instead and print out $_POST with print_r($_POST), just to verify your form is posting data correctly.
check you database connection code
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
replace with the below one,it is working and values get inserted...may be this might also be a problem
$con = new PDO('mysql:host=localhost;dbname=test', $user, $pass);