getting this error submitting a form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chris_s_22
Forum Commoner
Posts: 76
Joined: Wed Dec 31, 2008 2:05 pm

getting this error submitting a form

Post by chris_s_22 »

im getting this error
Warning: Invalid argument supplied for foreach() in /mounted-storage/home74b/sub009/sc42562-IQUU/www/truthdare/Game/functions.php on line 4
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 '' at line 1

Code: Select all

 
<?php
function create_game($playername, $sex) {
        if($_POST) { 
                foreach($_POST['player'] as $post_key) { 
                $query_string .= " ('".$post_key['sex']."', '".$post_key['playername']."'),";
                } 
                $query_string = substr_replace($query_string,"",-1);
        
                // store the information in the database
                $query = "INSERT INTO `game` (`playername`, `sex`) values $query_string";
                $result = mysql_query($query) or die(mysql_error());
 
                // if suceesfully inserted data into database, send confirmation link to email 
                if($result) {
                        header('Location: http://www.pinkangel4u.com/truthdare/Ga ... board.php');
                }
        }}
?> 
please help
christine xxx
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: getting this error submitting a form

Post by it2051229 »

look at this code

Code: Select all

 
$query_string .= " ('".$post_key['sex']."', '".$post_key['playername']."'),";
//blah
//blah
//blah
$query = "INSERT INTO `game` (`playername`, `sex`) values $query_string";
 
this is equal to

Code: Select all

 
$query = "INSERT INTO `game` (`playername`, `sex`) values $query_string ('".$post_key['sex']."', '".$post_key['playername']."'),";
 
you see the error? it has a "," appended character at the end of the sql statement.
Post Reply