Jump to content

news page


TheCatapult

Recommended Posts

Hi, Please help me in creating a News page. I currently made a one. Here's the code. Please help me find errors. My plan is to output all the records in database in descending order. post.html

<form action="register.php" method="post">Title<input type="text" name="title><br>Content<input type="password" name="content"><br>Poster<input type="text" name="poster"><input type="Submit"></form>

insert.php

<?php$username=("");$password=("");$database=("");$title=$_POST['title'];$content=$_POST['content'];$poster==$_POST['poster'];mysql_connect=("", $username, $password);mysql_select_db=($database) or die ("Unable to select database");$query="INSERT INTO news VALUES (",'$title', '$content', '$poster', 'date("m/d/y"), time()')";mysql_query=($query);mysql_close();?>

output.php

<?$username="";$password="";$database="";mysql_connect("",$username,$password);@mysql_select_db($database) or die( "Unable to select database");$query="SELECT * FROM news ORDER BY id DESC LIMIT";$result=mysql_query($query);$num=mysql_numrows($result);mysql_close();echo "<b><center>Database Output</center></b><br><br>";$i=0;while ($i < $num) {$title=mysql_result($result,$i,"title");$content=mysql_result($result,$i,"content");$poster=mysql_result($result,$i,"poster");$time=mysql_result($result,$i,"time");echo "$title<br>$content<br$poster";$i++;}?>

Thanks a lot!

Link to comment
Share on other sites

Hi niche, Here's the page. Problems were: 1. It does not write what I put in "poster". 2. It does not write the time when I posted the form in the row "time". 3. I want the output.php to display only the latest 5 entries that has been posted. Yes, I want it to be in descending form. Here's the current code. post.html

<form action="insert.php" method="post">Title<input type="text" name="title">Content<input type="text" name="content">Poster<input type="text" name="poster"><input type="Submit"></form>

insert.php

<?php$username=("");$password=("");$database=("");$title=$_POST['title'];$content=$_POST['content'];$poster==$_POST['poster'];mysql_connect("", $username, $password);mysql_select_db($database) or die ("Unable to select database");$query="INSERT INTO news VALUES ('','$title','$content','$poster','date, time()')";mysql_query($query);mysql_close();?>

output.php

<?$username="";$password="";$database="";mysql_connect("",$username,$password);@mysql_select_db($database) or die( "Unable to select database");$query="SELECT * FROM news";$result=mysql_query($query);$num=mysql_numrows($result);mysql_close();echo "<b><center>Database Output</center></b><br><br>";$i=0;while ($i < $num) {$title=mysql_result($result,$i,"title");$content=mysql_result($result,$i,"content");$poster=mysql_result($result,$i,"poster");$time=mysql_result($result,$i,"time");echo "$title<br>$content<br>$poster<br><br>";$i++;}?>

Please help!

Link to comment
Share on other sites

1. It does not write what I put in "poster".
$poster==$_POST['poster'];
will be
$poster=$_POST['poster'];

"==" is conditional oprators and "=" is assignment oprators

2. It does not write the time when I posted the form in the row "time".
$query="INSERT INTO news VALUES ('','$title','$content','$poster','date, time()')";
it is treating time() as string in double quotes either store it before in a variable then use it or concatenate the function in beetween the string whenerver you are not sure why database query is not working you can always use mysql_error() to check errors.http://php.net/mysql_error
3. I want the output.php to display only the latest 5 entries that has been posted. Yes, I want it to be in descending form.
you can use LIMIT clause with DESC
SELECT * FROM news LIMIT 5 DESC

Link to comment
Share on other sites

Hi! I have two questions. 1. Why there is an unexpected problem in output.php.

http://catapultphpmysql.eu.pn/newsagain/output.php

The current code is.

<?$username="";$password="";$database="";mysql_connect("",$username,$password);@mysql_select_db($database) or die( "Unable to select database");$query="SELECT * FROM news;$result=mysql_query($query);$num=mysql_numrows($result);mysql_close();echo "<b><center>Database Output</center></b><br><br>";$i=0;while ($i < $num) {$title=mysql_result($result,$i,"title");$content=mysql_result($result,$i,"content");$poster=mysql_result($result,$i,"poster");$time=mysql_result($result,$i,"time");echo "$title<br>$content<br>$poster<br><br>";$i++;}?>

2. For example, I make a variable in insert.php called "date", what should be the value of that variable. Oops, I also want to post a time! Please help me! Thanks so much!

Link to comment
Share on other sites

why not post the errors too, or at least be specific about the issue. did you forget a closing quote after

$quote = "SELECT * FROM news

Link to comment
Share on other sites

once you fixed the quotes as mentioned by thescintist you do like$result=mysql_query($query);if($result){$num=mysql_numrows($result);mysql_close();echo "<b><center>Database Output</center></b><br><br>"; you are just telling what you want to do but you have to specify what it is not working or waht is acting unexpectaly or if there any error. so it will be easier for use to help you and you should also use mysql_real_escape_string() toinputs before iserting it in query.http://php.net/mysql...l_escape_string$i=0;while ($i < $num) {$title=mysql_result($result,$i,"title");$content=mysql_result($result,$i,"content");$poster=mysql_result($result,$i,"poster");$time=mysql_result($result,$i,"time");echo "$title<br>$content<br>$poster<br><br>";$i++;}}elseecho mysql_error(); <==add this to check why it is failing not with your issue but..if there is more than one row it is best to use mysql_fetch_*() rather than mysql_reslut(). it will be more efficient.

Link to comment
Share on other sites

there is no relation with php and css . php is for serving page dynamaicaly no matter it is html or css. you can use css as you do in in genrel html page. you have to mae sure your html is not broken which may cause problem in css.

Link to comment
Share on other sites

Hi! Here's my current code. I tried to put all the variables in the <div>...</div> section of my page but what happen is.Parse error: syntax error, unexpected '<' in /srv/disk4/741839/www/catapultphpmysql.eu.pn/newsagain/output.php on line 23Here's the current code.

<?$username="";$password="";$database="";mysql_connect("",$username,$password);@mysql_select_db($database) or die( "Unable to select database");$query="SELECT * FROM news ORDER BY id DESC LIMIT 5";$result=mysql_query($query);$num=mysql_numrows($result);mysql_close();echo "<b><center>Database Output</center></b><br><br>";$i=0;while ($i < $num) {$title=mysql_result($result,$i,"title");$content=mysql_result($result,$i,"content");$poster=mysql_result($result,$i,"poster");?><div style="color:#00FF00"><?$div;<br><br>$content<br><br>$poster?></div><?$i++;}?>

Thanks

Link to comment
Share on other sites

<div style="color:#00FF00"><?$div;<br><br>$content<br><br>$poster?></div>should be similar to below<div style="color:#00FF00"><? echo $div.'<br><br>'.$content.'<br><br>'.$poster; ?></div>OR <div style="color:#00FF00"><? echo $div; ?><br><br><? echo $content; ?><br><br><? echo $poster; ?></div>

Link to comment
Share on other sites

it means there is "<" in line 23 which should not be there. i dont know where is your line 23 but i cant find any "<" which could cause this except short tagsuse <?php tag instead of shorthand <?. in some server it is turned off which may cause trouble.

<div style="color:#00FF00"><?$div;<br><br>$content<br><br>$poster?></div>
variables will not print itself you need to echo it mysql_numrows() is not inbuilt function it is mysql_num_rows()
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...