2011-06-26 3 views
0

私はこのPHPコードが...再びDBにニュースを追加する方法と、それらを表示した後に?このコードは大丈夫ですか?

をあなたの助けが必要:私はDBにニュースを追加したい

<div style="width:100%; text-align:left; overflow:visible; margin-top:11px; margin-bottom:7px;" id="regpage">   
<form action="" method="post"> 
<fieldset style="border:none;"> 

<label for="news" style="font-weight:normal;width:30%;float:left;display:block;"><p>this is the news field, you can write here:</p></label> 
<textarea rows="3" cols="104" name="news"></textarea></br></br> 

Choose news type: <select name="type" style="margin-top:5px;"> 
<option>1</option> 
<option>2</option> 
<option>2</option> 
</select> 

<input type="submit" name="addnews" value="add new news!" id="addnews" style="float:right; border:1px solid #999; background:#E4E4E4; 
margin-top:5px; padding-bottom:2px;"/> 

</fieldset> 
</form> 
</div> 

、と示すために後:

<? 

if(isset($_POST['addnews'])){ 
$type = $_POST['type']; 
$news = $_POST['news']; 
$date = date("d-m-Y"); 
$memip = $_SERVER['REMOTE_ADDR']; 

if($news == NULL){ 
$final_report.= "ALERT - Please enter something in the field with the news!"; 
}else{ 
if($type = '' AND NULL){ 
$final_report.= "ALERT - Please choose something from the checklist or you will die in pain!"; 
}else{ 
$create_news = mysql_query("INSERT INTO `lisnews` (`id`,`type`,`news`,`date`,`ip`) VALUES('','$type','$news','$date','$memip')"); 
$final_report.="<meta http-equiv='Refresh' content='0; URL=../admin/add-news.php'/>"; 
}}} 

?> 

そして、このフォームをそれらはページ上にあります。 。。(あなたはこれがあまりにもあなたから非常に非常に素晴らしいだろうと助けることができれば、すべての

よろしく

答えて

1

まず、あなたはすべてのデータの送信先のフォームを伝えるのを忘れて:

<form action="" method="post"> 

あなたは「=アクション」の後に、これらの空の引用符の間に掲示PHPファイルの場所を配置する必要があります。

今度はあなたのPHPを見てみましょう。

まず第一に、このsの場合ここでの発言。

if($type = '' AND NULL) 

=は等価演算子ではなく、代入演算子であることに注意してください。 $ type == ''でなければなりません。第二に、私はANDが有効なPHP構文ではないと思います。それは& &でなければなりません。そして、第3に、if文が真実を返すことはないでしょう。どうして?

のは、あなたがこの修正バージョンがあるとしましょう:$タイプは空の文字列である場合、そしてNULLがtrueの場合、その場合には

if($type == '' && NULL) 

が、これはtrueを返します。 PHPでは、NULLは決して真実ではありません。したがって、このステートメントは常に偽です。 $ typeが空またはnullのいずれかであったならば、おそらくチェックは何をやってみたかった、その場合、構文は次のとおりです。

if($type == '' || $type == NULL) 

次に、他のすべての文とは何ですか?これらのif文のうちの複数のものが真実である可能性は完全に可能です。私はそれがあるべきだと思うことはある:。

さらに
if($news == NULL){ 
    $final_report.= "ALERT - Please enter something in the field with the news!"; 
    if($type = '' AND NULL){ 
    $final_report.= "ALERT - Please choose something from the checklist or you will die in pain!"; 
    } 
}else{ 

、私はあなたが初期化されていない変数に=演算子を使用することができるかわかりません。私は$ final_report = ''をコードの先頭に追加するだけで安全です。

最後に、あなたはあなたのSQLに小さな誤差があります

"INSERT INTO `lisnews` (`id`,`type`,`news`,`date`,`ip`) VALUES('','$type','$news','$date','$memip')" 

列名は、SQLで引用符を取ることはありません。それは次のようになります。

"INSERT INTO lisnews (id,type,news,date,ip) VALUES('','$type','$news','$date','$memip')" 
関連する問題