2

SQLデータベースからユーザー名を取得してチャットに配置しようとしています。私のデータベースは以下のように構成されています。何をしても、。$ user_info ['username']を実行することでユーザーのユーザー名をエコーできるため、理解できないように機能しません。ただし、fwrite関数内に配置すると、何も表示されません。何が欠けているのですか、なぜユーザー名がfwrite関数内に表示されないのですか?

db http://goawaymom.com/db.jpg

<?
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("root_members") or die(mysql_error());

session_start();
if(isset($_SESSION['name'])){
$text = $_POST['text']; 
$fp = fopen("log.html", 'a');
fwrite($fp, "<div class='msgln'> ".$user_info['username'].">".stripslashes(htmlspecialchars($text))."</div>");
fclose($fp);
}
?>
4

3 に答える 3

2

$user_infoを定義する必要があります。

<?
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("root_members") or die(mysql_error());

session_start();
if(isset($_SESSION['name'])){

// select from database
$result = mysql_query('SELECT * FROM users WHERE username="' . $_SESSION['name'] . '"'); 
// retrieve row
$user_info = mysql_fetch_assoc($result); // fixed this from mysql_fetch_row to mysql_fetch_assoc

$text = $_POST['text']; 
$fp = fopen("log.html", 'a');
fwrite($fp, "<div class='msgln'> ".$user_info['username']." &gt;".stripslashes(htmlspecialchars($text))."</div>");
fclose($fp);
}
?>
于 2012-10-02T04:39:28.463 に答える
2

どこ$user_infoに住んでいますか?それがパズルの欠けている部分のように見えます。また、HTMLで適切にレンダリングするためにエンティティコードが必要な大なり記号も修正しました。

<?php
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("root_members") or die(mysql_error());

session_start();
if (isset($_SESSION['name'])) {
    $sql = sprintf("SELECT username FROM root_members WHERE querygoeshere='%s'",
                   mysql_real_escape_string($querygoeshere));

    $result = mysql_query($sql);
    if (!$result) {
        die('Could not execute query: ' . mysql_error());
    }
    $row = mysql_fetch_row($result);
    $username = $result[0];

    $text = $_POST['text'];
    $fp = fopen("log.html", 'a');
    fwrite($fp, "<div class='msgln'>" . $username . " &gt; " . stripslashes(htmlspecialchars($text)) ."</div>");
    fclose($fp);
}
于 2012-10-02T04:40:54.513 に答える
1

fwriteあなたは問題ないように見えますが>、ユーザー名の出力をフォローすると、タグの終わりとしてブラウザによって混乱する可能性があります。

これを試して:

fwrite($fp, "<div class='msgln'> ".$user_info['username']."&gt;".stripslashes(htmlspecialchars($text))."</div>");
于 2012-10-02T04:20:11.933 に答える