2017-05-20 22 views
-1

私はPHPでソーシャルウェブサイトを作成しています。ユーザーは自分のタイムラインにステータスを投稿するオプションを持っていますが、タイムラインに投稿しようとすると、Post added to your timeline. echoステートメントが表示されますが、データベースのオンラインに移動すると、ユーザー名列に記入され、誰か助けてくれますか?MYSQL PHPカラムが空

profile.php:

<form action="poststatus.php" method="post"> 
<textarea rows="3" cols="25" name="status" id="status"> 
</textarea> 
<button id="bt4" type="submit" name="bts">Post status</button> 
</form> 

poststatus.php:あなたはとてもあなたのコードはそのようになっているはずです、$body変数は、それが価値だを取得する必要がありから定義していない

ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL); 

include("connect.php"); 
include("auth_login.php"); 

// just define at the top of the script index.php 
$username = ''; 
$username = $_SESSION['username']; 

$body = ''; 

if(isset($_POST['bts'])) { 

if (empty($_POST["status"])) { 
    echo"You didn't enter anything . <a href= profile.php>Try again</a>"; 
    } else { 

    $sql = "INSERT INTO posts (username, body) VALUES ('" . $username . "', '" . $body . "')"; 

if(mysqli_query($conn, $sql)){         

echo"<a href= home.php>Post added to your timeline.</a>"; 
} else{ 
    echo "<br>error posting . <br> <a href= profile.php>Try again</a> " . 
mysqli_error($conn); 
} 
} 
} 
+0

が耀輝確認耀輝$のユーザー名と$ bodyの適切な値を持っているしてみてください。 ..挿入する前にvar:dump($ username)を試してください。 – scaisEdge

+1

空の値を挿入しています。この行が変数を何に設定したと思いましたか?: '$ body = '';'このコードは** SQLインジェクション**に広く開かれていて、ユーザーはあなたのサーバー上で任意のコードを実行できます。 – David

+0

それは私にこれを示していると私はそれが意味するものがわからない 'C:\ wamp \ www \ testwebsite \ poststatus.php:14:string 'username264'(長さ= 11) ' – mkd

答えて

1

profile.php:

<form action="poststatus.php" method="post"> 
<textarea rows="3" cols="25" name="status" id="status"> 
</textarea> 
<button id="bt4" type="submit" name="bts">Post status</button> 
</form> 

poststatus.php:それは、MySQL-注射のため危険だ原因

ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL); 

include("connect.php"); 
include("auth_login.php"); 

// just define at the top of the script index.php 
$username = ''; 
$username = $_SESSION['username']; 

$body = ''; 

if(isset($_POST['bts'])) { 

if (empty($_POST["status"])) { 
    echo"You didn't enter anything . <a href= profile.php>Try again</a>"; 
    } else { 
    $body = $_POST["status"]; 
    $sql = "INSERT INTO posts (username, body) VALUES ('" . $username . "', '" . $body . "')"; 

if(mysqli_query($conn, $sql)){         

echo"<a href= home.php>Post added to your timeline.</a>"; 
} else{ 
    echo "<br>error posting . <br> <a href= profile.php>Try again</a> " . 
mysqli_error($conn); 
} 
} 
} 

はまた、あなたが、明確なmyslq_queryを使うべきではありませんが、PDOむしろ

+0

注:これはまだ** SQLインジェクション**へと広がっています*これはライブシステムでは使用しないでください。 – David

+0

私はこのelse文を取得しています。あなたは何も入力しませんでした。もう一度やり直してください。 – mkd

+0

@Davidはい、確かに、私はそれをエディションで言及しました。 彼はむしろPDOか少なくとも 'mysqli_real_escape_string'を使用するべきです –

関連する問題