0

我正在更新一个 mysql 表。我收到如下错误

警告:mysql_fetch_array() 期望参数 1 是资源,布尔值在 C:\xampp\htdocs\test\edit.php 第 232 行错误中给出。您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '' 附近使用正确的语法

该查询似乎没有产生结果。我正在通过 url 将 id 传递给函数,但变量似乎已死,尽管它似乎在范围内。什么可能是我的错误。更新循环如下。我已经注释掉了一些我认为有问题的行,但它们很好。粗体代码是问题行。

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
        }
4

2 回答 2

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>';
于 2013-05-24T00:31:40.733 回答
0

mysql_query()如果有效或false无效,则返回结果集。您收到mysql_fetch_array()有关在需要结果集的情况下使用布尔值的投诉这一事实意味着查询已返回false(即,它没有工作)。

你应该使用类似的东西:

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

看看实际的错误什么,虽然我会在生产代码中看一些更健壮的东西。不过,这应该足以确定眼前的问题。

您可能还希望在尝试执行查询之前实际输出查询,以防万一,例如,$sid它是空的,或者它是一个字符串,您的查询似乎需要一个数值。

如果它一个字符串,您需要{$sid}用单引号括起来:

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

如果它是空的,你需要找出原因,因为这会给你无效的查询:

SELECT * FROM selections WHERE selection_id =

而且,当然,您应该mysqli_*尽可能使用这些功能,因为这些功能mysql_*已被弃用。

于 2013-05-24T00:41:29.927 回答