Error message not displaying

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • James
    New Member
    • Jun 2011
    • 17

    Error message not displaying

    Hi there. I seem to be having a problem with an if statement within the following PHP code. I have a form which a user can fill in, in order to add house details to a database such as price, description and image etc. This PHP script throws up no errors when it is run and is working perfectly fine. However, the if statement I have used to display an error message if the user doesn't fill in all the fields isn't working. When I submit a blank form the data isn't inserted into the database which it is designed to do, but the error message isn’t displayed. The only reason I can think of is because I have an input type file within the form and maybe this is causing the PHP to misread it. Any feedback would be much appreciated.

    Code:
    <?php 
    
    include('connect.php');
    				
    $target = "./houses";
    $target = $target . basename($_FILES['photo']['name']);
    
    if (isset($_POST['submit']))
    
    { 
    
    $price = mysql_real_escape_string(htmlspecialchars($_POST['price']));
    
    $rooms = mysql_real_escape_string(htmlspecialchars($_POST['rooms']));
    
    $address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
    
    $description = mysql_real_escape_string(htmlspecialchars($_POST['description']));
    
    $photo = (mysql_real_escape_string($_FILES['photo']['name']));
    
    			 
    if ($price == '' ||  $rooms == '' || $address == '' || $description == '')
    
    {
    
    $error = 'ERROR TRYING TO ADD A NEW RECORD: Please fill in all required fields';
    
    							form($price, $rooms, $address, $description, $photo, $error);
    
    }
    
    else
    {
    
    							mysql_query("INSERT house_info SET price='$price', rooms='$rooms', address='$address', description='$description', photo='$photo'")
    							or die(mysql_error()); 
    
    							header("Location:admin.php");
    }
    
    }
    			 
    else
    
    {
    					form('','','','','','');
    					
    }
    
    ?>
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You've populated a variable called $error and filled it with text. But you never print it to the page.

    Comment

    • James
      New Member
      • Jun 2011
      • 17

      #3
      sorry my mistake.

      This is the full code:

      Code:
      <?php
      
      			 function form($price, $rooms, $address, $description, $photo)
      			 {
      				
      				if (isset($_POST['submit']))
      					{ 
      						if ($error != '')
      							{
      								echo '<div style="padding:5px 10px 5px 10px; margin-top:20px; border:2px solid #FF0000; color:#FF0000; width:315px;">'.$error.'</div>';
      							}
      					}
      		?>
      				
      			<table id="records">
      				<thead>
      					<tr>
      						<th scope="col">Price</th>
      						<th scope="col">Room Information</th>
      						<th scope="col">Address</th>
      						<th scope="col">House Description</th>
      						<th scope="col">Photo</th>
      						<th scope="col">Edit</th>
      						<th scope="col">Delete</th>
      					</tr>
      				</thead>
      				
      		<?php
      			
      			include('connect.php');
      
      			$result = mysql_query("SELECT * FROM house_info") 
      					or die(mysql_error());  
      					
      				while($row = mysql_fetch_array( $result )) {
      	
      						echo "<tbody>";
      						echo "<tr>";
      						echo '<td>' . '&pound;'.$row['price'] . '</td>';
      						echo '<td>' . $row['rooms'] . '</td>';
      						echo '<td>' . $row['address'] . '</td>';
      						echo '<td>' . $row['description'] . '</td>';
      						echo '<td>' . $row['photo'] . '</td>';
      						echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
      						echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
      						echo "</tr>"; 
      				} 
      				
      				echo "</tbody>";
      				echo "</table>";
      		?>
      
      		<button>Add New Record</button>
      		
      		<div id="show-form">
      			<h4 style="margin-top:20px;">Add Property Information</h4>
      				
      				<form id="add-form" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" enctype="multipart/form-data">
      					<table cellspacing="0">
      						<tr>
      							<td><strong>Price: <span class="red">*</span></strong></td>
      							<td><input type="text" maxlength="15" size="12" name="price" /></td>
      						</tr>
      						<tr>
      							<td><strong>Room Information: <span class="red">*</span></strong></td>
      							<td><input type="text" maxlength="50" size="30" name="rooms" /></td>
      						</tr>
      						<tr>
      							<td><strong>Address: <span class="red">*</span></strong></td>
      							<td><input type="text" maxlength="50" size="30" name="address" /></td>
      						</tr>
      						<tr>
      							<td><strong>House Description: <span class="red">*</span></strong></td>
      							<td><input type="textarea" rows="10" size="20" name="description" /></td>
      						</tr>
      						<tr>
      							<td><strong>Photo <span class="red">*</span></strong></td>
      							<td><input type="hidden" name="size" value="350000"><input type="file" name="photo" /></td>
      						</tr>
      						<tr>
      							<td><p><span class="red">*</span> Required</p></td>
      							<td><input type="submit" name="submit" value="Add Property" class="add-button"></td>
      						</tr>	
      					</table>
      				</form> 
      
      			 <?php 
      				}
      
      				include('connect.php');
      				
      				$target = "./houses";
      				$target = $target . basename($_FILES['photo']['name']);
      
      				if (isset($_POST['submit']))
      				{ 
      
      					$price = mysql_real_escape_string(htmlspecialchars($_POST['price']));
      					$rooms = mysql_real_escape_string(htmlspecialchars($_POST['rooms']));
      					$address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
      					$description = mysql_real_escape_string(htmlspecialchars($_POST['description']));
      					$photo = (mysql_real_escape_string($_FILES['photo']['name']));
      
      			 
      						if ($price == '' ||  $rooms == '' || $address == '' || $description == '' || $photo == '')
      						{
      
      							$error = 'ERROR TRYING TO ADD A NEW RECORD: Please fill in all required fields';
      
      							form($price, $rooms, $address, $description, $photo, $error);
      
      						}
      
      						else
      						{
      
      							mysql_query("INSERT house_info SET price='$price', rooms='$rooms', address='$address', description='$description', photo='$photo'")
      							or die(mysql_error()); 
      
      							header("Location:admin.php");
      						}
      				}
      			 
      				else
      				{
      					form('','','','','','');
      					
      				}
      			?>

      Comment

      • zorgi
        Recognized Expert Contributor
        • Mar 2008
        • 431

        #4
        Your form function signature is this:
        Code:
        form($price, $rooms, $address, $description, $photo)
        and this is how you call it:

        Code:
        form($price, $rooms, $address, $description, $photo, [B]$error[/B]);
        Your function doesen't know about error.

        something elese I noticed. You have more than one:

        Code:
        include('connect.php');
        Try to have only one include line per file or at least use include_once

        Comment

        • James
          New Member
          • Jun 2011
          • 17

          #5
          Thanks very much zorgi! Hours of stressing has finally been solved! :D

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            I didn't ask for your full code. I'm telling you what the problem is. You never write the error message to the page.

            Comment

            • johny10151981
              Top Contributor
              • Jan 2010
              • 1059

              #7
              use few debug steps:
              like before line 68 add this line
              Code:
              print_r($_POST);
              exit();
              and then do the process and see what happen

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                I merged the answers from your double posts into one thread. please don’t open multiple threads on the same topic in the future.

                Comment

                Working...