0

I am updating a mysql table. i am getting an error as below

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test\edit.php on line 232 Error. 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

The query seems not to be producing a result. I am passing an id to a function via a url but the variable seems to die although it seems to be in scope. What could be my error. The loop for the update is below. i have commented out some lines which i thought where a problem but they are fine. code in bold are the problem lines.

elseif(isset($_POST['editSelection']))
{ 
    // check if form is submitted
    //collect variables posted by form.
    $fixture_id = mysql_real_escape_string($_POST['fixture_id']);
    $goalkeeper = mysql_real_escape_string($_POST['goalkeeper']);
    $defender = mysql_real_escape_string($_POST['defender']);
    $fullback = mysql_real_escape_string($_POST['fullback']);
    $midfielder = mysql_real_escape_string($_POST['midfielder']);
    $wing = mysql_real_escape_string($_POST['wing']);
    $striker = mysql_real_escape_string($_POST['striker']);
    $sid = mysql_real_escape_string($_POST['sid']); // receive the selection_id which was posted from the hidden field in the editForm

    $sql = "SELECT * FROM `selections` WHERE selection_id = {$sid}";
    $data = mysql_query($sql);

   **while($rows = mysql_fetch_array($data))
     {
        $opponents = $rows['opponents'];
     }**

    //validate form by checking for empty strings that user might have submitted using strlen() php built-in method. If no empty string form processes
            //if(strlen($fixture_id)>0 && strlen($goalkeeper)>0 && strlen($defender)>0 && strlen($fullback)>0 && strlen($midfielder)>0 && strlen($wing)>0  && strlen($striker)>0 && strlen($selection_id)>0) {  // if form fields are not empty, update Selection record in database

    $sql = "UPDATE `selections` SET goalkeeper ='{$goalkeeper}' WHERE selection_id = {$sid}";                               
    $query = mysql_query($sql) or die("Error executing query ".mysql_error());
                            echo "Selection updated <br/><br/>";    
                            echo "<a href=\"team_selections.php\">Go back to Team Selections page </a>";    

        //}
    }


echo"<tr><td>Midfielder</td><td><select name=\"midfielder\">";
                    $sql = "SELECT name FROM `player` ";
                    $data = mysql_query($sql);
                            while($rows = mysql_fetch_array($data)){
                                echo "<option value={$rows['name']}>";
                                echo $rows['name'];
                                echo "</option>";
                            }

                    echo "</select>";
                    echo "</td></tr>";


                    echo"<tr><td>Wing</td><td><select name=\"wing\">";
                    $sql = "SELECT name FROM `player` ";
                    $data = mysql_query($sql);
                            while($rows = mysql_fetch_array($data)){
                                echo "<option value={$rows['name']}>";
                                echo $rows['name'];
                                echo "</option>";
                            }

                    echo "</select>";
                    echo "</td></tr>";

                    echo"<tr><td>Striker</td><td><select name=\"striker\">";
                    $sql = "SELECT name FROM `player` ";
                    $data = mysql_query($sql);
                            while($rows = mysql_fetch_array($data)){
                                echo "<option value={$rows['name']}>";
                                echo $rows['name'];
                                echo "</option>";
                            }

                    echo "</select>";
                    echo "</td></tr>";


                    echo "<tr><td></td><td><input type=\"hidden\" value=\"{$rows['selection_id']}\" name=\"sid\"></td></tr>"; // create hidden field with selection_id which enables the right selection to be edited
                    echo "<tr><td></td><td><input type=\"submit\" value=\"Update Selection\" name=\"editSelection\"></td></tr>";

                    echo "</table></form>";
            } //end of while loop
        }
Dharman
  • 30,962
  • 25
  • 85
  • 135
Charles
  • 25
  • 1
  • 2
  • 8

2 Answers2

0

Have you tried adding mysql_error() to see what error message you are getting? Change this:

$data = mysql_query($sql);

to this:

$data = mysql_query($sql) or die(mysql_error());

The message you are receiving is saying that the result of that query is a true/false, not a mysql "resource". MySQL resources are the normal response of a mysql query, and they can be "read" by using mysql_fetch_array or mysql_fetch_assoc, etc.

So if you are getting a true/false response, then that particular query isnt giving you the data you desire. Troubleshoot with me: why is that happening?


Try this:

"SELECT * FROM `selections` WHERE `selection_id` = '$sid'";

Also, echo out the value of $sid so you can see that it has something in it. A null return will also not work in mysql_fetch_array.


Also try echoing out the entire $_POST to see exactly what is being received:

echo '<pre>';
print_r($_POST);
echo '</pre>';
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • could it be that my_real_escape() has some complications on values? – Charles May 24 '13 at 00:35
  • if i substitute $sid with a value of a record the record gets updated same time, e.g if i put 30 instead of $sid, record 30 gets updated but when i use the variable i sent via url i get the error. – Charles May 24 '13 at 00:37
  • Yes, that sounds a lot like the value of $sid is the problem. As I mentioned above, try echoing out $sid and see what its value is. Then look at how that value is being transmitted. Might just be a simply spelling / typo error...? – cssyphus May 24 '13 at 00:39
  • using '$sid' i get this error "Error executing query 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" – Charles May 24 '13 at 00:40
  • i echoed $sid before the while loop and its empty, no result displayed. I also echoed "hi ".$sid and hi was printed – Charles May 24 '13 at 00:42
  • Ok, so $sid is not getting POSTed in. Try at the top echoing the entire $_POST var. I'll add code to my answer above. – cssyphus May 24 '13 at 00:47
0

mysql_query() returns a result set if it works, or false if it doesn't work. The fact that you're getting complaints from mysql_fetch_array() about using a boolean where a result set is required means that the query has returned false (ie, it hasn't worked).

Yous should use something like:

$data = mysql_query($sql)  or die (mysql_error());

to see what the actual error is though I'd be looking at something a little more robust in production code. Still, that should be enough to identify the immediate problem.

You may also want to actually output the query before trying to execute it in case, for example, there's a problem with $sid, such as it being empty, or it being a string where your query seems to desire a numeric value.

If it is a string, you'll want to surround {$sid} with single quote marks:

$sql = "SELECT * FROM selections WHERE selection_id = '{$sid}'";

If it's empty, you'll need to track down why, since that will give you the invalid query:

SELECT * FROM selections WHERE selection_id =

And, of course, you should be moving to the mysqli_* functions where possible since the mysql_* ones are deprecated.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Array ( [selection_id] => 34 [fixture_id] => 2 [goalkeeper] => Charlie [defender] => Charlie [fullback] => Charlie [midfielder] => Charlie [wing] => Charlie [striker] => Charlie [sid] => [editSelection] => Update Selection ) Error executing query 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 – Charles May 24 '13 at 00:52
  • There, you can see that $sid is empty. Please post your HTML for that hidden field and let's see why it is empty. – cssyphus May 24 '13 at 00:53
  • @gibberish $sql = "SELECT name FROM `player` "; $data = mysql_query($sql); while($rows = mysql_fetch_array($data)){ echo ""; } echo ""; echo ""; echo ""; // create hidden field with selection_id which enables the right selection to be edited – Charles May 24 '13 at 00:57
  • @gibberish is right, $sid is empty, no value is passed, i think my `value=\"{$rows['selection_id']}\"` is out of scope – Charles May 24 '13 at 00:59
  • Sorry Charles, would you please add this to your original question at top so the formatting is easier to read? – cssyphus May 24 '13 at 00:59
  • sorry about that. i"ll do that. still new on the forum, finding my way – Charles May 24 '13 at 01:00
  • @gibberish i figured it out. i removed the `$rows[selection_id]` as a value and just put $selection_id which is the variable that was sent by the URL initially. I guess that is the only way i can make it work. Really thanks for the echo of a POST. really showed me where the error was – Charles May 24 '13 at 01:14
  • Thanks. Please mark answer correct if it was the best one to help you find your solution. – cssyphus May 24 '13 at 01:33