Jump to content

check if number already exists in database


saltedm8

Recommended Posts

I have a form that I am submitting to the database, one of those fields is id, that has to be a number, I need to be able to submit the number ( within the form choices user just types in any number they want ) and check to see if the number already exists in the database, if it exists I want to display a message to choose another id number

 

I am having trouble comparing the numbers in the database, how can I check the number and test to see if there are any of the same number already in the database ?

 

<?php 
include("connect.php");

$id = $_POST['id'];
$name	= $_POST['name'];
$address	= $_POST['address'];
$email	= $_POST['email'];

if($_POST['submit']){

$turn = mysql_query("SELECT letable WHERE id ='$id'");
if ($turn = $id){

	echo 'that number exists already';

	}

if($id){

	mysql_query("INSERT INTO letable (`id`,`name`,`address`,`email`)VALUES('$id','$name','$address','$email')");

}if(!$id){

	echo "<strong>id has not been filled in</strong>";

}

              }



?>

 

as you can see, I have tried, but obviously in that case id is the same as turn anyway lol

 

thank you

Link to comment
Share on other sites

one, turn is a mysql resource, not the results itself. You have to use mysql_fetch_array or one of the other functions that fetches the data.

 

also, if id is an integer column, don't surround what you are comparing it to with quotes. that denotes it as a string, which may cause problems

 

also, since you are testing if a row is present with the id, you don't need to compare the values, since they are already the same. you should check if the query returned any results

 

try

$turn = mysql_query("SELECT letable WHERE id =$id'");
$num = mysql_num_rows($turn);
if ($num > 0){
//no

 

Edit: one last thing. what you have here

if ($turn = $id){

is valid syntax. but incorrect logic. You are using the assignment operator (=) which assigns the value of the right operand to the left. You want to use the comparison operator(==) which will compare two values

if ($turn == $id){

 

very simple mistake that is commonly made, but just watch out for it

Link to comment
Share on other sites

I should have warned you I am a beginner  :shrug:

 

this is what I changed it to

 

<?php 
include("connect.php");

$id = (int)$_POST['id'];
$name	= $_POST['name'];
$address	= $_POST['address'];
$email	= $_POST['email'];


if($_POST['submit']){


if(mysql_num_rows($turn = mysql_query("SELECT letable WHERE id =$id LIMIT 1")))
       echo 'that number exists already';

if($id){

	mysql_query("INSERT INTO letable (`id`,`name`,`address`,`email`)VALUES('$id','$name','$address','$email')");

}if(!$id){

	echo "<strong>id has not been filled in</strong>";

}

              }



?>

 

and this was the result after submission and purposely putting in an existing number ( and it still posted in the database )

 

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in G:\wamp\www\lessons\homeworkdynamicform.php on line 36
Link to comment
Share on other sites

thats because,



if(mysql_num_rows($turn = mysql_query("SELECT letable WHERE id =$id LIMIT 1")))

is incorrect syntax.

$turn = mysql_query("SELECT letable WHERE id =$id LIMIT 1")

is a boolean statement. It basically means if $turn is assigned the value of that query, return true. else return false

 

I posted the fixed version one post up. change it to that

Link to comment
Share on other sites

thats because,



if(mysql_num_rows($turn = mysql_query("SELECT letable WHERE id =$id LIMIT 1")))

is incorrect syntax.

$turn = mysql_query("SELECT letable WHERE id =$id LIMIT 1")

is a boolean statement. It basically means if $turn is assigned the value of that query, return true. else return false

 

I posted the fixed version one post up. change it to that

 

Actually, they didn't have the right syntax :-o Notice how there's no "FROM table" in there.

 

if(mysql_num_rows($turn = mysql_query("SELECT * FROM letable WHERE id =$id LIMIT 1")))

Link to comment
Share on other sites

thank you, that last one worked, just have to work out how to stop it posting to the database now if its already in there  8)

 

Change to:

if(mysql_num_rows($turn = mysql_query("SELECT letable WHERE id =$id LIMIT 1"))){
       echo 'that number exists already';
} else {
	if($id){
		mysql_query("INSERT INTO letable (`id`,`name`,`address`,`email`)VALUES('$id','$name','$address','$email')");
	}else{
	echo "<strong>id has not been filled in</strong>";
	}
}

Link to comment
Share on other sites

AHHHH.. 2 things.. its not putting anything into the database when its ok, and the second thing is when I test putting in no id

 

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in G:\wamp\www\lessons\homeworkdynamicform.php on line 33

id has not been filled in

Link to comment
Share on other sites

Make sure it's this:

 

if(mysql_num_rows($turn = mysql_query("SELECT * FROM letable WHERE id =$id LIMIT 1"))){
	echo 'that number exists already';
} else {
	if(isset($id) && !empty($id)){
		mysql_query("INSERT INTO letable (`id`,`name`,`address`,`email`)VALUES('$id','$name','$address','$email')");
	}else{
		echo "<strong>id has not been filled in</strong>";
	}
}

Link to comment
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
p{ margin:0; padding:0;}
form { margin-top:55px; }
input { margin: 4px 0 4px 0;}

.one {margin: 0 0 0 58px;}
.two {margin: 0 0 0 37px;}
.three {margin: 0 0 0 22px;}
.four {margin: 0 0 0 38px;}
.five {margin: 0 0 0 165px;}
-->
</style>
</head>

<?php 
include("connect.php");

$id =    $_POST['id'];
$name	= $_POST['name'];
$address	= $_POST['address'];
$email	= $_POST['email'];


if($_POST['submit']){

  
     if(mysql_num_rows($turn = mysql_query("SELECT * FROM letable WHERE id =$id LIMIT 1"))){

echo 'that number exists already';

} else {

if(isset($id) && !empty($id)){

mysql_query("INSERT INTO letable (`id`,`name`,`address`,`email`)VALUES('$id','$name','$address','$email')");	

}else{

echo "<strong>id has not been filled in</strong>";


}	

}
		  
		  



?>

<body>

<table width="50%" border="0">
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>


<form id="form1" class="top" method="post" action="">
  <p>
    <label class="one">id:
      <input type="text" name="id" id="id" />
    </label>
  </p>
  <p>
    <label class="two">name:
      <input type="text" name="name" id="name" />
    </label>
  </p>
  <p>
    <label class="three">address:
      <input type="text" name="address" id="address" />
    </label>
  </p>
  <p>
    <label class="four">email:
      <input type="text" name="email" id="email" />
    </label>
  </p>
  <p>
    <label>
      <input class="five" type="submit" name="submit" id="submit" value="submit" />
    </label>
  </p>
</form>
</body>
</html>

Link to comment
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
p{ margin:0; padding:0;}
form { margin-top:55px; }
input { margin: 4px 0 4px 0;}

.one {margin: 0 0 0 58px;}
.two {margin: 0 0 0 37px;}
.three {margin: 0 0 0 22px;}
.four {margin: 0 0 0 38px;}
.five {margin: 0 0 0 165px;}
-->
</style>
</head>

<?php 
include("connect.php");
$id = $_POST['id'];
$name = $_POST['name'];
$address = $_POST['address'];
$email = $_POST['email'];

if($_POST['submit']) {
if(mysql_num_rows($turn = mysql_query("SELECT * FROM letable WHERE id =$id LIMIT 1"))) {
	echo 'that number exists already';
} else {
	if(isset($id) && !empty($id)) {
		mysql_query("INSERT INTO letable (`id`,`name`,`address`,`email`)VALUES('" . $id . "','" . $name . "','" . $address . "','" . $email . "')");
	} else {
		echo "<strong>id has not been filled in</strong>";
	}
}
}
?>

<body>

<table width="50%" border="0">
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>


<form id="form1" class="top" method="post" action="">
  <p>
    <label class="one">id:
      <input type="text" name="id" id="id" />
    </label>
  </p>
  <p>
    <label class="two">name:
      <input type="text" name="name" id="name" />
    </label>
  </p>
  <p>
    <label class="three">address:
      <input type="text" name="address" id="address" />
    </label>
  </p>
  <p>
    <label class="four">email:
      <input type="text" name="email" id="email" />
    </label>
  </p>
  <p>
    <label>
      <input class="five" type="submit" name="submit" id="submit" value="submit" />
    </label>
  </p>
</form>
</body>
</html>

Link to comment
Share on other sites

Try giving this one a shot.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
p{ margin:0; padding:0;}
form { margin-top:55px; }
input { margin: 4px 0 4px 0;}

.one {margin: 0 0 0 58px;}
.two {margin: 0 0 0 37px;}
.three {margin: 0 0 0 22px;}
.four {margin: 0 0 0 38px;}
.five {margin: 0 0 0 165px;}
-->
</style>
</head>

<?php 
include("connect.php");

if 
isset($_POST['id']) && 
isset($_POST['name']) && 
isset($_POST['address']) && 
isset($_POST['email']) && 
isset($_POST['submit']) && 
!empty($_POST['id'])) && 
!empty($_POST['name'])) && 
!empty($_POST['address'])) && 
!empty($_POST['email']))
{
if(mysql_num_rows(mysql_query("SELECT * FROM `letable` WHERE `id`='" . $_POST['id'] . "'LIMIT 1"))) {
	print('<b>Error:</b> Row of data with ID ' . $_POST['id'] . ' already exists in database.');
} else {
	mysql_query("INSERT INTO letable (`id`,`name`,`address`,`$_POST['email']`)VALUES('" . $_POST['id'] . "','" . $_POST['name'] . "','" . $_POST['address'] . "','" . $$_POST['email'] . "')");
}
} else {
print('<b>Error:</b> Form was not filled completely. Please try again.')
?>

<body>

<table width="50%" border="0">
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>


<form id="form1" class="top" method="post" action="">
  <p>
    <label class="one">id:
      <input type="text" name="id" id="id" />
    </label>
  </p>
  <p>
    <label class="two">name:
      <input type="text" name="name" id="name" />
    </label>
  </p>
  <p>
    <label class="three">address:
      <input type="text" name="address" id="address" />
    </label>
  </p>
  <p>
    <label class="four">$_POST['email']:
      <input type="text" name="$_POST['email']" id="$_POST['email']" />
    </label>
  </p>
  <p>
    <label>
      <input class="five" type="submit" name="submit" id="submit" value="submit" />
    </label>
  </p>
</form>
</body>
</html>

Link to comment
Share on other sites

Alright, I ran it through my debugger, and it came through error-free. Let's just hope everything is clean in connect.php and your database so I can call it a night... ;)

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
p{ margin:0; padding:0;}
form { margin-top:55px; }
input { margin: 4px 0 4px 0;}

.one {margin: 0 0 0 58px;}
.two {margin: 0 0 0 37px;}
.three {margin: 0 0 0 22px;}
.four {margin: 0 0 0 38px;}
.five {margin: 0 0 0 165px;}
-->
</style>
</head>

<?php 
error_reporting(E_ALL);
ini_set('display_errors','1');

include("connect.php");

if (isset($_POST['id']) && isset($_POST['name']) && isset($_POST['address']) && isset($_POST['email']) && isset($_POST['submit']) && !empty($_POST['id']) && !empty($_POST['name']) && !empty($_POST['address']) && !empty($_POST['email'])) {
    if(mysql_num_rows(mysql_query("SELECT * FROM `letable` WHERE `id`='" . $_POST['id'] . "'LIMIT 1"))) {
        print('<b>Error:</b> Row of data with ID ' . $_POST['id'] . ' already exists in database.');
    } else {
        mysql_query("INSERT INTO `letable` (`id`,`name`,`address`,`email`) VALUES('" . $_POST['id'] . "','" . $_POST['name'] . "','" . $_POST['address'] . "','" . $$_POST['email'] . "')");
    }
} else {
    print('<b>Error:</b> Form was not filled completely. Please try again.');
}
?>

<body>

<table width="50%" border="0">
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>


<form id="form1" class="top" method="post" action="">
  <p>
    <label class="one">id:
      <input type="text" name="id" id="id" />
    </label>
  </p>
  <p>
    <label class="two">name:
      <input type="text" name="name" id="name" />
    </label>
  </p>
  <p>
    <label class="three">address:
      <input type="text" name="address" id="address" />
    </label>
  </p>
  <p>
    <label class="four">$_POST['email']:
      <input type="text" name="$_POST['email']" id="$_POST['email']" />
    </label>
  </p>
  <p>
    <label>
      <input class="five" type="submit" name="submit" id="submit" value="submit" />
    </label>
  </p>
</form>
</body>
</html>

Link to comment
Share on other sites

Alright, here we go...

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
p{ margin:0; padding:0;}
form { margin-top:55px; }
input { margin: 4px 0 4px 0;}

.one {margin: 0 0 0 58px;}
.two {margin: 0 0 0 37px;}
.three {margin: 0 0 0 22px;}
.four {margin: 0 0 0 38px;}
.five {margin: 0 0 0 165px;}
-->
</style>
</head>

<?php 
error_reporting(E_ALL);
ini_set('display_errors','1');

include("connect.php");

if (isset($_POST['id'])) && (isset($_POST['name'])) && (isset($_POST['address'])) && (isset($_POST['email'])) && (isset($_POST['submit'])) && (!empty($_POST['id'])) && (!empty($_POST['name'])) && (!empty($_POST['address'])) && (!empty($_POST['email']))
{
if(mysql_num_rows(mysql_query("SELECT * FROM `letable` WHERE `id`='" . $_POST['id'] . "'LIMIT 1"))) {
	print('<b>Error:</b> Row of data with ID ' . $_POST['id'] . ' already exists in database.');
} else {
	mysql_query("INSERT INTO letable (`id`,`name`,`address`,`$_POST['email']`)VALUES('" . $_POST['id'] . "','" . $_POST['name'] . "','" . $_POST['address'] . "','" . $$_POST['email'] . "')");
}
} elseif (isset($_POST['submit'])) {
print('<b>Error:</b> Form was not filled completely. Please try again.')
}
?>

<body>

<table width="50%" border="0">
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>


<form id="form1" class="top" method="post" action="">
  <p>
    <label class="one">id:
      <input type="text" name="id" id="id" />
    </label>
  </p>
  <p>
    <label class="two">name:
      <input type="text" name="name" id="name" />
    </label>
  </p>
  <p>
    <label class="three">address:
      <input type="text" name="address" id="address" />
    </label>
  </p>
  <p>
    <label class="four">$_POST['email']:
      <input type="text" name="$_POST['email']" id="$_POST['email']" />
    </label>
  </p>
  <p>
    <label>
      <input class="five" type="submit" name="submit" id="submit" value="submit" />
    </label>
  </p>
</form>
</body>
</html>

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.