I have a page where theres a button, when I click the button a Modal shows up, which contains a form for adding a new user.
in the user table, username + email + userid are primary keys. So I need to validate first that they don't exist in the table.
Now inserting works just fine. also when I insert an already existing username/userid/email it doesn't get added. However, the alert doesn't show. so the user won't know why it wasn't added
try
{
if(isset($_POST['submit']))
{
if($_POST['submit'] == 'إضافة'){
$user_name = $_POST['user_name'];
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$role = $_POST['role'];
$userid = $_POST['userid'];
//check to see if username/email/userid already exists
//Check username
$stmt1 = $conn->prepare("SELECT * FROM users WHERE username='$user_name'");
//Check email
$stmt2 = $conn->prepare("SELECT * FROM users WHERE E-mail='$email'");
//Check userid
$stmt3 = $conn->prepare("SELECT * FROM users WHERE id=$userid");
$stmt1->execute();
$stmt2->execute();
$stmt3->execute();
if (mysqli_num_rows($stmt1)>0)
{
$name_error = "username already exists";
die();
}
else if (mysqli_num_rows($stmt2)>0)
{
$name_error = "email already exists";
die();
}
else if (mysqli_num_rows($stmt3)>0)
{
$name_error = "employee id already exists";
die();
}
//if username/email/userid don't exist, proceed with the insert query.
else
{
$stmt = $conn->prepare("INSERT INTO `user` (`id`, `username`, `name`, `E-mail`, `Password`, `Role`, `User_Id`) VALUES (NULL, '$username', '$Name', '$email', '$password', '$role', $userid)");
$stmt->execute();
$lastIntertedId = $conn->lastInsertId();
}
I added the following to the text fields, so the error shows beneath them.
<!-- Add User Modal -->
<div id="addUserModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST" action="user_managment.php" >
<div class="modal-header">
<h4 class="modal-title">إضافة مستخدم </h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>اسم المستخدم</label>
<input name="user_name" type="text" class="form-control" required>
<?php if(isset($name_error)): ?>
<span><?php echo $name_error;?></span>
<?php endif ?>
</div>
<div class="form-group">
<label>الاسم</label>
<input name="name" type="text" class="form-control" required>
</div>
<div class="form-group">
<label>الرقم الوظيفي</label>
<input name="userid" type="number" class="form-control" required>
<?php if(isset($name_error)): ?>
<span><?php echo $name_error;?></span>
<?php endif ?>
</div>
<div class="form-group">
<label>دور المستخدم </label>
<select name="role" class="form-control" id="sel1">
<option value="admin">مشرف</option>
<option value="employee">مدخل بيانات</option>
</select>
</div>
<div class="form-group">
<label>البريد الالكتروني</label>
<input name="email" type="email" class="form-control" required>
<?php if(isset($name_error)): ?>
<span><?php echo $name_error;?></span>
<?php endif ?>
</div>
<div class="form-group">
<label>الرقم السري</label>
<input name="password" type="password" class="form-control" required>
</div>
</div>
<div class="modal-footer" >
<input name="submit" type="submit" class="btn btn-success" value="إضافة">
<input type="button" class="btn btn-default" data-dismiss="modal" value="إلغاء">
</div>
</form>
</div>
</div>
</div>