0

ニュースを追加するための簡単なコードを作成しようとしています...(私の小さなプロジェクト)[送信]をクリックすると、成功のテキストが表示され、ページに再表示されますが、データが追加されておらず、フォームを空のままにすると-エラーメッセージは表示されません。

...そしてmysql_real_escape_stringは私にいくつかの問題を救うつもりですか?

<?php 
include('connect_db.php');
if(isset($_POST['submit']))
{
    $title = mysql_real_escape_string(htmlspecialchars($_POST['title']));
    $text = mysql_real_escape_string(htmlspecialchars($_POST['text']));

    if ($title == '' || $text == '')
    {

         // generate error message
         $error = 'ERROR: Please fill in all required fields!';
    }

    $result = mysql_query("INSERT INTO news ('id', 'date', 'title', 'text')
                           VALUES ('NULL', NOW(),'$title','$text')",$conn);

    echo "<b>Thank you!<br>You'll be redirected in (4) secs...";
    echo "<meta http-equiv=Refresh content=4;url=add.php>";

    } else {

    echo "<form method='post' action='add.php'>
          <legend>Add news</legend>
          <label>Title</label>
          <input type='text' name='title'>
          <label>Text</label>
          <textarea rows='5' name='text'></textarea>
          <br />
          <button type='submit' name='submit' class='btn'>Submit</button>
          </form>";
}?>
4

2 に答える 2

3

NULLはキーワードです。引用符で囲むことはできません。

同様に、フィールド名は`一重引用符ではなくバッククォートで'囲む必要があります。また、一貫性を保つために、フィールド名もバッククォートで囲む必要があります。

idまた、に設定されているように見えるAUTO_INCREMENTので、に設定する必要はありませんNULLdateTIMESTAMP(である必要があります)の場合は、それを設定DEFAULT CURRENT_TIMESTAMPしてクエリから削除することもできます。

$result = mysql_query("INSERT INTO `news` (`title`,`text`) VALUES ('$title','$text')");
于 2012-08-23T19:14:11.047 に答える
0

あなたのコードは少し未完成です。

<?php 

// connect to the database
include('connect_db.php');


if(isset($_POST['submit']))

{

    // htmlspecialchars is needed when displaying HTML to the user from an input, not for inserting into a database. mysql_real_escape_string is plenty for this purpose.

    $title = mysql_real_escape_string($_POST['title']);
    $text = mysql_real_escape_string($_POST['text']);

    if ($title == '' || $text == '')

    {

        // You have generated an error but you are not displaying it anywhere.

        // generate error message
        echo 'ERROR: Please fill in all required fields!';

       // You will want to either send the error in a query string to this page again and display it above the form or re-echo the form here.

    }else{

        // Don't submit the data if there is an error.

        // ID should be auto-increment in your database, don't set it here even if you set it NULL, you can also have MySQL apply the current time rather than here.

        $result = mysql_query("INSERT INTO news (`title`, `text`)
        VALUES ('$title','$text')");

        echo "<b>Thank you!<br>You'll be redirected in (4) secs...";
        echo "<meta http-equiv=Refresh content=4;url=add.php>";

    }

} else {

    echo "<form method='post' action='add.php'>
    <legend>Add news</legend>
    <label>Title</label>
    <input type='text' name='title'>
    <label>Text</label>
    <textarea rows='5' name='text'></textarea>
    <br />
    <button type='submit' name='submit' class='btn'>Submit</button>
    </form>";

}
?>
于 2012-08-23T19:23:37.200 に答える