Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,171,790 members, 7,882,752 topics. Date: Sunday, 07 July 2024 at 01:20 PM

How Do I Prevent Double Entry Into My Database Using PHP - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / How Do I Prevent Double Entry Into My Database Using PHP (679 Views)

Ransomware Attacks 2017: What Is Ransomware And How To Prevent, Stop Ransomware / How Can I Prevent / And Index.html From Having The Same Title Tage? / How Do I Get Information From My Database To Browser? (2) (3) (4)

(1) (Reply)

How Do I Prevent Double Entry Into My Database Using PHP by Raylight2(m): 11:55pm On May 07, 2015
Please webmasters, I am currently working on a project and have built the Mysql database and I send data from form using the script below:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbase";
//Create Connection
$conn = new mysqli($servername, $username, $password, $dbname);
if (!isset($_POST['undergraduate']))
{
$type="";
$n="";
$t="";
$f="";
$d="";
$l="";
$v="";
$z="";
$fa="";
$e="";
$u="";
$p="";
}
else{
$type=$_POST['type'];
$n=$_POST['name'];
$t=$_POST['tel'];
$f=$_POST['faculty'];
$d=$_POST['dept'];
$l=$_POST['level'];
$v=$_POST['villa'];
$z=$_POST['zone'];
$fa=$_POST['facebook'];
$u=$_POST['username'];
$p=$_POST['password'];
$e=$_POST['email'];
}
//Check Connection
if ($conn->connect_error){
die("Connection failed:" . $conn->connect_error);
}
$sql = "INSERT INTO undergraduate (type,name,tel,faculty,dept,level,villa,zone,facebook,email,username,password) VALUES('$type','$n','$t','$f','$d','$l','$v','$z','$fa','$e','$u','$p')";
$send=mysql_query($sql);
$up="<b style='color:green;'>You have successfully registered with us! $n</b>";
$conn->close();
?>

The challenge I am facing at present is that a user can register twice. Please, how do i contain this.

Please help me out...it's kinda urgent.. thanks for your past response.

I remain loyal...
Re: How Do I Prevent Double Entry Into My Database Using PHP by Nobody: 5:01am On May 08, 2015
just search against the database....



<?php
if(!empty($_POST['name'])){

$name = filter_var($_POST['name'],FILTER_SANITIZE_STRING) ;
//assuming we already connected to the database..
$as = $conn ->query (''SELECT username FROM table WHERE username=$name"wink;
if($as -> num_rows > 0 ){
echo 'username already registered';
} else {
// do the real insertion here
}
}
?>

2 Likes

(1) (Reply)

Partners Needed in an Upcoming Forum / A Very Good Web Designer Needed / Let us build a Mobile Apps from your blog/websiteTo Grow Your Business( See How)

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 10
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.