开发者

How to add news in db, and after show them? It's this code ok?

i need again your help...

I have this php code:

<? 

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'/>"; 
}}}

?>

And this form:

<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;displ开发者_JAVA技巧ay: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>

I want to add news in DB, and after to show them on a page. (if you can help with this too will be very very nice from you.

Regards.


First of all, you forgot to tell the form where to send all the data:

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

You have to put the location of the PHP file you posted in between those empty quote marks after 'action='.

Now let's look at your PHP.

First of all, this if statement here.

if($type = '' AND NULL)

Remember that = is the assignment operator, not the equality operator. It should be $type == ''. Secondly, I don't think AND is valid PHP syntax. It should be &&. And thirdly... that if statement will never return true. Why?

Let's say you have this corrected version:

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

In that case, this will return true if $type is an empty string, and if NULL is true. However to PHP, NULL is never true. So this statement will always be false. Perhaps what you wanted to do was check if $type was either empty OR NULL, in which case the syntax is:

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

Next, what's with all the else statements? It's entirely possible that more than one of those if statements could be true. What I think it should be is:

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{ 

Furthermore, I'm not sure you can use the .= operator on a variable that hasn't been initialized. I'd add in $final_report = '' to the beginning of the code just to be safe.

Finally, you have a small error in your SQL:

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

Column names don't take quotes in SQL. It should be:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜