2

ログインしたユーザーがメモを入力して保存できる HTML のテキストエリアがあります。次のスクリプトを使用して、保存したメモをデータベースから取得します

<?php
function notes() {
    $password = "fake_password";
    $connect = mysql_connect("localhost", "user", $password) or die("Couldn't connect to the database!");
    mysql_select_db("db_name") or die("Couldn't find the database!");

    $query = mysql_query("SELECT * FROM users WHERE notes!=''");
    $numrows = mysql_num_rows($query);

    if ($numrows != 0) {
        while ($row = mysql_fetch_assoc($query)){
            $notes = $row['notes'];
        }

        echo $notes;
    }
}
?>

それはすべてうまく機能しますが、今では、[保存] ボタンがクリックされたときにユーザーがメモ (テキストエリア) に加えた変更を保存するにはどうすればよいですか?

編集:

フォーム自体は次のとおりです。

<div class="row-fluid">
    <div class="span12">
        <br />
        <center><textarea class="input-xxlarge" rows="15"><?php include('includes/func.php'); notes(); ?></textarea>
        <br />
        <button class="btn btn-primary">Save Changes</button>
        </center>
    </div>
</div>
4

1 に答える 1

2

最初: mysql ではなく、 PDOを使用してデータベースに接続する必要があります。Mysql は廃止予定であり、 SQL インジェクション攻撃を受けやすいため、Web アプリケーションでの使用は安全ではありません。

その警告で、それがあなたが使用しているものなので、mysqlを使用して返信します。

それほど難しくありません。もちろん、テーブル構造やフォームのコードがない場合、以下ではフィールド名の例を使用する必要があり、ユーザー ID を$_SESSION変数に格納することを前提としています。

<?php
    function savenotes() {
        // The database connection code should be moved to a central function/location rather than called in every function
        $password = "fake_password";
        $connect = mysql_connect("localhost", "user", $password) or die("Couldn't connect to the database!");
        mysql_select_db("db_name") or die("Couldn't find the database!");

        // Retreive notes from form
        $notes = $_POST['notes']; // Assumes field is called notes, and you used method=POST
        // Assuming a user can only update their own notes, and that you are storing the user id somewhere
        $userid = $_SESSION['userid']; 
        $query = mysql_query("UPDATE users SET notes ='" . mysql_real_escape_string($notes) . "' WHERE userid = " . (int)$userid);


}

} ?>

于 2012-10-30T00:20:37.790 に答える