How to check if the entered fields already exists or not, during Updating data using mysql
How to check if the entered fields already exists or not, during Updating data using mysql
Update.php
<?php
include_once("connection.php");
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$mobno=$_REQUEST['mobno'];
$id=$_REQUEST['id'];
$checkemail="SELECT * FROM dhruv_users WHERE email= '$_REQUEST[email] AND id='$id'";
$checkmob="SELECT * FROM dhruv_users WHERE mobno= '$_REQUEST[mobno]' AND id='$id'";
$rsemail = mysqli_query($conn,$checkemail);
$rsmob = mysqli_query($conn,$checkmob);
$dataemail = mysqli_num_rows($rsemail);
$datamob = mysqli_num_rows($rsmob);
if($dataemail >= 1 && $datamob >= 1) {
echo "data_exists";
}
else{
$query=mysqli_query($conn,"update dhruv_users set name='$name',email='$email',mobno='$mobno' where id='$id'");
if($query){
echo "success";
}
else
{
echo "unsuccess";
}
}
?>
Ok Here is the problem, I want to check while updating the email or mobile number that if the, email id and mob number is already taken by some one else or not. But when I submit data it shows the email id is taken, as I am already using that email id and mobile number.
I want to check some one else rather than me is using that email id and mobile number or not.
AND id != your_id
didnt worked pls elaborate yourself
– DHRUV THAKKAR
Jul 2 at 2:07
You have to change the
SELECT query in a way that your account isn't included. This can be done with the condition id != your_id_number. This has to be added to the other already conditions like email=... or mobno=.... This way you will not get your own account but all the other accounts matching the email and/or mobno.– Progman
Jul 2 at 17:49
SELECT
id != your_id_number
email=...
mobno=...
1 Answer
1
I'm just re-writing your code and changing some of its parts so that it can give you desired result . But before that I will suggest you to block injection and also use mysqli , not the old one .
<?php
include_once("connection.php");
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$mobno=$_REQUEST['mobno'];
$id=$_REQUEST['id'];
$sql_check = "SELECT * FROM dhruv_users WHERE email = '".$email."'
OR mobno = '".$mobno."'" ;
$res_check = mysql_query($sql_check) ;
$row_check = mysql_fetch_array($res_check) ;
if(!empty($row_check)){
echo "data_exists";
}
else{
$query=mysqli_query("update dhruv_users set name='$name', email ='$email', mobno ='$mobno' where id='$id'");
if(mysql_affected_rows() > 0){
echo "Success" ;
}
else{
echo "Update unsuccessful" ;
}
}
?>
no this dosnt works, please re-read the question!
– DHRUV THAKKAR
Jul 1 at 11:28
You want to check whether email id and mobile number combination exists or either the email id or mobile number exists .
– PHP Web
Jul 1 at 16:18
yes, but in different manner. check if someone is using the mobno or email id rather than me
– DHRUV THAKKAR
Jul 2 at 2:07
yes , I understand , my code will give you the same output you are looking for .
– PHP Web
Jul 2 at 6:33
Right now may be there is no other user who is using the same mobile number or email id , if there is one , it will show you the message that data exists . You register yourself as an outsider and test it yourself .
– PHP Web
Jul 2 at 6:34
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Use
AND id != your_idsince you want to check all the other accounts, not your account.– Progman
Jul 1 at 9:45