Jump to content

im having an erro when i there is a duplicate on id number


theratix

Recommended Posts

Hi

im having an erro when i there is a duplicate on id number. i want the code to return just a simple message like the key already exist and echo the existing key,

here is my code.

<?php
try {
$db_options = array(
/* important! use actual prepared statements (default: emulate prepared statements) */
/* throw exceptions on errors (default: stay silent) */
/* fetch associative arrays (default: mixed arrays) */
PDO::ATTR_EMULATE_PREPARES => false
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO('mysql:host=localhost;dbname=safabc;charset=utf8', 'root', '', $db_options);
} catch (PDOException $e) { // Report the Error!
/* You never want to use the following on a production website */
$status_message = "<p>Something is not right, check your php.ini settings or code</p>";
}
$data = array();
$submit = filter_input(INPUT_POST, 'submit', FILTER_SANITIZE_SPECIAL_CHARS);
if ( isset($submit) && $submit == 'Submit') {
$data['id_number'] = filter_input(INPUT_POST, 'id_number', FILTER_SANITIZE_SPECIAL_CHARS);
$data['player_name'] = filter_input(INPUT_POST, 'player_name', FILTER_SANITIZE_SPECIAL_CHARS);
$data['player_surname'] = filter_input(INPUT_POST, 'player_surname', FILTER_SANITIZE_SPECIAL_CHARS);
$data['club'] = filter_input(INPUT_POST, 'club', FILTER_SANITIZE_SPECIAL_CHARS);
$data['league'] = filter_input(INPUT_POST, 'league', FILTER_SANITIZE_SPECIAL_CHARS);
/* Setup the query for the database table */
$query = 'INSERT INTO players
(id_number, player_name, player_surname, club, league, date_registered)
VALUES
(:id_number, :player_name, :player_surname, :club, :league, NOW())
';
/* Prepare the query */
$stmt = $pdo->prepare($query);
/* Execute the Query */
$result = $stmt->execute( array(
':id_number' => $data['id_number'],
':player_name' => $data['player_name'],
':player_surname' => $data['player_surname'],
':club' => $data['club'],
':league' => $data['league']
));
/* Display the result of the insertion */
if ($result)
{
$status_message = "Data Successfully inserted";
}
else
{
$status_message = "Insertion Failed";
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Database Table Insert</title>
</head>
<body>
<p>Status of Insertion: <span><?php echo (isset($status_message)) ? $status_message : 'Waiting...'; ?></span></p>
<form action="" method="post">
<label class="labelStyle" for="id_number">ID Number</label>
<input id="id_number" type="text" name="id_number" value=""><br>
<label class="labelStyle" for="player_number">First Name</label>
<input id="player_name" type="text" name="player_name" value=""><br>
<label class="labelStyle" for="player_surname">Last Name</label>
<input id="player_surname" type="text" name="player_surname" value=""><br><br>
<select id="club" name="club">
<option value="tigers" selected>Detroit Tigers</option>
<option value="royals">Kansas City Royals</option>
<option value="indians">Cleveland Indians</option>
<option value="white_sox">Chicago White Sox</option>
<option value="twins">Minnesota Twins</option>
</select>
<br><br>
<input type="radio" name="league" value="american" checked>American League
<input type="radio" name="league" value="national">National League
<br><br>
<input class="submitBtn" type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...