I have used this posting script in PHP that allows users to simply add posts and get displayed on a page. Many other features would be added later, but in this script the very first post that is created gets displayed on the ‘index.php’ page and also the first record gets inserted into the database table. But the other subsequent records inserted after the first record don’t get inserted in MySQL or get displayed on ‘index.php’. I don’t know what the problem is. Plz help….
this is index.php
Code: Select all
<?php
include "header1.php";
?>
<div id="content">
<table bordercolor="white">
<?php
$con=mysql_pconnect("localhost","root","");
mysql_select_db("sitedata",$con);
$result= mysql_query("SELECT * from sitetable order by uid");
while($row = mysql_fetch_array($result))
{
$name = $row['name'];
$title = $row['title'];
$post = $row['post'];
echo "<tr><td width=\"190px\" height=\"128px\"><br>Posted by: $name ";
echo "<br>Title: $title </td>";
echo "<td height=\"128px\">$post</td></tr>";
}
?>
</table>
</div>
This is add_post_form.php
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
include "header1.php";
?>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$error = array();
$name = (isset($_POST['name'])) ? $_POST['name'] : "";
$title = (isset($_POST['title'])) ? $_POST['title'] : "";
$post = (isset($_POST['post'])) ? $_POST['post'] : "";
if(strlen($name) === 0)
{
$error['name'] = '<center><font color="red">name cannot be left blank</font></center>';
}
if(strlen($title) === 0)
{
$error['title'] = '<center><font color="red">title cannot be left blank</font></center>';
}
if(strlen($post) === 0)
{
$error['post'] = '<center><font color="red">post cannot be left blank</font></center>';
}
if(empty($error))
{
$con=mysql_pconnect("localhost","root","");
mysql_select_db("sitedata",$con);
$result= mysql_query("SELECT * from sitetable where uid>=0");
$result = mysql_query("INSERT into sitetable(uid, name, title, post) values('', '$_POST[name]', '$_POST[title]', '$_POST[post]')");
header('Location: index.php');
}
}
?>
<div id="content">
<?php
if(isset($error)){
?>
<?php foreach($error as $key=>$item) echo "<strong>$item</strong></br>"; ?>
<?php
}
?>
<center><form action="add_post_form.php" method="post"><br><br>
Name: <input type="text" name="name"><br><br>
Title: <input type="text" name="title"><br><br>
Post: <textarea name="post"></textarea><br><br>
<input type="submit" value="Submit">
</form></center>
</div>