-5

重複の可能性:
警告: mysql_fetch_array(): 指定された引数は有効な MySQL 結果ではありません

ユーザーが自分のアカウント設定を変更できる PHP スクリプトがあります。しかし、見つけられない間違いがあるため、問題があります。助けてください。

警告: mysql_fetch_assoc() は、パラメーター 1 がリソースであると想定します。これは、69 行目の /Applications/XAMPP/xamppfiles/htdocs/tutorials/findFriends/account_settings.php で指定されたブール値です。

<?
include ("inc/incfiles/header.inc.php");
if ($user) {

}
else
{
 die ("You must be logged in to view this page!");
}
?>
<?
$senddata = $_POST['senddata'];

//Password variables
$old_password = strip_tags($_POST['oldpassword']);
$new_password = strip_tags($_POST['newpassword']);
$repeat_password = strip_tags($_POST['newpassword2']);

if ($senddata) {
//If the form has been submitted ...

$password_query = mysql_query("SELECT * FROM users WHERE username='$user'");
while ($row = mysql_fetch_assoc($password_query)) {
    $db_password = $row['password'];

    //md5 the old password before we check if it matches
    $old_password_md5 = md5($old_password);

    //Check whether old password equals $db_password
    if ($old_password_md5 == $db_password) {
     //Continue Changing the users password ...
     //Check whether the 2 new passwords match
     if ($new_password == $repeat_password) {
        if (strlen($new_password) <= 4) {
         echo "Sorry! But your password must be more than 4 character long!";
        }
        else
        {

        //md5 the new password before we add it to the database
        $new_password_md5 = md5($new_password);
       //Great! Update the users passwords!
       $password_update_query = mysql_query("UPDATE users SET password='$new_password_md5' WHERE username='$user'");
       echo "Success! Your password has been updated!";

        }
     }
     else
     {
      echo "Your two new passwords don't match!";
     }
    }
    else
    {
     echo "The old password is incorrect!";
    }
}
 }
else
{
 echo "";
}


$updateinfo = $_POST['updateinfo'];

//First Name, Last Name and About the user query
$get_info = mysql_query("SELECT first_name, last_name, bio FROM users WHERE username='$user'");
$get_row = mysql_fetch_assoc($get_info);
$db_firstname = $get_row['first_name'];
$db_last_name = $get_row['last_name'];
$db_bio = $get_row['bio'];

//Submit what the user types into the database
if ($updateinfo) {
 $firstname = strip_tags($_POST['fname']);
 $lastname = strip_tags($_POST['lname']);
 $bio = $_POST['bio'];


 if (strlen($firstname) < 3) {
echo "Your first name must be 3 more more characters long.";
 }
 else
 if (strlen($lastname) < 5) {
echo "Your last name must be 5 more more characters long.";
}
else
{
//Submit the form to the database
$info_submit_query = mysql_query("UPDATE users SET first_name='$firstname', last_name='$lastname', bio='$bio' WHERE username='$user'");
echo "Your profile info has been updated!";
header("Location: $user");
}
}
else
{
 //Do nothing
}
?>
<h2>Edit your Account Settings below</h2>
<hr />
<form action="account_settings.php" method="post">
<p>CHANGE YOUR PASSWORD:</p> <br />
Your Old Password: <input type="text" name="oldpassword" id="oldpassword" size="40"><br />
Your New Password: <input type="text" name="newpassword" id="newpassword" size="40"><br />
Repeat Password  : <input type="text" name="newpassword2" id="newpassword2" size="40"><br />
<input type="submit" name="senddata" id="senddata" value="Update Information">
</form>
<hr />
<form action="account_settings.php" method="post">
<p>UPDATE YOUR PROFILE INFO:</p> <br />
First Name: <input type="text" name="fname" id="fname" size="40" value="<? echo $db_firstname; ?>"><br />
Last Name: <input type="text" name="lname" id="lname" size="40" value="<? echo $db_last_name; ?>"><br />
About You: <textarea name="bio" id="bio" rows="7" cols="40"><? echo $db_bio; ?></textarea>

<hr />
<input type="submit" name="updateinfo" id="updateinfo" value="Update Information">
</form>
<br />
<br />

姓または名を変更したい場合は、次のエラーが発生します。

警告: ヘッダー情報を変更できません - /Applications/XAMPP/xamppfiles/htdocs/ の (/Applications/XAMPP/xamppfiles/htdocs/tutorials/findFriends/inc/incfiles/header.inc.php:72 で開始された出力) によって既に送信されたヘッダー93 行目の tutorials/findFriends/account_settings.php

header.inc.php は次のとおりです。

<?
include ("inc/scripts/mysql_connect.inc.php");
session_start();
if (isset($_SESSION['user_login'])) {
$user = $_SESSION["user_login"];
}
else {
$user = "";
}
?>
<!DOCTYPE html>

<html lang="en">
<head>
        <meta charset="utf-8">

        <link rel="stylesheet" href="css/reset.css" media="screen">
        <link rel="stylesheet" href="css/master.css" media="screen">
        <link rel="stylesheet" href="css/blue.css" media="screen">
    <link rel="stylesheet" href="css/main.css" media="screen">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
        <script src="js/jquery.color.js"></script>
        <script src="js/script.js"></script>
    <script src="js/placeholder-js.js" type="text/javascript"></script>
    <script src="js/main.js" type="text/javascript"></script>

        <title>findFriends</title>
</head>
<body>
                <div class="mashmenu">
                        <div id="menuWrapper">
                        <div class="fnav">

                                <a href="#" class="flink" >findFriends+ </a>

                                <div class="allContent">

                                        <div class="snav" >
                                                <a href="#" class="slink" >About findFriends</a>

                                                <div class="insideContent">

                                                        <span class="featured" >What is it?<br />
                            findFriends is an open source social network, created for the sinimma YouTube tutorial
                            series. All code is explained and completely free to download and use.<a href="http://www.youtube.com/sinimma">Visit our YouTube
                            channel ...</a>
                            </span>
                                                </div><!-- end insideContent -->
                                        </div><!-- end snav -->

                                        <div class="snav" >
                                                <a href="#" class="slink" >Open Source?</a>
                                                <div class="insideContent">
                                                        <span class="featured" >All code is open source and freely availible,</span>
                                                        because this means that anyone can download and improve the source code whatever
                            way they want without permission. <br />You may use this source code in any way to do anything.
                            All we <br />ask is that you keep the link at the bottom of the page, as a way of saying thanks for
                            all of the work we have done. :)
                                                </div><!-- end insideContent -->
                                        </div><!-- end snav -->

                                        <div class="snav" >
                                                <a href="http://www.youtube.com/sinimma" class="slink" >Youtube Channel</a>
                                                <div class="insideContent">
                                                        <span class="featured" ></span>
                                        </div><!-- end snav -->
                    </div>
                                </div><!-- end allContent -->

                        </div><!-- end fnav -->

                        <?
                        if (isset($_SESSION["user_login"])) {
                        echo '

                        <div class="fnav">

                                <a href="' . $user . '" class="flink" >' . $user . '\'s Profile</a>

                        </div><!-- end fnav -->
                        <div class="fnav">

                                <a href="account_settings.php" class="flink" >Account Settings</a>

                        </div><!-- end fnav -->
                        <div class="fnav">

                                <a href="logout.php" class="flink" >Logout</a>

                        </div><!-- end fnav -->

                        ';
                        }
                        else
                        {
                                echo '

                                <div class="fnav">

                                <a href="index.php" class="flink" >Sign Up+ </a>

                        </div><!-- end fnav -->
            <div class="fnav">

                                <a href="index.php" class="flink" >Login+ </a>

                        </div><!-- end fnav -->

                                ';
                        }
                        ?>

                        <div class="feat">
                                <form id="searchForm">
                <fieldset>
                    <div class="input">
                        <input type="text" class="Search" id="s" value="Search findFriends ..." />
                    </div>
                    <input type="submit" id="searchSubmit" value="" />
                </fieldset>
            </form>
                        </div><!-- end fnav feat -->
          </div>
                </div><!--end mashmenu -->
        <div id="wrapper">
<br />
<br />
<br />
<br />
4

2 に答える 2

2

クエリは失敗します。これは、 "expects parameter 1 to be resource, boolean given"エラー メッセージが表示される場合は常に99.99%です。さらなる呼び出しでクエリ結果を使用しようとするためポップアップしますが、失敗した場合、期待するリソースではなくブール値 ( ) を取得します。false

経験則は次のとおりです。次のようなコードを使用して、クエリが成功したかどうかを常に確認します

$result = mysql_query()
if( $result !== false ) {
   // ok, let's proceed
}

また、コードはSQL インジェクション攻撃に対して非常にオープンです。などのメソッドを使用して、クエリを作成する変数を常に渡しますmysql_real_escape_string()

編集

コードには 1 つのクエリしかないため、以下を置き換えます。

$password_query = mysql_query("SELECT * FROM users WHERE username='$user'");

$password_query = mysql_query( sprintf("SELECT * FROM users WHERE username='%s'",
                       mysql_real_escape_string($user)) );

あなたの問題を解決します。そして、ドロップする必要がありますstrip_tags()。ユーザー入力を破壊しているため、ここで使用するのは適切な場所ではありません。そして最後に、そうではありませ$password_query$password_query_result

mysql_PS:拡張機能を放棄してmysqli_orを使用することを検討してくださいPDOmysql_拡張子は非推奨ですmysqli_ほとんどのドロップイン交換と同様に、痛みを伴う移行ではありません。

于 2012-11-25T20:52:27.710 に答える
0

問題は次のクエリにあります (68 行目):

$get_info = mysql_query("SELECT first_name, last_name, bio FROM users WHERE username='$user'");

このクエリは失敗するため、必要に応じて MySQL リソースの代わりに$get_infoブール値が含まれます。FALSEmysql_fetch_assoc

そして、SQL インジェクションの脆弱性についてスクリプトをチェックしてください - あなたのスクリプトは驚くほど脆弱です!

于 2012-11-25T20:55:17.847 に答える