Reputation: 8621
I'm attempting to check my database if a row exists, but it is always returning "User ID already exists for another Employee", if it exists or not. This is baffling me, because the code was working before. The latest change that I added was PHP sessions, which I don't think would break it?
With the sessions, if this info is needed, I start the session at the top of the page, and if the session is 0, it loads a password box, when the correct password is entered, it sets session to 1 which then shows all the other boxes on the page, including the one to add an employee to the table, which is where I am having issues.
Here is my code for the input boxes:
<h1>Add New Employee</h1>
<form method="POST">
ID: <input name="IDadd" type="text">
Name: <input name="Nameadd" type="text">
Job: <select name='Jobadd'>
<option value='IOS'>IOS</option>
<option value='Remarketing'>Remarketing</option>
<option value='Computers'>Computers</option>
<option value='Beats'>Beats</option>
<option value='Inventory'>Inventory</option>
<option value='Check-In'>Check-In</option>
<option value='Tech'>Tech</option>
</select>
<input value="Submit New Employee" type="submit" onsubmit="return false" id="newEmployee" name="newEmployee"><br><br></center>
</form>
And here is the code that should be checking if it exists already.
if (isset($_POST['newEmployee'])) {
$userId = mysqli_real_escape_string( $con, $_POST[ 'IDadd' ] );
$userName = mysqli_real_escape_string( $con, $_POST[ 'Nameadd' ] );
$userJob = mysqli_real_escape_string( $con, $_POST[ 'Jobadd' ] );
$checkUserID = mysqli_query("SELECT `userId` FROM Employee WHERE `userId`='$userId'");
if (mysqli_num_rows($checkUserId) > 0) {
$sql = "INSERT INTO Employee (userId, Name, Job, Week)
VALUES ('$userId', '$userName', '$userJob', '$currentWeekIs')";
mysqli_query($con, $sql);
} else {
echo $userId . " " . $userName . " " . $userJob;
echo "<center><br><br><h1>User ID ".$userId." already exists for another employee.</h1></center>";
}
}
So, I guess my question is, why is it always returning "User ID already exists for another Employee"? Or, what are some alternatives on how I can solve this issue?
Upvotes: 1
Views: 91
Reputation: 71422
Your query is failing because you are not passing proper parameters to mysqli_query()
where you are making your SELECT.
The first parameter need to be the connection resource and second the query itself. Check out the documentation, make sure you are looking at your erros logs when developing, and include error handling in your code.
Upvotes: 1