Jump to content

news page


TheCatapult
 Share

Recommended Posts

Hi, It's good that W3Schools Forums has back! I have searched a PHP code for News Page and found this to be easy and simple. But I'm having problem because this has a problem I can't fix myself. Would anyone help me to fix this? Here's the code. add.php

<html><head><title>Add News</title><meta http-equiv="Content-Type" content="text/html; charset="iso"-8859-1"></head><body><?$name = $_POST["name"];   $link = @mysql_connect("", "", "");   if(!$link){	  echo('Error connecting to the database: ' . $mysql_error());	  exit();   }   $db = @mysql_select_db('');   if(!$db){	  echo('Error selecting database: ' . $mysql_error());	  exit();     $query = "INSERT INTO news (name, email, headline, story, timestamp) VALUES ('" . mysql_real_escape_string($name) . "', "' . mysql_real_escape_string($email) . "', '" . mysql_real_escape_string($headline) . "', '" . mysql_real_escape_string($story) . "', NOW())";   if(!$result){	  echo('Error adding news: ' . $mysql_error());	  exit();   }else{   mysql_close($link);   echo('Success!<br><a href="add.php">Click here</a> to add more news.<br><a href="edit.php">Click here</a> to edit news.<br><a href="../index.php">Click here</a> to return to the main page.');   }}else{?><form name="form1" method="post" action="<? echo $PHP_SELF; ?>">  <table width="50%" border="0" cellspacing="0" cellpadding="0">    <tr>	  <td width="50%">Name</td>	  <td><input name="name" type="text" id="name"></td>    </tr>    <tr>	  <td>Email</td>	  <td><input name="email" type="text" id="email"></td>    </tr>    <tr>	  <td> </td>	  <td> </td>    </tr>    <tr>	  <td>Headline</td>	  <td><input name="headline" type="text" id="headline"></td>    </tr>    <tr>	  <td>News Story</td>	  <td><textarea name="story" id="story"></textarea></td>    </tr>    <tr>	  <td colspan="2"><div align="center">		  <input name=$_POST["hiddenField"] == "add_n") {type="hidden" value="add_n">		  <input name="add" type="submit" id="add" value="Submit">	    </div></td>    </tr>  </table>  </form><? } ?></body></html>

front.php

<html><head><title>View News</title><meta http-equiv="Content-Type" content="text/html; charset="iso"-8859-1"></head><body><?$link = @mysql_connect("", "", "");if(!$link){   echo('Error connecting to the database: ' . mysql_error());   exit();}$db = @mysql_select_db('');if(!$db){   echo('Error selecting database: ' . mysql_error());   exit();}$query = "SELECT id, headline, timestamp FROM news ORDER BY timestamp DESC";$result = @mysql_query($query);if(!$result){   echo('Error selecting news: ' . mysql_error());   exit();}if (mysql_num_rows($result) > 0){    while($row = mysql_fetch_object($result))    {    ?>   <font size="-1"><b><? echo $row->headling; ?></b> <i><? echo formatDate($row->timestamp); ?></i></font>    <?    }}else{   ?>   <font size="-2">No news in the database</font><? }   mysql_close($link); ?></body></html>

edit.php

<html><head><title>Edit News</title><meta http-equiv="Content-Type" content="text/html; charset="iso"-8859-1"></head><body><?if(!isset($a){$link = @mysql_connect("", "", "");if(!$link){   echo('Error connecting to the database: ' . $mysql_error());   exit();}$db = @mysql_selectdb('');if(!$db){   echo('Error selecting database: ' . $mysql_error());   exit();}$query = "SELECT id, headline, timestamp FROM news ORDER BY timestamp DESC";$result = @mysql_query($query);if(!$result){   echo('Error selecting news: ' . $mysql_error());   exit();}if (mysql_num_rows($result) > 0){    while($row = mysql_fetch_object($result))    {    ?>   <font size="-1"><b><? echo $row->headling; ?></b> <i><? echo formatDate($row->timestamp); ?></i></font>   <br>   <font size="-2"><a href="edit.php?a=edit&id=<? echo $row->id; ?>">edit</a> |   <a href="edit.php?a=delete&id=<? echo $row->id; ?>">delete</a></font>    <?}else{   ?>   <font size="-2">No news in the database</font><? }   mysql_close($link);}elseif($a == 'edit'){if(!isset($update)){$link = @mysql_connect(localhost, username, password);if(!$link){   echo('Error connecting to the database: ' . $mysql_error());   exit();}$db = @mysql_selectdb('mydatabase');if(!$db){   echo('Error selecting database: ' . $mysql_error());   exit();}$query = "SELECT name, email, headline, story FROM news WHERE id = '$id'";$result = @mysql_query($query);if(!$result){   echo('Error selecting news item: ' . $mysql_error());   exit();}mysql_fetch_object($result);?><form name="form1" method="post" action="edit.php?a=edit&id=<? echo($id) ?>&update=1">  <table width="50%" border="0" cellspacing="0" cellpadding="0">    <tr>	  <td width="50%">Name</td>	  <td><input name="name" type="text" id="name" value="<? echo($row->name) ?>"></td>    </tr>    <tr>	  <td>Email</td>	  <td><input name="email" type="text" id="email" value="<? echo($row->email) ?>"></td>    </tr>    <tr>	  <td> </td>	  <td> </td>    </tr>    <tr>	  <td>Headline</td>	  <td><input name="headline" type="text" id="headline" value="<? echo($row->headline) ?>"></td>    </tr>    <tr>	  <td>News Story</td>	  <td><textarea name="story" id="story" value="<? echo($row->story) ?>"></textarea></td>    </tr>    <tr>	  <td colspan="2"><div align="center">		  <input name="hiddenField" type="hidden" value="update">		  <input name="add" type="submit" id="add" value="Update">	    </div></td>    </tr>  </table>  </form><?}else{$query = "UPDATE news SET name = '$name, email = '$email', headline = '$headline', story = '$story', timestamp = NOW() WHERE id = '$id';$result = @mysql_query($query);if(!$result){   echo('Error updating news item: ' . $mysql_error());   exit();}else{   mysql_close($link);   echo('Update successful!');}}elseif($a == 'delete'){$link = @mysql_connect(localhost, username, password);if(!$link){   echo('Error connecting to the database: ' . $mysql_error());   exit();}$db = @mysql_selectdb('mydatabase');if(!$db){   echo('Error selecting database: ' . $mysql_error());   exit();}$query = "DELETE FROM news WHERE id = '$id'";$result = @mysql_query($query);if(!$result){   echo('Error deleteing news item: ' . $mysql_error());   exit();}mysql_close($link);echo('News item deleted.');} ?></body></html>

Thanks!

Link to comment
Share on other sites

On the line that starts with

$query = "UPDATE news SET 

you've missed a closing

"

at its end, i.e. the line should end with

id = '$id'";

Assuming that's not really what you're asking... what exactly is the problem you're having? Any error messages? Remove your "@" BTW.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...