saltedm8 Posted September 7, 2009 Share Posted September 7, 2009 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 Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 7, 2009 Share Posted September 7, 2009 try $id = (int)$_POST['id']; Make sure in the database, the column storing the number is set to some type of "...INT" Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted September 7, 2009 Share Posted September 7, 2009 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 Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 7, 2009 Share Posted September 7, 2009 if(mysql_num_rows($turn = mysql_query("SELECT letable WHERE id =$id LIMIT 1"))) echo 'that number exists already'; Try that instead. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted September 7, 2009 Share Posted September 7, 2009 if(mysql_num_rows(mysql_query("SELECT letable WHERE id =$id LIMIT 1"))) shorter than my example, but basically the same thing Quote Link to comment Share on other sites More sharing options...
saltedm8 Posted September 7, 2009 Author Share Posted September 7, 2009 I should have warned you I am a beginner 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 Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted September 7, 2009 Share Posted September 7, 2009 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 Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 7, 2009 Share Posted September 7, 2009 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"))) Quote Link to comment Share on other sites More sharing options...
saltedm8 Posted September 7, 2009 Author Share Posted September 7, 2009 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 Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 7, 2009 Share Posted September 7, 2009 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 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>"; } } Quote Link to comment Share on other sites More sharing options...
saltedm8 Posted September 7, 2009 Author Share Posted September 7, 2009 nah im getting Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in G:\wamp\www\lessons\homeworkdynamicform.php on line 32 again, and its still inserting Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 7, 2009 Share Posted September 7, 2009 Fix the query again xD I copied it wrong :-o the "SELECT letable" to "SELECT * FROM letable" Quote Link to comment Share on other sites More sharing options...
saltedm8 Posted September 7, 2009 Author Share Posted September 7, 2009 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 Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 7, 2009 Share Posted September 7, 2009 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>"; } } Quote Link to comment Share on other sites More sharing options...
saltedm8 Posted September 7, 2009 Author Share Posted September 7, 2009 parse error line 98 Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 7, 2009 Share Posted September 7, 2009 Copy/paste the whole code and put it in [php][/php] tags. Quote Link to comment Share on other sites More sharing options...
saltedm8 Posted September 7, 2009 Author Share Posted September 7, 2009 <!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> Quote Link to comment Share on other sites More sharing options...
bundyxc Posted September 7, 2009 Share Posted September 7, 2009 <!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> Quote Link to comment Share on other sites More sharing options...
saltedm8 Posted September 7, 2009 Author Share Posted September 7, 2009 very close.... just got a warning when I tested not putting in any id at all Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in G:\wamp\www\lessons\homeworkdynamicform.php on line 32 id has not been filled in Quote Link to comment Share on other sites More sharing options...
bundyxc Posted September 7, 2009 Share Posted September 7, 2009 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> Quote Link to comment Share on other sites More sharing options...
saltedm8 Posted September 7, 2009 Author Share Posted September 7, 2009 Parse error on line 34 lol.. we will get there hopefully Quote Link to comment Share on other sites More sharing options...
bundyxc Posted September 7, 2009 Share Posted September 7, 2009 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> Quote Link to comment Share on other sites More sharing options...
saltedm8 Posted September 7, 2009 Author Share Posted September 7, 2009 very very close.. a little snag Error: Form was not filled completely. Please try again. is there before I even fill out the form, once its filled out its fine Quote Link to comment Share on other sites More sharing options...
bundyxc Posted September 7, 2009 Share Posted September 7, 2009 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.