更新mysql数据库中的记录时出错

时间:2013-05-24 00:24:37

标签: php mysql sql-update

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

警告:mysql_fetch_array()要求参数1为资源,在第232行的C:\ xampp \ htdocs \ test \ edit.php中给出布尔值 错误。您的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>";    

        //}
    }

回声 “中场”;                         $ sql =“SELECT name FROM player”;                         $ data = mysql_query($ sql);                                 while($ rows = mysql_fetch_array($ data)){                                     echo“”;                                     echo $ rows ['name'];                                     echo“”;                                 }

                    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
        }

2 个答案:

答案 0 :(得分:0)

您是否尝试添加mysql_error()以查看您收到的错误消息? 改变这个:

$data = mysql_query($sql);

到此:

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

您收到的消息是该查询的结果是真/假,而不是mysql“资源”。 MySQL资源是mysql查询的正常响应,可以使用mysql_fetch_array或mysql_fetch_assoc等“读取”它们。

因此,如果您获得了真/假响应,那么该特定查询不会为您提供所需的数据。与我一起排除故障:为什么会发生这种情况?


试试这个:

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

另外,回显$ sid的值,这样你就可以看到它里面有东西。 null返回在mysql_fetch_array中也不起作用。


还尝试回显整个$ _POST,以确切了解收到的内容:

echo '<pre>';
print_r($_POST);
echo '</pre>';

答案 1 :(得分:0)

mysql_query()返回结果集(如果有效),或false如果不起作用。您收到mysql_fetch_array()关于使用需要结果集的布尔值的投诉的事实意味着查询已返回false(即,它没有工作)。

Yous应该使用类似的东西:

$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_*已被弃用。