Currently, I am trying to create an "add" form on PHP for one of my assignments in order for users to insert data into a database that will be displayed in a table on page index.php. The issue is that when I click to submit the form, the page refreshes and ends up not adding any data to the database at all. There are also no errors displayed after submitting. I believe the database is connected as the users in the database do appear in the table in index.php however those were already added through phpMyAdmin itself. I am completely stuck as I've tried searching on google and still haven't found a possible solution for this issue. Below is my code for both index.php and add.php. I am still a beginner especially when it comes to PHP. Please do let me know if I need to provide any other additional code to fix this issue. Thank you in advance!
add.php
<?php
require_once "pdo.php";
session_start();
if ( isset($_POST['Par_Name']) && isset($_POST['Par_Email'])
&& isset($_POST['Par_Tel'])) {
// Data validation
if ( strlen($_POST['Par_Name']) < 1 || strlen($_POST['Par_Tel']) < 1 || strlen($_POST['Game_Num']) < 1) {
$_SESSION['error'] = 'Missing data';
header("Location: add.php");
return;
}
if ( strpos($_POST['Par_Email'],'@') === false ) {
$_SESSION['error'] = 'Bad data';
header("Location: add.php");
return;
}
$sql = "INSERT INTO participants (Par_Name, Par_Email, Par_Tel, Game_Num)
VALUES (:Par_Name, :Par_Email, :Par_Tel, :Game_Num)";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':Par_Name' => $_POST['Par_Name'],
':Par_Email' => $_POST['Par_Email'],
':Par_Tel' => $_POST['Par_Tel'],
':Game_Num' => $_POST['Game_Num']));
$_SESSION['success'] = 'Record Added';
header( 'Location: index.php' ) ;
return;
}
// Flash pattern
if ( isset($_SESSION['error']) ) {
echo '<p style="color:red">'.$_SESSION['error']."</p>\n";
unset($_SESSION['error']);
}
?>
<p>Add A New User</p>
<form method="post">
<p>Participant Name:
<input type="text" name="name"></p>
<p>Participant Email:
<input type="text" name="email"></p>
<p>Particpant Telephone Number:
<input type="text" name="telephone"></p>
<p>Game Number:
<input type="text" name="game"></p>
<p><input type="submit" value="Add New"/>
<a href="index.php">Cancel</a></p>
</form>
index.php
<?php
require_once "pdo.php";
session_start();
?>
<html>
<head></head><body>
<?php
if ( isset($_SESSION['error']) ) {
echo '<p style="color:red">'.$_SESSION['error']."</p>\n";
unset($_SESSION['error']);
}
if ( isset($_SESSION['success']) ) {
echo '<p style="color:green">'.$_SESSION['success']."</p>\n";
unset($_SESSION['success']);
}
echo('<table border="1" cellpadding="10" cellspacing="1">'."\n");
$stmt = $pdo->query("SELECT Par_Num, Par_Name, Par_Tel, Par_Email, Game_Num FROM participants");
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
echo "<tr><td>";
echo(htmlentities($row['Par_Num']));
echo("</td><td>");
echo(htmlentities($row['Par_Name']));
echo("</td><td>");
echo(htmlentities($row['Par_Tel']));
echo("</td><td>");
echo(htmlentities($row['Par_Email']));
echo("</td><td>");
echo(htmlentities($row['Game_Num']));
echo("</td><td>");
echo('<a href="edit.php?Par_Num='.$row['Par_Num'].'">Edit</a> / ');
echo('<a href="delete.php?Par_Num='.$row['Par_Num'].'">Delete</a>');
echo("</td></tr>\n");
}
?>
</table>
<a href="add.php">Add New</a>
CodePudding user response:
You need to set proper name of Input Box, You have set different name in Input box and You are using different name in post variables:
Your add.php page HTML needs like this:
<p>Add A New User</p>
<form method="post">
<p>Participant Name:
<input type="text" name="Par_Name"></p>
<p>Participant Email:
<input type="text" name="Par_Email"></p>
<p>Particpant Telephone Number:
<input type="text" name="Par_Tel"></p>
<p>Game Number:
<input type="text" name="Game_Num"></p>
<p><input type="submit" value="Add New"/>
<a href="index.php">Cancel</a></p>
</form>