Hey everyone,
I have been going over this in my head and can't seem to figure out the solution. I've got a database table called brands and in my form I'm trying to get it to where when I edit a brand, I can't edit it into an already existing brand. My code just automatically adds the new brand even though it exists as well as adds the existing brand as a new brand if I were to retype the existing brand. I hope that makes sense..
Basically I want to check if a brand exists in the database based on it's id, and if it does, throw out an error. I also want to check if form was submitted with values or if it is empty, if it is, throw error. ..and if no errors are thrown, add/edit the table in databse.
Here is my php logic for validating the form:
$sql ="SELECT * FROM brand ORDER BY brand";
$results = $db->query($sql);
$errors = array();
//Edit brand
if (isset($_GET['edit']) && !empty($_GET['edit'])) {
$edit_id = (int)$_GET['edit'];
$edit_id = sanitize($edit_id);
$sql2 = "SELECT * FROM brand WHERE id = '$edit_id'";
$edit_result = $db->query($sql2);
$eBrand = mysqli_fetch_assoc($edit_result);
}
// Delete brand
if (isset($_GET['delete']) && !empty($_GET['delete'])) {
$delete_id = (int)$_GET['delete'];
$delete_id = sanitize($delete_id);
$sql = "DELETE FROM brand WHERE id = '$delete_id'";
$db->query($sql);
header('Location: brands.php');
}
// if add form is submmited
if (isset($_POST['add_submit'])) {
$brand = sanitize(mysqli_real_escape_string($db, $_POST['brand']));
// check if brand is blank
if ($_POST['brand'] == '') {
$errors[] .= 'You must enter a brand!';
}
// check if brand exists in database
$sql = "SELECT * FROM brand WHERE brand = '$brand'";
if (isset($_GET['edit'])) {
$sql = "SELECT * FROM brand WHERE brand = '$brand' AND id != '$edit_id'";
}
$result = $db->query($sql);
$count = mysqli_num_rows($result);
if ($count > 0) {
$errors[] .= $brand.' already exists. Please choose another brand name...';
}
// display errors
if (!empty($errors)) {
echo display_errors($errors);
}else {
// add brand to database
$sql = "INSERT INTO brand (brand) VALUES ('$brand')";
if (isset($_GET['edit'])) {
$sql = "UPDATE brand SET brand = '$brand' WHERE id = '$edit_id'";
}
$db->query($sql);
header('Location: brands.php');
}
}
Here is my html form:
<div class="text-center">
<form class="form-inline" action="brands.php<?=((isset($_GET['edit']))?'?edit='.$edit_id:'');?>" method="post">
<div class="form-group">
<?php
$brand_value = '';
if (isset($_GET['edit'])) {
$brand_value = $eBrand['brand'];
}else {
if (isset($_POST['brand'])) {
$brand_value = sanitize($_POST['brand']);
}
} ?>
<label for="brand"><?=((isset($_GET['edit']))?'Edit':'Add a');?> Brand:</label>
<input type="text" name="brand" id="brand" class="form-control" value="<?=$brand_value; ?>">
<?php if(isset($_GET['edit'])): ?>
<a href="brands.php" class="btn btn-default">Cancel</a>
<?php endif; ?>
<input type="submit" name="add_submit" value="<?=((isset($_GET['edit']))?'Edit':'Add');?> brand" class="btn btn-success">
</div>
</form>
</div>
Any help or advice would be very much appreciated. Thank you!