kendallkamikaze Posted December 15, 2008 Share Posted December 15, 2008 I've been working on trying to fix this code for the last 2 hours, I want it to make it so that it will not let you enter two times. this is what i have so far: <?php include 'header.php'; $horseid = $_POST['horseid']; $userid = $_POST['userid']; $classtype = $_POST['classtype']; $classname = $_POST['classname']; $sql="INSERT INTO winterfest (horseid, userid, classname, classtype) VALUES ('$_POST[horseid]','$id','$_POST[classname]','$_POST[classtype]')"; if (!mysql_query($sql)) { die('You cannot enter that show because you have already that horse in one of its type.'); } echo "Success! You have entered Winterfest!"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/ Share on other sites More sharing options...
kendallkamikaze Posted December 15, 2008 Author Share Posted December 15, 2008 oops wrong thread Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-715592 Share on other sites More sharing options...
kendallkamikaze Posted December 15, 2008 Author Share Posted December 15, 2008 <?php include 'header.php'; $horseid = $_POST['horseid']; $userid = $_POST['userid']; $classtype = $_POST['classtype']; $classname = $_POST['classname']; $sql="INSERT INTO winterfest (horseid, userid, classname, classtype) VALUES ('$_POST[horseid]','$id','$_POST[classname]','$_POST[classtype]')"; if (!mysql_query($sql)) { die('You cannot enter that show because you have already that horse in one of its type.'); } echo "Success! You have entered Winterfest!"; ?> I've been working on trying to fix this code for the last 2 hours, I want it to make it so that it will not let you enter two times. Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-715594 Share on other sites More sharing options...
suma237 Posted December 15, 2008 Share Posted December 15, 2008 use REPLACE instead on INSERT. Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-715602 Share on other sites More sharing options...
kendallkamikaze Posted December 15, 2008 Author Share Posted December 15, 2008 <?php include 'header.php'; $horseid = $_POST['horseid']; $userid = $_POST['userid']; $classtype = $_POST['classtype']; $classname = $_POST['classname']; $sql="REPLACE INTO winterfest (horseid, userid, classname, classtype) VALUES ('$_POST[horseid]','$id','$_POST[classname]','$_POST[classtype]')"; if (!mysql_query($sql)) { die('You cannot enter that show because you have already that horse in one of its type.'); } echo "Success! You have entered Winterfest!"; ?> didnt work Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-715606 Share on other sites More sharing options...
Yesideez Posted December 15, 2008 Share Posted December 15, 2008 First use SELECT to see if they have that type of horse and if they don't use INSERT to add one. Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-715625 Share on other sites More sharing options...
Maq Posted December 15, 2008 Share Posted December 15, 2008 Not tested but should work. If the user has 2 or more records than it won't let them insert, are there any other restrictions? include 'header.php'; $horseid = $_POST['horseid']; $userid = $_POST['userid']; $classtype = $_POST['classtype']; $classname = $_POST['classname']; $check_query = "SELECT * FROM winterfest WHERE id = $userid"; $num_rows = mysql_num_rows($check_query) or die(mysql_error()); if($num_rows > 1) { echo "You have enter 2 or more times."; } else { $sql="INSERT INTO winterfest (horseid, userid, classname, classtype) VALUES ('$horseid','$userid','$classname','$classtype')"; mysql_query($sql) or die(mysql_query()); echo "Success! You have entered Winterfest!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-715885 Share on other sites More sharing options...
revraz Posted December 15, 2008 Share Posted December 15, 2008 I think he wants it so you can't enter twice, if so, he would need to change if($num_rows > 1) to either if($num_rows => 1) or if($num_rows > 0) Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-715906 Share on other sites More sharing options...
Maq Posted December 15, 2008 Share Posted December 15, 2008 Yes, she certainly did. Thanks revraz Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-715912 Share on other sites More sharing options...
kendallkamikaze Posted December 15, 2008 Author Share Posted December 15, 2008 still no luck :/ Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-715988 Share on other sites More sharing options...
gevans Posted December 15, 2008 Share Posted December 15, 2008 Does your table have a primary key? Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-716020 Share on other sites More sharing options...
kendallkamikaze Posted December 15, 2008 Author Share Posted December 15, 2008 yes. id int(10) No auto_increment Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-716029 Share on other sites More sharing options...
gevans Posted December 15, 2008 Share Posted December 15, 2008 Can you double check that it is a primary key, as replace needs a PK to work. Also your query is using POST data when you've already assigned it to variables. And in the query the var $id hasn't been assigned anywhere Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-716035 Share on other sites More sharing options...
revraz Posted December 15, 2008 Share Posted December 15, 2008 Dupe post? http://www.phpfreaks.com/forums/index.php/topic,230036.0.html Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-716036 Share on other sites More sharing options...
kendallkamikaze Posted December 15, 2008 Author Share Posted December 15, 2008 yes, sorry i had posted it in the wrong thread. I didnt even realize people were posting on it O.O With the code MAQ provided, seems like it would work but I'm getting: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/collegh9/public_html/one-stride/winterfest_enter.php on line 10 <?php include 'header.php'; $horseid = $_POST['horseid']; $userid = $_POST['userid']; $classtype = $_POST['classtype']; $classname = $_POST['classname']; $check_query = "SELECT * FROM winterfest WHERE id = $userid"; $num_rows = mysql_num_rows($check_query) or die(mysql_error()); if($num_rows => 1) { echo "You have enter 2 or more times."; } else { $sql="INSERT INTO winterfest (horseid, userid, classname, classtype) VALUES ('$horseid','$userid','$classname','$classtype')"; mysql_query($sql) or die(mysql_query()); echo "Success! You have entered Winterfest!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-716047 Share on other sites More sharing options...
revraz Posted December 15, 2008 Share Posted December 15, 2008 echo $check_query, sounds like $userid is empty or maybe it's a string? If so, put single quotes around $userid $check_query = "SELECT * FROM winterfest WHERE id = '$userid'"; Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-716058 Share on other sites More sharing options...
PFMaBiSmAd Posted December 15, 2008 Share Posted December 15, 2008 Actually, greater than or equal is >= => has special significance and is not a comparison operator. Upon further review, the posted code has a few other problems. There is no mysql_query() in it to execute the SELECT query and the or die() statement would need to be part of that mysql_query(). Replace this line - $num_rows = mysql_num_rows($check_query) or die(mysql_error()); With these - $result = mysql_query($check_query) or die(mysql_error()); $num_rows = mysql_num_rows($result)); Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-716062 Share on other sites More sharing options...
Maq Posted December 15, 2008 Share Posted December 15, 2008 Sorry thank you for the corrections guys, I don't have an environment to test this in right now ;/ @PFMaBiSmAd You have an extra ')' in the second line you corrected. My revised code (that should work ): include 'header.php'; $horseid = $_POST['horseid']; $userid = $_POST['userid']; $classtype = $_POST['classtype']; $classname = $_POST['classname']; $check_query = "SELECT * FROM winterfest WHERE id = $userid"; $result = mysql_query($check_query) or die(mysql_error()) $num_rows = mysql_num_rows($check_query); if($num_rows >= 1) { echo "You have enter 2 or more times."; } else { $sql="INSERT INTO winterfest (horseid, userid, classname, classtype) VALUES ('$horseid','$userid','$classname','$classtype')"; mysql_query($sql) or die(mysql_query()); echo "Success! You have entered Winterfest!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-716072 Share on other sites More sharing options...
kendallkamikaze Posted December 15, 2008 Author Share Posted December 15, 2008 with the above revised code, you get: Parse error: syntax error, unexpected T_VARIABLE in /home/collegh9/public_html/one-stride/winterfest_enter.php on line 11 Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-716075 Share on other sites More sharing options...
Maq Posted December 15, 2008 Share Posted December 15, 2008 Oops here you go (supposed to be $num_rows = mysql_num_rows($result); ) include 'header.php'; $horseid = $_POST['horseid']; $userid = $_POST['userid']; $classtype = $_POST['classtype']; $classname = $_POST['classname']; $check_query = "SELECT * FROM winterfest WHERE id = $userid"; $result = mysql_query($check_query) or die(mysql_error()) $num_rows = mysql_num_rows($result); if($num_rows >= 1) { echo "You have enter 2 or more times."; } else { $sql="INSERT INTO winterfest (horseid, userid, classname, classtype) VALUES ('$horseid','$userid','$classname','$classtype')"; mysql_query($sql) or die(mysql_query()); echo "Success! You have entered Winterfest!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-716078 Share on other sites More sharing options...
kendallkamikaze Posted December 15, 2008 Author Share Posted December 15, 2008 lol its all good...im new to this whole php thing and my programmer is unavilable... Now im getting: Parse error: syntax error, unexpected T_VARIABLE in /home/collegh9/public_html/one-stride/winterfest_enter.php on line 11 Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-716085 Share on other sites More sharing options...
premiso Posted December 15, 2008 Share Posted December 15, 2008 Look on/near line 11 and solve it. That is a basic syntax issue and easy fix. The issue lies with the $result row there is no ending ; after the die statement. Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-716087 Share on other sites More sharing options...
Maq Posted December 15, 2008 Share Posted December 15, 2008 $result = mysql_query($check_query) or die(mysql_error()) needs to be: $result = mysql_query($check_query) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-716089 Share on other sites More sharing options...
kendallkamikaze Posted December 15, 2008 Author Share Posted December 15, 2008 the code is fully functional now, but its still allowing double entries...AHAH! I've got it. I realized that user id is not actually posted onto the winterfest table, so by switching user id to horseid its now functional! <?php include 'header.php'; $horseid = $_POST['horseid']; $userid = $_POST['userid']; $classtype = $_POST['classtype']; $classname = $_POST['classname']; $check_query = "SELECT * FROM winterfest WHERE horseid = $horseid"; $result = mysql_query($check_query) or die(mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows >= 1) { echo "You have enter 2 or more times."; } else { $sql="INSERT INTO winterfest (horseid, userid, classname, classtype) VALUES ('$horseid','$userid','$classname','$classtype')"; mysql_query($sql) or die(mysql_query()); echo "Success! You have entered Winterfest!"; } ?> This website has been amazing. You guys are GREAT help to my lack of PHP knowledge! I wish I could pay you guys for all your help. If you ever need any work or have time for any work I'm willing to pay to get things fixed/made for my game! <3 Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-716096 Share on other sites More sharing options...
Maq Posted December 15, 2008 Share Posted December 15, 2008 I wish I could pay you guys for all your help. Donate! Quote Link to comment https://forums.phpfreaks.com/topic/137004-solved-no-double-entry-please-help/#findComment-716101 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.