Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
How to check if name already exist in MySQL Database?
I have made an php form, a connection script and a php script to write in database.
index.php*************************************************************
<html>
<head>
<title>Test</title>
</head>
<body>
<form method="POST" action="send.php">
<label>Name</label><input type="text" name="name"><br />
<label>Age</label><input type="text" name="age"><br /><br />
<input type="submit">
</form>
</body>
</html>
**************************************************************************
send.php***************************************************************
<?php include_once("connect.php"); ?>
<?php
$name=$_POST["name"];
$age=$_POST["age"];
mysql_select_db("test", $connect);
$query=("INSERT INTO name (id, name, age) VALUES('', '$name', '$age')");
if(mysql_query($query,$connect)){
echo "<h1>:)</h1>";
}else{
echo mysql_error();
}
?>
*************************************************************************
I have a database named "test" and a table named "name"
and
in this table I have 3 columns id, name and age.
So,
How to check all names in database
and
give error to user that the name you have entered,
already exist in the database and choose another name.
It'll be great if you can just explain your answer because I'm Beginner.
Thank You in Advance.
6 Answers
- Anonymous8 years agoFavorite Answer
<?php
$name=$_POST["name"];
$age=$_POST["age"];
mysql_select_db("test", $connect);
$query_check_name = "select * from name where name ='" . $name . "'";
$res_check_name = mysql_query($query_check_name);
$arr_check_name = mysql_fetch_array($res_check_name);
if(empty($arr_check_name)){
$query=("INSERT INTO name (id, name, age) VALUES('', '$name', '$age')");
if(mysql_query($query,$connect)){
echo "<h1>:)</h1>";
}else{
echo mysql_error();
}
}
else{
// redirect to your page (index.php?msg=name_exist);
header( 'Location: index.php?msg=name_exist' ) ;
}
?>
you need to check on index page that $_GET['msg'] variable (msg) is exist or not. For this you can use isset() function. So put below code in index.php file on top or where you want to display message.
<?php
if(isset($_GET['msg']) && $_GET['msg'] == 'name_exist'){
echo "Name Already Exist. Please Enter Unique Name.";
}
?>
I did not check this code. May be possible some syntax error.
Hope this will help :)
Source(s): Self - Anonymous5 years ago
When the database isn't finding the '$name' value in your field it is returning 0 rows. I bet your php code assumes that you are getting at least one row back. Hence the error when the resource doesn't have anything in it. There are two ways around it. Either you can modify your sql to $query = "SELECT count(*) FROM users WHERE name='$name';"; which will guarantee you a result where you can just check that it's greater than 0 or you can use the mysql_num_rows() function to check that you actually have a row before you fetch the first row e.g. // does user exist? $name = mysql_real_escape_string($name); $query = "SELECT * FROM users WHERE name='$name';"; $res = mysql_query($query); if (mysql_num_rows($res) > 0) { // yes, pull in the user details } else // no, user doesn't exist }
- 8 years ago
First of all... create a unique key for name column
create table tablename (
...
user_id bigint, -- or whatever
age date,
unique key(name)
...
);
when you execute mysql_query then if name already exist then mysql return false other wise reture true..
$result= mysql_query(INSERT INTO table_name(age,name) values('23','ketan');
if(mysql_num_rows($result)==0){
echo 'name available';
}else{
echo '<script>alert("name already exist")</script>';
}
- 8 years ago
SORRY FOR MY BAD ENGLISH!
you need to select users from your database, that have you entered in the form...
mysql_query("SELECT id FROM name WHERE name = $name");
...then check if id is defined give an error using javascript.
- How do you think about the answers? You can sign in to vote the answer.
- just "JR"Lv 78 years ago
send.php******************************…
<?php //please, DO NOT close and re-open php!
include_once("connect.php");
$name=$_POST["name"];
$age=$_POST["age"];
mysql_select_db("test", $connect);
$query = "select * from `name` where `ìd`='" . $name . "' limit 1";
$res = mysql_query($query);
$row = mysql_fetch_array($res);
if ($row['id'] == $name)
echo ("Name already exists");
else
...