KruSuPhy Posted May 6, 2010 Share Posted May 6, 2010 Hey guys, I'm making a jeopardy game in PHP to better myself. I've been working on it for a couple days, and i ran into a problem making an Edit category Name system. i can connect to the db fine, and insert data fine, but i can't figure out how to make it send all five textfields to their ID spot in `sections`. I have id's one through five, but i can't figure out how to send it all to the database. i get some unexpected T_VARIABLE error on line 7, which is <?php include 'jepconfig.php'; $name=$_POST['secname']; $idnum=$_POST['idnum']; $query="INSERT $name INTO sections WHERE id="$idnum"" $result = mysql_query($query) ?> I may have done something wrong in that one, but nothing else i tried has worked either. Can someone help? I can upload the game of what i have so far if needed. Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/ Share on other sites More sharing options...
Muddy_Funster Posted May 6, 2010 Share Posted May 6, 2010 your query syntax is a little off: $query="INSERT INTO sections (name) VALUES (".$name.") WHERE id= ".$idnum."; also append your $results line so it reads: $result = mysql_query($query) or die (mysql_error); remember and keep an eye on your line terminators ( ; ) Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1053906 Share on other sites More sharing options...
Pikachu2000 Posted May 6, 2010 Share Posted May 6, 2010 If the records are already in the database, you would use an UPDATE query. If they are new records, you don't use a WHERE clause. Also, correct syntax for the die() statement to echo the actual error thrown by mysql would be: or die( mysql_error() ); Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1053908 Share on other sites More sharing options...
Muddy_Funster Posted May 6, 2010 Share Posted May 6, 2010 If the records are already in the database, you would use an UPDATE query. If they are new records, you don't use a WHERE clause. Also, correct syntax for the die() statement to echo the actual error thrown by mysql would be: or die( mysql_error() ); Thanks Pikachu, for clearing that up. That proves it's time for me to clock off for the night Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1053912 Share on other sites More sharing options...
KruSuPhy Posted May 6, 2010 Author Share Posted May 6, 2010 @Muddy; What are the periods in the names for? edit; @anyone really, what are the periods for? are they needed? Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1053913 Share on other sites More sharing options...
Pikachu2000 Posted May 6, 2010 Share Posted May 6, 2010 They're concatenation operators. Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1053915 Share on other sites More sharing options...
KruSuPhy Posted May 6, 2010 Author Share Posted May 6, 2010 that means it puts two strings together, right? Why would it be needed in this case? Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1053916 Share on other sites More sharing options...
Pikachu2000 Posted May 6, 2010 Share Posted May 6, 2010 String quoting dictates whether they're needed. A variable in a single quoted string is no longer a variable, it is interpreted as a literal. Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1053926 Share on other sites More sharing options...
KruSuPhy Posted May 6, 2010 Author Share Posted May 6, 2010 what is a literal? And i did what you guys said, but im getting some other error i can't figure out now.. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id= (5)' at line 1 I don't even know what's in any of my line 1s that could cause an error... Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1053929 Share on other sites More sharing options...
Pikachu2000 Posted May 6, 2010 Share Posted May 6, 2010 what is a literal? Paste this in to a new .php file and run it: <?php $variable = "This is a string in a variable"; echo 'Single quoted: The variable is $variable <br />'; echo 'Single quoted and concatenated: The variable is ' . $variable . '<br />'; echo "Double quoted: The variable is $variable <br />"; echo "Double quoted and concatenated: The variable is " . $variable . "<br />"; ?> And i did what you guys said, but im getting some other error i can't figure out now.. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id= (5)' at line 1 I don't even know what's in any of my line 1s that could cause an error... That's the error returned by MySQL in the mysql_error() part of the die() statement. The error is in the first line of the QUERY string, not the script, and it even tells you (sort of) where it is. Paste up your new code, and I'll have a look at what you have now. Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1053934 Share on other sites More sharing options...
KruSuPhy Posted May 6, 2010 Author Share Posted May 6, 2010 <?php include 'jepconfig.php'; $name=$_POST['secname']; $idnum=$_POST['idnum']; $query="INSERT INTO sections (name) VALUES (".$name.") WHERE id= .$idnum."; $result = mysql_query($query) or die( mysql_error() ); ?> basically what i had before, just with what i was told to change, couldn't see anything else that should be modified... i have five rows, each with an ID from 1 to 5(there will be 5 categories in the game). this should send the category name to 'name' in `sections` where the ID is the same as the field. Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1053935 Share on other sites More sharing options...
Pikachu2000 Posted May 6, 2010 Share Posted May 6, 2010 OK. I need to know just what you're trying to do. Are there already records in the database that you are changing, or are new records being inserted? Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1053936 Share on other sites More sharing options...
KruSuPhy Posted May 6, 2010 Author Share Posted May 6, 2010 already in the database there's the column ID and name, with rows 1 2 3 4 5 for id, and one two three four five for name. Can you tell me how exactly i should assign an id(1-5) to the name that i'm editing? currently i have a form with five text fields for the name of the categories. How would i get a specific id to each text field? If i did wrong what I think i did, I'll feel like a complete moron, so i'd rather not say what i did exactly Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1053937 Share on other sites More sharing options...
Pikachu2000 Posted May 6, 2010 Share Posted May 6, 2010 Believe me, we've all felt like morons at some point. Nobody is born with PHP knowledge. If you have the records in the database already, and you just need to change a field, you need to use an UPDATE query rather than an INSERT query. Are you loading all of these into a form and trying to edit them, or are you doing this some other way? // PSEUDO-CODE - - - - - - - $query = "UPDATE tbl_name SET id_field_name = 1 WHERE name = 'name' "; // note that integer values do not get quoted in a query string, whereas strings do get quoted mysql_query( $query ) or die( mysql_error() ); Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1053941 Share on other sites More sharing options...
KruSuPhy Posted May 6, 2010 Author Share Posted May 6, 2010 Well basically I'm just trying to get a basic jeopardy game with the main page, admin access to change questions and sections, and... that's about it. from there i'll make it pretty with nice features like team setup and all that nonsense. for now i just have a form with text fields that let the person type in text(obv) then sends that text to the database to be received by the main question page. i think. Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1053942 Share on other sites More sharing options...
Pikachu2000 Posted May 6, 2010 Share Posted May 6, 2010 OK. Does what I posted above make sense to you and give you a better idea of how to approach this? Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1053943 Share on other sites More sharing options...
KruSuPhy Posted May 6, 2010 Author Share Posted May 6, 2010 it does, but would i have $query = "UPDATE sections SET id = 1 WHERE name = 'name' "; mysql_query( $query ) or die( mysql_error() );]/php] Quote Link to comment https://forums.phpfreaks.com/topic/200852-jeopardy-game/#findComment-1054287 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.