posting script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

posting script

Post 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>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: posting script

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: posting script

Post 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?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

Re: posting script

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: posting script

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply