Jump to content

How to check for unique phone numbers in mysql?


bambinou1980

Recommended Posts

Hello,

 

I am new to PHP, I only started to read books and watch video online.

I do apologies  if my code is incorrect and added in a stupid way but I did my maximum with whatever I have learned till now.

 

I am having a real problem, I have created 2 forms, the first form takes the data of the users and then pass them to the next form using sessions. Now on the second form, I need to take the final data, send it to mysql and return an error if the phone number is not unique(if the user has already registered).

 

Any idea how to do this please?

The code works well apart form this.

Also, at the end of the execution, I need the page to be redirected to the first page, which is why I use an header redirect.

 

Please help keep looking online for a solution but get completely mixed up.

On this form I only have:

<?php  session_start();// store session data//echo $_COOKIE['PHPSESSID'];    $code = trim($_POST['code']);    require_once("header.php"); //if ($code != $_SESSION['random'])//{    //Echo '<br><br>You should have now receive a  Code via sms</br>';   // Echo 'Please enter Your Mobile Code in the Box!</br>';   // Echo 'If no Mobile Code have been received, contact email.....</br>';//}else{    //echo ' There was an error, please go back and try again';//}?><!DOCTYPE HTML><head><link type="text/css" rel="stylesheet" href="jotform.css" /></head><body> <div align="center"><form action="" method="post" enctype="application/x-www-form-urlencoded">  <div class="form-all">    <ul class="form-section">      <div class="form-line" id="id_1"><input type="text" name="email" value="<?php echo $_SESSION["email"];?>" type="text" />  <input type="text" name="phone" value="<?php echo $_SESSION['prefix'];?><?php echo $_SESSION['phone'];?>" type="text" /> <div class="form-label-top">Type in Your Mobile Code<br />Awaiting for Validation</div><div id="cid_1" class="form-input-wide"> <input class=" form-textbox" name="code" value="" type="text" maxlength="6" /><br /><img src="awaiting-loading.gif" width="105" height="16" /></div><br /><div><textarea class="textarea" id="textarea" readonly="readonly" rows="7" cols="68">  Terms and Conditions ForMemberskhkhjkjhkjhkhkjhkjhkjh </textarea> </div></br><input class=" form-textbox" type="checkbox" name="terms" /><span id="form-tickbox-text">I have read and agree to the Terms and Conditions</span></br><input class=" form-textbox" type="checkbox" name="agree" /><span id="form-tickbox-text">Yes I agree to receive VIP notifications</span></br><input type="hidden" name="submitted" value="1"/><input name="submit" type="submit" value="Register Now!" class="form-submit-button form-submit-button-flat_round_teal"/><br /><br /></div></form><?php echo $_SESSION['random']; ?> </div></body></html>

 

 

Here it is:

On my second form page, I have "require_once("header.php");" at the top. The below code is for the header.php page

<php><?php$host="localhost"; // Host name $username="---------------"; // Mysql username $password="----------------"; // Mysql password $db_name="----------------"; // Database name $tbl_name="-----------------"; // Table name   $random = trim($_POST['code']);$phone = trim($_POST['phone']); $email = trim($_POST['email']); //$session_random = trim($_SESSION['random']);     //If hidden field is submitted    if ( isset($_POST['submitted']) && $_POST['submitted']==1){   $email_sanitize= $email ;  if(filter_var($email_sanitize, FILTER_VALIDATE_EMAIL)  == FALSE) {     echo '<div align="center" class="text-not-correct">Error:xx001 There is an error with the form please try again</div></br>';        }else{  if(strlen($phone) !== 11)   {  echo '<div align="center" class="text-not-correct">Error: xx002 There is an error with the form please try again</div></br>';    }else{      if(empty($email_sanitize))    {     $errorMessage .= '<div align="center" class="text-not-correct">Error: xx003 There is an error with the form please try again</div></br>';       }else{    if ($random != $_SESSION['random'])   {   echo '<div align="center" class="text-not-correct">Your Mobile code is incorrect - Please try again</div></br>';      }else{       echo '';      if(!isset($_POST['terms'])){        echo '<div align="center" class="text-not-correct">Please tick and accept the Terms and Condition Box!</div></br>';        }else{            if(!isset($_POST['agree'])){            echo '<div align="center" class="text-not-correct">Please tick and accept the VIP notifications!</div></br>';       }else{       <!-------------------------------HERE IS WHERE i STRUGGLE-------------------------------->    $con=mysqli_connect($host,$username,$password,$db_name); // Check connectionif (mysqli_connect_errno($con))  {  echo '<div align="center" class="text-not-correct">Error: xx003 Failed to connect to Database: </div></br>';  }mysqli_query($con,"CREATE TABLE $tbl_name");  $email=$_POST['email'];$phone=$_POST['phone'];$code=$_POST['code'];$date = date('Y-m-d H:i:s');  $email=mysqli_real_escape_string($con,$email);$phone=mysqli_real_escape_string($con,$phone);$code=mysqli_real_escape_string($con,$code); // This query will fail, cause we did not escape $newpersmysqli_query($con,"INSERT into $tbl_name (date, phone_number, email, code) VALUES ('$date', '$phone', '$email', '$code')"); // This query will work, cause we escaped $newpers   if(mysqli_num_rows($con) > 0) {            echo "Phone already exist!";    }else{ mysqli_close($con); if (mysqli_query) header("Refresh: 5;url=http://website.com/page1.php");echo '<div align="center" class="text-correct">Congratulation you are now registered!</div></br>';echo '<div align="center"><img src="loading.gif" width="100" height="100" /></div>';             }}}}}}}}?>  
</php>
 

 

 

 

 

Thank you so much in advance for your help, a friend of mine told me this forum was top of the top  to get problems sorted in PHP.

 

 

 

 

Ben

Link to comment
Share on other sites

I think that you should not try to create the table in your script.

mysqli_query($con,"CREATE TABLE $tbl_name");

Set it up once, before your site goes live and forget about it. When you create the table make the phone column a unique key. This will not allow a duplicate phone to be inserted. Before the insert try something like:

$result=mysqli_query($con,"SELECT * FROM $tblname WHERE phone=$phone");
$row_cnt = mysqli_num_rows($result);
if ($row_cnt>0){
echo "this phone number has already been used";
}else{
// Do your insert here
}
Link to comment
Share on other sites

Hi Freak Dr and the Web mason

 

Thank you so much for your help.

I have added Dr Freak code after the insert and it gives me 2 strange errors:

 

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, object given in /home...
 
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home....
 
 
Any idea what this translate to please?
 
 
<?php$email=$_POST['email'];$phone=$_POST['phone'];$code=$_POST['code'];$date = date('Y-m-d H:i:s');  $email=mysqli_real_escape_string($con,$email);$phone=mysqli_real_escape_string($con,$phone);$code=mysqli_real_escape_string($con,$code);  $result=mysqli_query($con,"SELECT * FROM $tblname WHERE phone=$phone");$row_cnt = mysqli_num_rows($result);if ($row_cnt>0){echo '<div align="center" class="text-correct">This phone number has already been registered!</div>';}else{ // This query will fail, cause we did not escape $newpersmysqli_query($con,"INSERT into $tbl_name (date, phone_number, email, code) VALUES ('$date', '$phone', '$email', '$code')"); // This query will work, cause we escaped $newpers   if(mysqli_num_rows($con) > 0) {            echo "Phone already exist!";    }else{ mysqli_close($con); if (mysqli_query) header("Refresh: 5;url=http://site.com/page1.php");echo '<div align="center" class="text-correct">Congratulation you are now registered!</div></br>';echo '<div align="center"><img src="loading.gif" width="100" height="100" /></div>';             }}}}}}}}}?> 

 
 
Thank you!
 
Ben
Link to comment
Share on other sites

One can only assume your system forces unique usernames, apply the same technique to your phone number field.

 

Otherwise, post only the relevant code.

 

Bambinou, do you understand what Jessica is saying here? By enforcing a UNIQUE constraint on the phone field within your table you'll achieve the basis of what you want. You can then implement php code to check for the phone number and display an error if it exists. 

 

Moreover, why on earth were you attempting to create the table every time the script loaded? Think logically about what you're doing.

Link to comment
Share on other sites

I have added Dr Freak code after the insert and it gives me 2 strange errors:

 

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, object given in /home...
 
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home....
 
 
Any idea what this translate to please?

You never connected to the database. You will need to create the database and add the table first. I'd recommend using PhpMyAdmin. Google mysql php tutorial for an overview of how  MySQL works or perhaps Jessica can recommend one. Then you need a line in your code before you do anything wiht Mysql like


$con = mysqli_connect("localhost", "my_user", "my_password", 'my_db');
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.