I'm collecting the birthdates of users in my system, doing so by linking the the user's unique ID to their birthday entry in another table.
To prevent users from accidentally/purposefully entering two birthdate entries for their accounts, I'd like to remove to the entry form for birthdays IF the user has already entered a birthday prior.
For instance:
$value = mysqli_query("SELECT bd_user_id FROM user_birthdate WHERE
bd_user_id="$user_id";");
From that data, how will I be able to return some form value to determine if whether the user's ID has already been index in user_birthdate or not? (Where $user_id = The current user's ID)
Or perhaps I'm taking the wrong approach here? The logic behind it is what's been getting me.
How can I check if whether a value is NOT indexed in a database table?
You normally query the database as you did
$value = mysqli_query("SELECT bd_user_id FROM user_birthdate WHERE bd_user_id="$user_id";");
Than you use mysqli_num_rows(), and check if it returns 0.
$num_rows = mysqli_num_rows($value);
if($num_rows > 0){
//exists
}else{
//doesn't exist
}
**Sorry, as Devon said in your case it's mysqli_num_rows not mysql_num_rows.
Related
In PHP, I'm using mysqli_affected_rows to validate whether a user has been registered in a database, where a query looks something like this:
"UPDATE schedule WHERE userid = '$user' AND date='2018-05-05'"
If the user doesn't exist, the number of affected rows is 0, and if they do exist, it's 1.
This works OK, except for when a user is registered in a second time. Here, MySQL will say 0 rows are affected, since there has been no change to the schedule.
Is there anything I can add to my query to always affect rows? Or is this just a poor approach?
To check whether a user exists in a database, I would use a select statement instead.
sql = "SELECT * FROM schedule WHERE userid = '$user' AND date='2018-05-05'"
Then do something if a user exists
$result = $conn->query($sql);
if ($result->num_rows > 0) {
//user exists
}
I would also assume you would like to have unique users in the database? I that case I would check whether a user exists first using the same code before adding a new record.
Good evening.
I have a doubt about inclusion in the database. A common example is a user account where the user names must be unique.
In the following logic:
User enters data into a form.
Data are collected via $ _POST (or other method)
Before saving the data is checked if the user name is already registered
If not, save the record, if it is, it informs the user that the username that already exists.
Example:
<?php
$username = $_POST['username'];
$query = "SELECT * FROM `user_tbl` WHERE `username` = '{$username}'";
$result = mysql_query($query);
if ( mysql_num_rows ( $result ) > 1 ) {
/* Username already exists */
echo 'Username already exists';
} else {
/* Username doesn't exist */
/* a certain time is elapsed after checking */
/* .. insert query */
}
My question is, in a system with high volume of requests, it is possible that between the time to check whether the user name already exists and the inclusion (if the user name does not exist), another user can do the same action in same time?
UPDATE
I know about the safety issue, use this code (copy and past) to explain to the question is about the possibility of two users do the same thing at the same time. On the issue of unique index, I know how it works, maybe I was not clear on the question, it was only to see if there was a possibility of "simultaneous commands occur." Thanks for the answers.
Besides the already placed correct comments, the solution to this is to use transactions & an unique index:
The unique index guarantees that a value can only exist once.
In your case the index could be:
CREATE UNIQUE INDEX idx_nn_1 ON user_tbl(username);
That way the user can only exist once. If two people insert the same username now at the same time, one of the inserts will fail. That failure you need to catch and handle.
Well I have my register done all nice, as required fields it holds your email, name, and password. When you log in it asks for your email and password.
When they log in I have it so that they have their email stored in a session, but how do I get their firstname stored in a session, even though they didn't type it? Thanks.
The thing is, I don't know the users first name so...
When the user logins in with their username/password and after verification of the information, query the database for their record. Set the returned value(s) into the session. If you are already getting information about the account, add a table join (if in separate tables) or the columns.
When they log in and you are checking the login credentials (email and password), if you find the user, select their first name as well and store it in another session variable:
$query = 'SELECT firstname FROM users WHERE email = $email AND password = $password LIMIT 1';
...
if ($result && mysql_num_rows($result) == 1) {
$row = mysql_fetch_object($result);
$_SESSION['firstname'] = $row->firstname;
...
That might not be exact based on table and column names, but that's the general idea.
I am having endless troubles with duplicate entries, so I need to check the database, and if a user has already entered that day, their entry will not be submitted and they will be redirected to a landing page that tells them they have already entered that day, and that they may only enter again tomorrow.
The field I would like to check is the id_number field in the database, since each user has a unique id number, so basically, if a user with the same id number submitted on the same date they should be redirected to a landing page, how would I go about doing this? I am still new to a lot of this, so please be forgiving.
Thanx in advance.
before you submit your sql query to add a new record you can read the DB first to make sure the ID is not already in there. Get the users id into a variable and do this query.
$query = "select * from mytable where `id` = '$id'";
$result = mysql_query($query);
then you can count the number of records returned and do different things depending on the result, checking that its zero before adding them. If its not zero then redirect.
if(mysql_num_rows($result) > 0)
{
//add them
}
else
{
//redirect them
}
Hope this helps you out
Another question which has me perplexed:
I have a table which enables users to enter as many rows as they like based on their userid and unique id (auto incremental).
I need to be able to get this information from mysql and place the previously entered information into the fields on the web application (they may need to be edited before confirming that they're correct).
I store the total number of records for that user so far in one variable, and the total number of records for all users in another variable.
The question is: how do I get the range of ids for the records the user has already enterered.
Example: User 1 has 2 records in the database and there is 7 in total (5 by another user). How would I get the unique IDs of the 2 records that already exist?
Thanks for any suggestions you may have!
I'm not entirely sure what you mean, so this may or may not be helpful.
This SQL should give you the record ids:
SELECT id FROM tableofuserrows WHERE userid = [User Id]
You can then fetch this from the database with PHP, e.g.
$q = mysql_query('SELECT id FROM tableofuserrows WHERE userid = ' . (int) $_GET['userid']) or die(mysql_error());
$result = array();
while ($row = mysql_fetch_assoc($q)) {
$result[] = $row['id'];
}
mysql_free_result($q);
echo json_encode($result);
So if you wanted to fetch these IDs from the browser using jQuery:
$.getJSON("http://url", { userid: 3 }, //set userid properly
function(data){
$.each(data, function(i,id){
//do something with the recordid
alert(id);
});
}
);
Do you have to do this dynamically using jquery or can you load the fields in the web form with the rest of the page using php ?
Either way, you're going to need to query the database table for all rows where userid = a certain user. Once you get these results, you'll need to create a page you can call and get results from using jquery if you're going that route.
Someone just posted what I'm saying with code examples :-)
I decided to use MIN(id) in the select statement, counting how many rows there are and then populating the form fields accordingly, starting with the min value and adding the counted rows. Works well ;)