Page 1 of 1

posting script

Posted: Sun May 02, 2010 2:46 am
by pritam79
Hi all,
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>

Re: posting script

Posted: Sun May 02, 2010 4:57 am
by social_experiment

Code: Select all

<?php $result = mysql_query("INSERT into sitetable(uid, name, title, post) values('', '$_POST[name]', '$_POST[title]', '$_POST[post]')"); ?>
What is the value of 'uid'? It seems from this query, you are inserting an empty (NULL) value into the table with each query. This might be what is affecting your select query.

Re: posting script

Posted: Sun May 02, 2010 5:27 am
by social_experiment
With regards to the value for 'uid' being empty, if the field is an integer (int) then '' is picked up as 0. What is the use of uid in your table?

Re: posting script

Posted: Mon May 03, 2010 12:31 am
by pritam79
got the solution, thanks.

now i m trying to display the form input error messages next to the form fields in case of error in input.

Re: posting script

Posted: Mon May 03, 2010 2:24 am
by social_experiment
If you are just using html for this, you could put the error in a <div> and use absolute / relative positioning to put it next to the form.