DarkShadowWing Posted September 3, 2009 Author Share Posted September 3, 2009 and by do, i mean submit Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-911986 Share on other sites More sharing options...
mikesta707 Posted September 3, 2009 Share Posted September 3, 2009 um, that link doesn't work. Do you get any errors on your page? try changing mysql_num_rows to mysql_affected_rows. by the way, the code you currenly have doesn't check the right data id Firstname Lastname email 1 test user blah@blah.blah 2 test user blah@blah.blah 3 test user blah@blah.blah 4 5 6 7 test and 'firstname' (which is what you test for) are not the same so it will allow you to update the table Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-911989 Share on other sites More sharing options...
DarkShadowWing Posted September 3, 2009 Author Share Posted September 3, 2009 right. what i need is to check if the user has entered his or her name more than once, and has entered his or her email more than once, and submitted it more than once. like if i enter test as my first name, and user for my last, it would check the database to see if "test" AND "user" BOTH EXIST, if they don't, then submit, else return an error to me Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-911992 Share on other sites More sharing options...
scotchegg78 Posted September 3, 2009 Share Posted September 3, 2009 make sure you check your posts before sticking them in the DB first... run em through this function... function safe($value){ return mysqli_real_escape_string($con, $value); } Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-911996 Share on other sites More sharing options...
DarkShadowWing Posted September 3, 2009 Author Share Posted September 3, 2009 thanks mate! ^^ Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-912000 Share on other sites More sharing options...
mikesta707 Posted September 3, 2009 Share Posted September 3, 2009 OK, so if i submit test as first name and user as lastname, if both exist in a row, then I can't submit, however, if 1 row has test as first name but poop as last name, I can still submit? In that case change your or clauses (||) to and clauses (&&) Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-912001 Share on other sites More sharing options...
mikesta707 Posted September 3, 2009 Share Posted September 3, 2009 make sure you check your posts before sticking them in the DB first... run em through this function... function safe($value){ return mysqli_real_escape_string($con, $value); } hes probably gonna wanna use mysql_real_escape_string seeing as he is using mysql functions. but yes this is a good idea Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-912003 Share on other sites More sharing options...
DarkShadowWing Posted September 3, 2009 Author Share Posted September 3, 2009 right. what i need is to check if the user has entered his or her name more than once, and has entered his or her email more than once, and submitted it more than once. like if i enter test as my first name, and user for my last, it would check the database to see if "test" AND "user" BOTH EXIST, if they don't, then submit, else return an error to me Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-912008 Share on other sites More sharing options...
DarkShadowWing Posted September 3, 2009 Author Share Posted September 3, 2009 OK, so if i submit test as first name and user as lastname, if both exist in a row, then I can't submit, however, if 1 row has test as first name but poop as last name, I can still submit? In that case change your or clauses (||) to and clauses (&&) no. lol. Ur right on the 1st part. u CANT submit if it finds "test" OR "user". but if it's "poop", and it finds "test" again, then it wont let it submit either. Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-912010 Share on other sites More sharing options...
mikesta707 Posted September 3, 2009 Share Posted September 3, 2009 oh my bad I should have caught this earlier. You have to send the post variables to the function recordExists('firstname', $_POST['firstname'], 'users0001','metaldetect01') The 2nd parameter was the value of the column, and the first was the column itself right? Yeah you have to pass the post variables to the function. try it now Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-912012 Share on other sites More sharing options...
DarkShadowWing Posted September 3, 2009 Author Share Posted September 3, 2009 Now it shows this in my db after 2 submits of "test", "user", and "email": id firstname lastname email 1 test user blah@blah.blah 2 test user blah@blah.blah 3 test user blah@blah.blah 4 5 6 7 8 9 The code: if(recordExists('firstname', $_POST['firstname'], 'users0001','metaldetect01') || recordExists('lastname', $_POST['lastname'], 'users0001','metaldetect01') || recordExists('email', $_POST['email'], 'users0001','metaldetect01')){ echo "user: ".$firstname.", ".$lastname.", email: ".$email." already exists!"; }else{ $sql="INSERT INTO $tbl (firstname, lastname, email) VALUES ('".safe($_POST['firstname'])."','".safe($_POST['lastname'])."','".safe($_POST['email'])."')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } } It returned NOTHING this time for id 8, and id 9 (lastest 2 submits i made) Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-912020 Share on other sites More sharing options...
DarkShadowWing Posted September 3, 2009 Author Share Posted September 3, 2009 do u know whats wrong? Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-912028 Share on other sites More sharing options...
unknown1 Posted September 3, 2009 Share Posted September 3, 2009 Try something like the following $sql_site = ("SELECT * FROM table WHERE `id` = '$_SESSION[id]' "); $site_con = mysql_query($sql_site, $conn) or die(mysql_error()); $numrows = mysql_num_rows($site_con); if($numrows > 0){ while($row = mysql_fetch_array($site_con)) { echo "$row['row name']"; } Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-912036 Share on other sites More sharing options...
DarkShadowWing Posted September 4, 2009 Author Share Posted September 4, 2009 Now its complaining about T_String >< here: <?php $conn = "localhost"; $user = "myuser"; $pass = "mypass"; $dbname = "metaldetect01"; $tbl = "users0001"; $con = mysql_connect($conn,$user,$pass); if (!$con) { die('Could not connect to database: "' . $dbname . '" because ' . mysql_error()); } mysql_select_db($dbname, $con); $sql_site = ("SELECT * FROM $tbl WHERE `id` = $_SESSION[id] AND `firstname` = $_POST[firstname] AND `lastname` = $_POST[lastname] AND `email` = $_POST[email] "); $site_con = mysql_query($sql_site, $conn) or die(mysql_error()); $numrows = mysql_num_rows($site_con); //if(recordExists('firstname', 'firstname', 'users0001','metaldetect01') || recordExists('lastname', 'lastname', 'users0001','metaldetect01') || recordExists('email', 'email', 'users0001','metaldetect01')){ if($numrows > 0){ while($row = mysql_fetch_array($site_con)) { echo "$row['row name']"; echo "user: ".$firstname.", ".$lastname.", email: ".$email." already exists!"; }else{ $sql="INSERT INTO $tbl (firstname, lastname, email) VALUES ('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['email']."')"; echo "You were successfully added to the database, $firstname, $lastname!"; echo "Returning you to the previous page..."; echo "<script>setTimeout('document.location=\"./ThankYou.html\";', 10000);</script>"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } } function safe($value){ return mysqli_real_escape_string($con, $value); } function recordExists($id,$idval,$table,$db) {//check for id=idval in table and return TRUE or FALSE $result = mysql_query("SELECT * FROM ".$table." WHERE ".$id."='".$idval."'") or die(mysql_error()); if(mysql_num_rows($result) > 0) {//if we found more than 0 return true; }//end if row return false; } mysql_close($con) ?> the line it complains about is: if($numrows > 0){ while($row = mysql_fetch_array($site_con)) { echo "$row['row name']"; echo "user: ".$firstname.", ".$lastname.", email: ".$email." already exists!"; } the whole code part for that above is: if($numrows > 0){ while($row = mysql_fetch_array($site_con)) { echo "$row['row name']"; echo "user: ".$firstname.", ".$lastname.", email: ".$email." already exists!"; }else{ $sql="INSERT INTO $tbl (firstname, lastname, email) VALUES ('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['email']."')"; echo "You were successfully added to the database, $firstname, $lastname!"; echo "Returning you to the previous page..."; echo "<script>setTimeout('document.location=\"./ThankYou.html\";', 10000);</script>"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } } Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-912115 Share on other sites More sharing options...
DarkShadowWing Posted September 4, 2009 Author Share Posted September 4, 2009 I've tried everything. I STILL cant fix the "T_String".. Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-912409 Share on other sites More sharing options...
DarkShadowWing Posted September 4, 2009 Author Share Posted September 4, 2009 Ok. its fixed but its STILL not detecting multiple user name records in the mysql database.. <?php $conn = "localhost"; $user = "myuser"; $pass = "mypass"; $dbname = "metaldetect01"; $tbl = "users0001"; $con = mysql_connect($conn,$user,$pass); if (!$con) { die('Could not connect to database: "' . $dbname . '" because ' . mysql_error()); } mysql_select_db($dbname, $con); $sql_site = ("SELECT * FROM $tbl WHERE `id` = '$_SESSION[id]' AND `firstname` = '$_GET[firstname]' AND `lastname` = '$_GET[lastname]' AND `email` = '$_GET[email]' "); $site_con = mysql_query($sql_site, $con) or die(mysql_error()); $numrows = mysql_num_rows($site_con); if($numrows > 0){ while($row = mysql_fetch_array($site_con)) { echo "user: ".$row['firstname'].", ".$row['lastname'].", email: ".$row['email']." already exists!"; } }else{ $sql="INSERT INTO $tbl (firstname, lastname, email) VALUES ('".$_GET['firstname']."','".$_GET['lastname']."','".$_GET['email']."')"; echo "You were successfully added to the database, ".$_GET['firstname'].", ".$_GET['lastname']."!"; echo "Returning you to the previous page..."; echo "<script>setTimeout('document.location=\"./ThankYou.html\";', 10000);</script>"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } } function safe($value){ return mysqli_real_escape_string($con, $value); } function recordExists($id,$idval,$table,$db) {//check for id=idval in table and return TRUE or FALSE $result = mysql_query("SELECT * FROM ".$table." WHERE ".$id."='".$idval."'") or die(mysql_error()); if(mysql_num_rows($result) > 0) {//if we found more than 0 return true; }//end if row return false; } mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-912462 Share on other sites More sharing options...
DarkShadowWing Posted September 4, 2009 Author Share Posted September 4, 2009 can someone please help me? it still lets me submit the same name or email twice.. Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-912739 Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 Click Quote Link to comment https://forums.phpfreaks.com/topic/173007-need-help-with-checking-value-in-database/page/2/#findComment-912787 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.