Via a form on the site, organizations can be added to the database. The organization ID is unique in PhpMyAdmin, but organizations with the same name can be added multiple times. Any suggestions on how to fix this within PHP or PhpMyAdmin?
The database has the following structure:
And in funtions.php, I have the following code:
// Add sport organizations to database
if (isset($_POST['button'])){
$data = array(
'organization' => $_POST['organization'],
'description' => $_POST['description'],
'website' => $_POST['website'],
'facebook' => $_POST['facebook'],
'instagram' => $_POST['instagram'],
'phone' => $_POST['phone'],
'email' => $_POST['email'],
);
$table_name = 'organizations';
$result = $wpdb->insert($table_name, $data, $format=NULL);
if ($result==1) {
echo "<script>alert('Organizations saved');</script>";
}
else {
header("refresh: 0; url=/");
echo "<script>alert('Unable to save organization');</script>";
}
}
And for reference, this is the HTML code:
<form autocomplete="off" role="form" method="POST">
<div >
<div >
<input id="organization" name="organization" type="text" placeholder="Name of organization" required="">
</div>
<div >
<input id="description" name="description" type="textarea" placeholder="Description" required="">
</div>
<div >
<input id="website" name="website" type="text" placeholder="Website" >
</div>
<div >
<input id="facebook" name="facebook" type="text" placeholder="Facebook" >
</div>
<div >
<input id="instagram" name="instagram" type="text" placeholder="Instagram" >
</div>
<div >
<input id="phone" name="phone" type="text" placeholder="Phone" >
</div>
<div >
<input id="email" name="email" type="text" placeholder="Email" >
</div>
</div>
<div >
<div >
<div >
<input type="submit" name="submitbtn">
</div>
</div>
</div>
</form>
CodePudding user response:
Basically you need to add unique index to the organizations table but you have to consider proper error handling since any attempt to add organization with duplicated name will throw an error.
ALTER TABLE `organizations` ADD UNIQUE INDEX `unq_organization` (`organization`);