Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,how are you doing guys actually nowadays am working on android app and this app connect with mysql db through php well everything is perfectly fine now i just want before inserting it check that cnic number is exist on db or not if yes so it give me return otherwise query will be executre of insert please guys help me am new on php

What I have tried:

<?php
include 'DatabaseConfig.php';
// Create connection
$conn = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
 if($_SERVER['REQUEST_METHOD'] == 'POST')
 {
 $DefaultId = 0;

 $ImageData = $_POST['image_path'];
 
 $ImageName = $_POST['image_name'];
 
 $Fullname = $_POST['fullname'];
 
 $CNIC = $_POST['cnic'];
 
  $Mobile = $_POST['mobile'];
  
  $Address = $_POST['address'];
  
   $District = $_POST['district'];
   
   $Gender = $_POST['gender'];
  
   $Education = $_POST['education'];
   
   $Ward = $_POST['ward'];
   
   $Ps = $_POST['ps'];
   
   $Uc = $_POST['uc'];
   
   $Bjf = $_POST['bjf'];
   
   $By = $_POST['by'];
   
   $Dt = $_POST['dt'];
   
   $Email = $_POST['email'];
 
 $GetOldIdSQL ="SELECT id FROM information ORDER BY id ASC";
 
 $Query = mysqli_query($conn,$GetOldIdSQL);
 
 while($row = mysqli_fetch_array($Query)){
 
 $DefaultId = $row['id'];
 }
 
 $ImagePath = "images/$DefaultId.png";
 
 $ServerURL = "laserlightskincare.com/$ImagePath";
 
 //$InsertSQL = "insert into information (image_path,image_name,Fullname,CNIC,mobile,Address,District,ps,uc,ward,Gender,education,Doj,bjf,boy) values ('$ServerURL','$ImageName','$Fullname','$Cnic','$Mobile','$Address','$District','$PS','$UC','Ward','$Gender','Education','$Doj','$bjf','$boj','$boy')";
 $InsertSQL = "insert into information (image_path,image_name,Fullname,CNIC,mobile,Address,District,Gender,education,ward,ps,uc,bjf,boy,Doj,email) values ('$ServerURL','$ImageName','$Fullname','$CNIC','$Mobile','$Address','$District','$Gender','$Education','$Ward','$Ps','$Uc','$Bjf','$By','$Dt','$Email')";
 
 if(mysqli_query($conn, $InsertSQL)){

 file_put_contents($ImagePath,base64_decode($ImageData));

 echo "You have successfully registered";
 } 
 mysqli_close($conn);
 }else{
 echo "Not Uploaded";
 }

?>
Posted
Updated 1-Jan-19 1:47am
v2
Comments
Richard MacCutchan 1-Jan-19 5:06am    
What is the question?
Member 9983063 1-Jan-19 5:17am    
Actually sir my question is when user register so before executing the insert query its check if user cnic no is already available in db so it give return otherwise user will be register

1 solution

PHP
$InsertSQL = "insert into information (image_path,image_name,Fullname,CNIC,mobile,Address,District,Gender,education,ward,ps,uc,bjf,boy,Doj,email) values ('$ServerURL','$ImageName','$Fullname','$CNIC','$Mobile','$Address','$District','$Gender','$Education','$Ward','$Ps','$Uc','$Bjf','$By','$Dt','$Email')";

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
Quote:
Actually sir my question is when user register so before executing the insert query its check if user cnic no is already available in db

try to select for that cnic and how many record you get.
 
Share this answer
 
v2
Comments
Member 9983063 1-Jan-19 14:46pm    
ok I got but please give me the solution of my problem please add the solution in my code if you can I will be really thankful to you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900