Updating a dB
Hey...I have some PHP code, that uses a case statement to decide which to use (based on a field called 'group'), and then is supposed to update all the fields in that row, except for 'group'
it says I don't have the right syntax (near UPDATE), and I couldn't figure out what I had wrong
Could you guys help me? Here's the code:
<?php
/* Get input */
$group = $_POST['group'];
$name = $_POST['name'];
$time = $_POST['time'];
$year = $_POST['year'];
$event = $_POST['event'];
/* Find right case */
switch ($group) {
case '6 & Under Girls':
/* Replace */
$query = mysql_query(\"UPDATE Under6Girls SET group = '$group', name = '$name', time = '$time', year = '$year' WHERE event = '$event'\") or die(mysql_error());
break;
case '6 & Under Boys':
/* Replace */
$query = mysql_query(\"UPDATE Under6Boys SET group = '$group', name = '$name', time = '$time', year = '$year' WHERE event = '$event'\") or die(mysql_error());
break;
case '8 & Under Girls':
/* Replace */
$query = mysql_query(\"UPDATE Under8Girls SET group = '$group', name = '$name', time = '$time', year = '$year' WHERE event = '$event'\") or die(mysql_error());
break;
case '8 & Under Boys':
/* Replace */
$query = mysql_query(\"UPDATE Under8Boys SET group = '$group', name = '$name', time = '$time', year = '$year' WHERE event = '$event'\") or die(mysql_error());
break;
case '9-10 Girls':
/* Replace */
$query = mysql_query(\"UPDATE 910Girls SET group = '$group', name = '$name', time = '$time', year = '$year' WHERE event = '$event'\") or die(mysql_error());
break;
case '9-10 Boys';
/* Replace */
$query = mysql_query(\"UPDATE 910Boys SET group = '$group', name = '$name', time = '$time', year = '$year' WHERE event = '$event'\") or die(mysql_error());
break;
case '11-12 Girls';
/* Replace */
$query = mysql_query(\"UPDATE 1112Girls SET group = '$group', name = '$name', time = '$time', year = '$year' WHERE event = '$event'\") or die(mysql_error());
break;
case '11-12 Boys';
/* Replace */
$query = mysql_query(\"UPDATE 1112Boys SET group = '$group', name = '$name', time = '$time', year = '$year' WHERE event = '$event'\") or die(mysql_error());
break;
case '13-14 Girls';
/* Replace */
$query = mysql_query(\"UPDATE 1314Girls SET group = '$group', name = '$name', time = '$time', year = '$year' WHERE event = '$event'\") or die(mysql_error());
break;
case '13-14 Boys';
/* Replace */
$query = mysql_query(\"UPDATE 1314Boys SET group = '$group', name = '$name', time = '$time', year = '$year' WHERE event = '$event'\") or die(mysql_error());
break;
case '15-18 Girls';
/* Replace */
$query = mysql_query(\"UPDATE 1518Girls SET group = '$group', name = '$name', time = '$time', year = '$year' WHERE event = '$event'\") or die(mysql_error());
break;
case '15-18 Boys';
/* Replace */
$query = mysql_query(\"UPDATE 1518Boys SET group = '$group', name = '$name', time = '$time', year = '$year' WHERE event = '$event'\") or die(mysql_error());
break;
};
/* Disconnect */
disconnect_db();
echo \"Done.\";
?>
Thanks
Busy posted this at 22:25 — 24th May 2004.
He has: 6,151 posts
Joined: May 2001
try something like group = '"$group"' (single quote, double quote - double single)
You could also make your life easier by just having one database query at the bottom and having the database table a switched variable
$query = mysql_query("UPDATE $whatwhere SET group = .....
$whatwhere being the variable that changes depending on the switch
You might want to add a default or if empty check to it though to be safe
Greg K posted this at 23:55 — 24th May 2004.
He has: 2,145 posts
Joined: Nov 2003
I would agree with Busy on using the switch to change a variable, and then do one single query at the bottom. However, I would put the full sql statement into a variable iself, and then do
$query = mysql_query($sql);
' Also, right before this line, place something like<?php
echo \"<p>SQL:\" . $sql . \"</p>\n\";
?>
So many times I see people who have problems getting a query, insert or update to work, yet they do not actually look at the final sql statement that is sent to the server. Adding this line will let you see this.
Next step after that, if it looks like everything is formatted ok, manually execute the sql statement on the server (either command line, or through something like phpMyAdmin), as this will usually give you a more detailed responce to the error. Plus then you can start breaking down the sql statements to see which part isn't working, executing just parts.
ie. test the WHERE clause by doing a simple
SELECT * FROM {dbname} WHERE event = {Whatever data the printed SQL statement showed you}
'From here you can see if there were actually any matching rows to update.Since yours is an error about syntax, I'm guessing just seeing the actual SQL statement will reviel the answer. Here is how I build sql statements:
<?php
$sql = \"UPDATE 910Girls SET \";
$sql .= \"group = '\" . $group . \"', \";
$sql .= \"name = '\" . $name . \"', \";
$sql .= \"time = '\" . $time . \"', \";
$sql .= \"year = '\" . $year . \"' \";
$sql .= \"WHERE event = '\" . $event. \"'\";
?>
It makes it a little easier when writing, and you can notice more quickly whne you mis a quote becasue one row will stand out a little more.
Hope these tips help.
-Greg
PS. Don't forget to validate the input, and use addslashes to protect the names in case they have quotes, apostrophes, or other characters in them (see http://www.php.net/addslashes)
kb posted this at 19:49 — 25th May 2004.
He has: 1,380 posts
Joined: Feb 2002
OK, now it switches a variable, and sends to a general update string, and the update string has the double single single double quoting. That got rid of the syntax error.
Now, I get a parse error on line 66, which happens to be:
<?php
$query = mysql_query(\"UPDATE\" . $location . \"SET group = '\"$group\"', name = '\"$name\"', time = '\"$time\"', year = '\"$year\"' WHERE event = '\"$event\"'\") or die(mysql_error());
?>
I tried it as "UPDATE $location SET ..." and the way it is now. Whats up now? Thanks.
Suzanne posted this at 19:58 — 25th May 2004.
She has: 5,507 posts
Joined: Feb 2000
You have unequal quotation marks.
"$group" should be " . $group . " in this case (and all other double quoted variables).
kb posted this at 20:43 — 25th May 2004.
He has: 1,380 posts
Joined: Feb 2002
That doesn't seem to solve the problem, I just get more syntax errors, involving the same stuff
ShaneS posted this at 21:01 — 25th May 2004.
They have: 93 posts
Joined: Jun 2003
I am going to give you the ABSOLUTE correct method. Are yours requires registered globals on (I may have that confused, pretty sure im right with that "rg" statement).
$sql ="UPDATE $location SET group = '{$_POST['group']}', name = '{$_POST['name']}', time = '{$_POST['time']', year = '{$_POST['year']}' WHERE event = '{$_POST['event']}'";
$query = mysql_query($sql) or die(mysql_error());
Note: The above code assumed you are submitting a form via the "post" method and all the variables EXECEPT $location are coming from that form and are given name enclosed in [''] ie $_POST['foo']. The curly braces allow for single quote to be utilized in the SQL statement without breaking it.
[Design Alpha] -Web Services : Design,Hosting,Advertising,Software
Ask about custom pricing on hosting!!
Site Assets: [UltraGaming.com] [Blades of Warcraft]
Greg K posted this at 14:07 — 26th May 2004.
He has: 2,145 posts
Joined: Nov 2003
If you were to break up the SQL statement and assigned it to a variable as I gave an example above, then php would have only choked on one section, thus helping you identify which section it is having trouble with...
Addtionally, in line you gave above that is still giving you errors, note that you will also have problems with the actual SQL being sent becasue of the missing spaces. Say the variable $location was Under6Girls, then your sql statement would (in part) look like:
UPDATEUnder6GirlsSET group...
' instead ofUPDATE Under6Girls SET group...
'Again, this is where having the sql in a variable, and then output the content of that variable (while testing the script) before the line that isexecuting the sql statement would help you catch the problem.
-Greg
kb posted this at 02:06 — 1st June 2004.
He has: 1,380 posts
Joined: Feb 2002
Alright...exams are over I have time now to do this
I did it the way Greg suggested, concatenating the strings to create a sql statement:
<?php
$sql = \"UPDATE \" . $location .\" SET \";
$sql .= \"group = '\" . $group . \"', \";
$sql .= \"name = '\" . $name . \"', \";
$sql .= \"time = '\" . $time . \"', \";
$sql .= \"year = '\" . $year . \"' \";
$sql .= \"WHERE event = '\" . $event. \"'\";
$query = mysql_query($sql) or die(mysql_error());
?>
and there error I get is this:
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 'group = '6 & Under Girls', name = 'Test', time = '5:15.11', yea
'Where does that "yea" come from? GRAHGH! This is frustrating me...
Suzanne posted this at 03:24 — 1st June 2004.
She has: 5,507 posts
Joined: Feb 2000
Don't use reserved characters? & for instance?
kb posted this at 03:59 — 1st June 2004.
He has: 1,380 posts
Joined: Feb 2002
that would help, but i'm testing it out under Under6Girls...so that's not the problem...but i'll fix it anyways
Greg K posted this at 05:12 — 1st June 2004.
He has: 2,145 posts
Joined: Nov 2003
The "yea" was most likely it cutting off the SQL statement in the middle of the "year = '..." part.
What is the structure of your database? What are the field types?
Try just for testing to remove the & and then see if it works. If it works then, you will know that is the problem. If you still get the error without the &, then that works, and something else must be wrong, but that is where I would start first.
-Greg
Busy posted this at 10:13 — 1st June 2004.
He has: 6,151 posts
Joined: May 2001
write & like this instead amp;
the gaps in the names could be a problem too
ShawnK posted this at 04:19 — 20th June 2004.
He has: 19 posts
Joined: Jun 2004
lol...
The problem is from 9-10 year old boys and down you put a ; instead of a : after the case.
kb posted this at 16:48 — 1st July 2004.
He has: 1,380 posts
Joined: Feb 2002
ok, i have spent some time on this, and modified it a little. it still doesnt work.
heres the new php:
<?php
/* Get input */
$group = $_POST['group'];
$name = addslashes($_POST['name']);
$time = addslashes($_POST['time']);
$year = $_POST['year'];
$event = $_POST['event'];
/* Find right case */
switch ($group) {
case 'Under6Girls':
$location = \"Under6Girls\";
break;
case 'Under6Boys':
$location = \"Under6Boys\";
break;
case 'MixedUnder6':
$location = \"relay\";
$event = \"100 Free Mixed Under6\";
break;
case 'Under8Girls':
$location = \"Under8Girls\";
break;
case 'Under8Boys':
$location = \"Under8Boys\";
break;
case 'MixedUnder8':
$location = \"relay\";
$event = \"100 Free Mixed Under8\";
break;
case '910Girls':
$location = \"910Girls\";
if ($event = \"200 Free Relay\") {
$location = \"relay\";
$event = \"200 Free Girls 910\";
};
break;
case '910Boys':
$location = \"910Boys\";
if ($event = \"200 Free Relay\") {
$location = \"relay\";
$event = \"200 Free Boys 910\";
};
break;
case '1112Girls':
$location = \"1112Girls\";
if ($event = \"200 Free Relay\") {
$location = \"relay\";
$event = \"200 Free Girls 1112\";
};
break;
case '1112Boys':
$location = \"1112Boys\";
if ($event = \"200 Free Relay\") {
$location = \"relay\";
$event = \"200 Free Boys 1112\";
};
break;
case '1314Girls':
$location = \"1314Girls\";
if ($event = \"200 Free Relay\") {
$location = \"relay\";
$event = \"200 Free Girls 1314\";
};
break;
case '1314Boys':
$location = \"1314Boys\";
if ($event = \"200 Free Relay\") {
$location = \"relay\";
$event = \"200 Free Boys 1314\";
};
break;
case '1518Girls':
$location = \"1518Girls\";
break;
case '1518Boys':
$location = \"1518Boys\";
break;
case 'Mixed1518':
$location = \"relay\";
$event = \"200 Free Mixed 1518\";
break;
};
/* Update */
$sql = \"UPDATE \" . $location . \" SET \";
$sql .= \"name = '\" . $name . \"', \";
$sql .= \"time = '\" . $time . \"', \";
$sql .= \"year = '\" . $year . \"' \";
$sql .= \"WHERE event = '\" . $event. \"'\";
$query = mysql_query($sql) or die(mysql_error());
/* Disconnect */
disconnect_dbh();
echo \"Done. <a href=\\"javascript:go_back()\\">Go Back</a>\";
?>
You can see the page at dscpool.com/records
Busy posted this at 21:47 — 1st July 2004.
He has: 6,151 posts
Joined: May 2001
have you tried echoing your values after the switch statement, or echoing the $sql value before the query
kb posted this at 14:58 — 7th July 2004.
He has: 1,380 posts
Joined: Feb 2002
Yes, i just did...and here's the results
UPDATE SET name = 'test', time = '5:25.11', year = '1235' WHERE event = '25 Free'
'Busy posted this at 05:39 — 16th July 2004.
He has: 6,151 posts
Joined: May 2001
your missing the database table name, $location isn't working
Abhishek Reddy posted this at 03:55 — 17th July 2004.
He has: 3,348 posts
Joined: Jul 2001
Yeah. In fact, the whole switch() wasn't working -- the data sent to $group was malformed. The form fields were contructed as: descriptive_text. It needed to be value="intended_code">descriptive_text. Otherwise, the default $group value was descriptive_text.
Additionally, the if() statements were using the assignment symbol (=), not the comparison (==) operator.
Just for reference.
vexcom posted this at 05:16 — 16th July 2004.
He has: 21 posts
Joined: Jul 2004
Not sure if this will help at all, but i get problems whenever i use column names that are also SQL reserved functions or language reserved words.
For example you should avoid naming columns "group" because SQL gets confused when it sees the word group, Ive had similar problems with USER, URL and someother weird ones. When in doubt add My.. so try MyGroup or Group1 as the column name...
This may not help in this case, but its good practice yo
GL!
- oh, one more thing, try breakingit down to the simplest esence, like one case, or use hardcodd values before you start using cariables make sure you basic SQL code is solid...
- Wesley E. Warren, CEO - Vexcom Advanced Systems Development
Realty-SEO Real Estate Marketing
Freelance Graphic and Web Designers Directory - Join Free
"All limitations are self imposed" ~ Malaclypse The Younger
kb posted this at 20:00 — 17th July 2004.
He has: 1,380 posts
Joined: Feb 2002
Yeah it works...thanks to abhishek
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.