Save bank value to same user

Asked

Viewed 295 times

3

I have a page with a field for entries that are recorded in the database and when recording I assign a user ID randomly in this register.

Thus:

$name = $_POST['name'];
$phone1 = $_POST['phone1'];
$time = $_POST['time'];
$client = $_POST['client'];

$sql = "SELECT * FROM `tblUsers` where grupo = 'contato' and ativo = 1 and periodo = 'manha' ORDER BY RAND() LIMIT 1";
    $resultado = $pdo ->query($sql);
        if($resultado !== false)
            {
                foreach($resultado as $row)
                            {
                                $randomicId = $row['idVendedor'];
                            }
            }

In the above SQL I take a random ID of my users and assign the $randomicId variable.

At the time of inserting in the bank:

$stmt = $pdo->prepare('INSERT INTO tblContacts(name,phone1,time,client,date,hourSend,qtd,origem,idUsuario)VALUES(:name,:phone1,:time,:client,:date,:hourSend,:qtd,:origem,:idUsuario)');
                        $stmt->execute(array(':name' => $name,':phone1' => $phone1,':time' => $time, ':client' => $client, ':date' =>$date, ':hourSend' => $hour , ':qtd' => $qtd, ':origem'=> $origem,':idUsuario'=>$randomicId));

So far everything happens normal, after saving I select and display this data to the logged users according to their ID.

My problem is the repeated registrations, because I wanted to record even if they were repeated.

For example:

  • no day 1 so-and-so made a registration and recorded in the bank for ID user 32.
  • no day 3 so did another registration, but this time recorded in the bank ID user 20.

Thus, I would be sending 2 entries to the same user.

How to verify if the registration already exists and if it already exists save the same user ID in the bank ?

ps.: The 'phone1' field will hardly be equal as there are no 2 equal phones. It could be used to make the comparison.

  • If a person fills in the wrong phone, will redo the form with the right phone... this would already break the check by phone. Same with the name...

  • So , but how could I check ? Ps.: In the phone field , I already do the treatment not to come with missing number, letter, etc. the problem would be same as Cvoce said , if the person type wrong . But , still ,it is difficult to occur .

  • Before generating random ID you cannot give a SELECT in tblContacts searching for the client and returning the idUsuario? If find, use it, otherwise you generate the random.

  • I tried to do so , but is not saving any ID now : $sql = "SELECT * FROM tblContacts Where phone1 = '$phone1' "; $result = $Pdo ->query($sql); if($result !== false) { foreach($result as $Row) { $randomicId = $Row['idVendedor']; } } Else Sql that will generate the random ID }

  • boy, tips that work me, I like to make this type of registration, of very elaborate, today I have the perfect function, since you have Name, Phone, Time and Client, in case, I do not know exactly how you use this Time, but, the name, I believe that there is only one person with a name and surname in your system that belongs to the team such and has the phone such, the site you can do to compare all of them, ie when you type the name and surname, go in the database and search, when it finds, it shows a green message for the user to know that it is correct because it has already done so, so the

  • Did any of the answers help?

Show 1 more comment

2 answers

1

You may be doing it 2 ways, it’s more an outline than a solution. But I hope you can help him:

  • Create a field that he reports on a type of password, or own identification code and then write to the database using as verification.

Example:

Código de identificação: meunome123456


OR


  • Creates a Cookie with a token user and save it to be used as verification. But there is the problem of the user deleting the navigation data.

You may be creating a Cookie as follows:

$token = md5(uniqid(rand() . time(), true));
setcookie("Nome do Cookie", $token, time() + (30 * 24 * 60 * 60), "/"); // expira em 30 dias

You can access the Cookie like this:
$_COOKIE["Nome do Cookie"]

0

Notice errors in PHP are just for trying to use something that doesn’t exist. Declaim the variable $randomicId out of the if blocks if your need is to use it in different places.

  • Good afternoon Fernando , the problem if I declare outside the IF , it will not catch the correct amount. I know that the variable is filled in the true condition ( if you find an equal record in the database , its value will be equal to the saved user id) . But when passing to the 'fake' IF condition , is that it is not catching .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.