Here's an example of a bootstrap form to add/edit/delete records in the sg_users table using PHP and PDO:
<?php
// Check if form has been submitted
if(isset($_POST['submit'])) {
// Get form data
$email = $_POST['email'];
$name = $_POST['name'];
$usertype = $_POST['usertype'];
$company = $_POST['company'];
$tel = $_POST['tel'];
$mobile = $_POST['mobile'];
$businesstype = $_POST['businesstype'];
$newsletter = isset($_POST['newsletter']);
$status = isset($_POST['status']);
$logo = $_FILES['logo']['name'];
$rating = $_POST['rating'];
// Validate form data (e.g. check required fields, validate email format, etc.)
// ...
// If logo file is uploaded, move it to server directory and update logo field in $params array below
if(!empty($_FILES['logo']['name'])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES['logo']['name']);
move_uploaded_file($_FILES['logo']['tmp_name'], $target_file);
$logo = $target_file;
}
// Update or insert record in sg_users table
if($_POST['sgid']) {
// Update existing record
$sql = "UPDATE sg_users SET email = :email, name = :name, usertype = :usertype, company = :company, tel = :tel, mobile = :mobile, businesstypeid = :businesstype, newsletter = :newsletter, status = :status, rating = :rating";
if(!empty($logo)) {
$sql .= ", logo = :logo";
}
$sql .= " WHERE sgid = :sgid";
} else {
// Insert new record
$sql = "INSERT INTO sg_users (email, name, usertype, company, tel, mobile, businesstypeid, newsletter, status, rating, logo) VALUES (:email, :name, :usertype, :company, :tel, :mobile, :businesstype, :newsletter, :status, :rating, :logo)";
}
// Prepare SQL statement and bind parameters
$stmt = $DB->prepare($sql);
$params = array(
':email' => $email,
':name' => $name,
':usertype' => $usertype,
':company' => $company,
':tel' => $tel,
':mobile' => $mobile,
':businesstype' => $businesstype,
':newsletter' => $newsletter,
':status' => $status,
':rating' => $rating
);
if(!empty($logo)) {
$params[':logo'] = $logo;
}
if($_POST['sgid']) {
$params[':sgid'] = $_POST['sgid'];
}
$stmt->execute($params);
if($_POST['sgid']) {
// Update success message
$message = "Record updated successfully.";
} else {
// Insert success message
$message = "Record added successfully.";
}
}
// Check if edit/delete link is clicked and get sgid parameter
if(isset($_GET['action']) && $_GET['action'] == "edit" && isset($_GET['sgid'])) {
$stmt = $DB->prepare("SELECT * FROM sg_users WHERE sgid = :sgid");
$stmt->execute(array(':sgid' => $_GET['sgid']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
} elseif (isset($_GET['action']) && $_GET['action'] == "delete" && isset($_GET['sgid'])) {
// Delete record and show success message
$stmt = $DB->prepare("DELETE FROM sg_users WHERE sgid = :sgid");
$stmt->execute(array(':sgid' => $_GET['sgid']));
$message = "Record deleted successfully.";
}
// Get list of businesstypes for dropdown
$stmt = $DB->prepare("SELECT * FROM sg_businesstype");
$stmt->execute();
$businesstypes = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!-- HTML form -->
<div class="container">
<h2>SG Users</h2>
<?php if($message) { ?>
<div class="alert alert-success"><?php echo $message; ?></div>
<?php } ?>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="sgid" value="<?php echo $row['sgid']; ?>">
<div class="form-group">
<label for="email">Email*</label>
<input type="email" class="form-control" id="email" name="email" required value="<?php echo $row['email']; ?>">
</div>
<div class="form-group">
<label for="name">Name*</label>
<input type="text" class="form-control" id="name" name="name" required value="<?php echo $row['name']; ?>">
</div>
<div class="form-group">
<label for="usertype">User Type*</label>
<select class="form-control" id="usertype" name="usertype" required>
<option value="1" <?php echo $row['usertype'] == 1 ? "selected" : ""; ?>>Free</option>
<option value="2" <?php echo $row['usertype'] == 2 ? "selected" : ""; ?>>Premium</option>
</select>
</div>
<div class="form-group">
<label for="company">Company</label>
<input type="text" class="form-control" id="company" name="company" value="<?php echo $row['company']; ?>">
</div>
<div class="form-group">
<label for="tel">Tel</label>
<input type="tel" class="form-control" id="tel" name="tel" value="<?php echo $row['tel']; ?>">
</div>
<div class="form-group">
<label for="mobile">Mobile</label>
<input type="tel" class="form-control" id="mobile" name="mobile" value="<?php echo $row['mobile']; ?>">
</div>
<div class="form-group">
<label for="businesstype">Business Type</label>
<select class="form-control" id="businesstype" name="businesstype">
<option value="">Select a business type</option>
<?php foreach($businesstypes as $businesstype) { ?>
<option value="<?php echo $businesstype['businesstypeid']; ?>" <?php echo $row['businesstypeid'] == $businesstype['businesstypeid'] ? "selected" : ""; ?>><?php echo $businesstype['businesstype']; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<label for="newsletter"><input type="checkbox" id="newsletter" name="newsletter" <?php echo $row['newsletter'] ? "checked" : ""; ?>> Subscribe to newsletter</label>
</div>
<div class="form-group">
<label for="status"><input type="checkbox" id="status" name="status" <?php echo $row['status'] ? "checked" : ""; ?>> Active account</label>
</div>
<div class="form-group">
<label for="logo">Logo</label>
<input type="file" id="logo" name="logo" class="form-control-file">
<?php if($row['logo']) { ?>
<img src="<?php echo $row['logo']; ?>" alt="Logo" style="max-height: 100px;">
<?php } ?>
</div>
<div class="form-group">
<label for="rating">Rating</label>
<input type="number" class="form-control" id="rating" name="rating" min="0" max="5" value="<?php echo $row['rating']; ?>">
</div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
<a href="sg_users.php" class="btn btn-secondary">Cancel</a>
<?php if($row['sgid']) { ?>
<a href="sg_users.php?action=delete&sgid=<?php echo $row['sgid']; ?>" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this record?')">Delete</a>
<?php } ?>
</form>
</div>