By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Error message not displaying

P: 17
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php 
  3.  
  4. include('connect.php');
  5.  
  6. $target = "./houses";
  7. $target = $target . basename($_FILES['photo']['name']);
  8.  
  9. if (isset($_POST['submit']))
  10.  
  11.  
  12. $price = mysql_real_escape_string(htmlspecialchars($_POST['price']));
  13.  
  14. $rooms = mysql_real_escape_string(htmlspecialchars($_POST['rooms']));
  15.  
  16. $address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
  17.  
  18. $description = mysql_real_escape_string(htmlspecialchars($_POST['description']));
  19.  
  20. $photo = (mysql_real_escape_string($_FILES['photo']['name']));
  21.  
  22.  
  23. if ($price == '' ||  $rooms == '' || $address == '' || $description == '')
  24.  
  25. {
  26.  
  27. $error = 'ERROR TRYING TO ADD A NEW RECORD: Please fill in all required fields';
  28.  
  29.                             form($price, $rooms, $address, $description, $photo, $error);
  30.  
  31. }
  32.  
  33. else
  34. {
  35.  
  36.                             mysql_query("INSERT house_info SET price='$price', rooms='$rooms', address='$address', description='$description', photo='$photo'")
  37.                             or die(mysql_error()); 
  38.  
  39.                             header("Location:admin.php");
  40. }
  41.  
  42. }
  43.  
  44. else
  45.  
  46. {
  47.                     form('','','','','','');
  48.  
  49. }
  50.  
  51. ?> 
  52.  
  53.  
Dec 20 '11 #1

✓ answered by zorgi

Your form function signature is this:
Expand|Select|Wrap|Line Numbers
  1. form($price, $rooms, $address, $description, $photo)
and this is how you call it:

Expand|Select|Wrap|Line Numbers
  1. form($price, $rooms, $address, $description, $photo, $error);
Your function doesen't know about error.

something elese I noticed. You have more than one:

Expand|Select|Wrap|Line Numbers
  1. include('connect.php');
Try to have only one include line per file or at least use include_once

Share this Question
Share on Google+
7 Replies


Rabbit
Expert Mod 10K+
P: 12,347
You've populated a variable called $error and filled it with text. But you never print it to the page.
Dec 20 '11 #2

P: 17
sorry my mistake.

This is the full code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.              function form($price, $rooms, $address, $description, $photo)
  4.              {
  5.  
  6.                 if (isset($_POST['submit']))
  7.                     { 
  8.                         if ($error != '')
  9.                             {
  10.                                 echo '<div style="padding:5px 10px 5px 10px; margin-top:20px; border:2px solid #FF0000; color:#FF0000; width:315px;">'.$error.'</div>';
  11.                             }
  12.                     }
  13.         ?>
  14.  
  15.             <table id="records">
  16.                 <thead>
  17.                     <tr>
  18.                         <th scope="col">Price</th>
  19.                         <th scope="col">Room Information</th>
  20.                         <th scope="col">Address</th>
  21.                         <th scope="col">House Description</th>
  22.                         <th scope="col">Photo</th>
  23.                         <th scope="col">Edit</th>
  24.                         <th scope="col">Delete</th>
  25.                     </tr>
  26.                 </thead>
  27.  
  28.         <?php
  29.  
  30.             include('connect.php');
  31.  
  32.             $result = mysql_query("SELECT * FROM house_info") 
  33.                     or die(mysql_error());  
  34.  
  35.                 while($row = mysql_fetch_array( $result )) {
  36.  
  37.                         echo "<tbody>";
  38.                         echo "<tr>";
  39.                         echo '<td>' . '&pound;'.$row['price'] . '</td>';
  40.                         echo '<td>' . $row['rooms'] . '</td>';
  41.                         echo '<td>' . $row['address'] . '</td>';
  42.                         echo '<td>' . $row['description'] . '</td>';
  43.                         echo '<td>' . $row['photo'] . '</td>';
  44.                         echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
  45.                         echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
  46.                         echo "</tr>"; 
  47.                 } 
  48.  
  49.                 echo "</tbody>";
  50.                 echo "</table>";
  51.         ?>
  52.  
  53.         <button>Add New Record</button>
  54.  
  55.         <div id="show-form">
  56.             <h4 style="margin-top:20px;">Add Property Information</h4>
  57.  
  58.                 <form id="add-form" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" enctype="multipart/form-data">
  59.                     <table cellspacing="0">
  60.                         <tr>
  61.                             <td><strong>Price: <span class="red">*</span></strong></td>
  62.                             <td><input type="text" maxlength="15" size="12" name="price" /></td>
  63.                         </tr>
  64.                         <tr>
  65.                             <td><strong>Room Information: <span class="red">*</span></strong></td>
  66.                             <td><input type="text" maxlength="50" size="30" name="rooms" /></td>
  67.                         </tr>
  68.                         <tr>
  69.                             <td><strong>Address: <span class="red">*</span></strong></td>
  70.                             <td><input type="text" maxlength="50" size="30" name="address" /></td>
  71.                         </tr>
  72.                         <tr>
  73.                             <td><strong>House Description: <span class="red">*</span></strong></td>
  74.                             <td><input type="textarea" rows="10" size="20" name="description" /></td>
  75.                         </tr>
  76.                         <tr>
  77.                             <td><strong>Photo <span class="red">*</span></strong></td>
  78.                             <td><input type="hidden" name="size" value="350000"><input type="file" name="photo" /></td>
  79.                         </tr>
  80.                         <tr>
  81.                             <td><p><span class="red">*</span> Required</p></td>
  82.                             <td><input type="submit" name="submit" value="Add Property" class="add-button"></td>
  83.                         </tr>    
  84.                     </table>
  85.                 </form> 
  86.  
  87.              <?php 
  88.                 }
  89.  
  90.                 include('connect.php');
  91.  
  92.                 $target = "./houses";
  93.                 $target = $target . basename($_FILES['photo']['name']);
  94.  
  95.                 if (isset($_POST['submit']))
  96.                 { 
  97.  
  98.                     $price = mysql_real_escape_string(htmlspecialchars($_POST['price']));
  99.                     $rooms = mysql_real_escape_string(htmlspecialchars($_POST['rooms']));
  100.                     $address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
  101.                     $description = mysql_real_escape_string(htmlspecialchars($_POST['description']));
  102.                     $photo = (mysql_real_escape_string($_FILES['photo']['name']));
  103.  
  104.  
  105.                         if ($price == '' ||  $rooms == '' || $address == '' || $description == '' || $photo == '')
  106.                         {
  107.  
  108.                             $error = 'ERROR TRYING TO ADD A NEW RECORD: Please fill in all required fields';
  109.  
  110.                             form($price, $rooms, $address, $description, $photo, $error);
  111.  
  112.                         }
  113.  
  114.                         else
  115.                         {
  116.  
  117.                             mysql_query("INSERT house_info SET price='$price', rooms='$rooms', address='$address', description='$description', photo='$photo'")
  118.                             or die(mysql_error()); 
  119.  
  120.                             header("Location:admin.php");
  121.                         }
  122.                 }
  123.  
  124.                 else
  125.                 {
  126.                     form('','','','','','');
  127.  
  128.                 }
  129.             ?> 
  130.  
  131.  
  132.  
Dec 20 '11 #3

zorgi
Expert 100+
P: 431
Your form function signature is this:
Expand|Select|Wrap|Line Numbers
  1. form($price, $rooms, $address, $description, $photo)
and this is how you call it:

Expand|Select|Wrap|Line Numbers
  1. form($price, $rooms, $address, $description, $photo, $error);
Your function doesen't know about error.

something elese I noticed. You have more than one:

Expand|Select|Wrap|Line Numbers
  1. include('connect.php');
Try to have only one include line per file or at least use include_once
Dec 20 '11 #4

P: 17
Thanks very much zorgi! Hours of stressing has finally been solved! :D
Dec 20 '11 #5

Rabbit
Expert Mod 10K+
P: 12,347
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.
Dec 20 '11 #6

100+
P: 1,059
use few debug steps:
like before line 68 add this line
Expand|Select|Wrap|Line Numbers
  1. print_r($_POST);
  2. exit();
  3.  
and then do the process and see what happen
Dec 21 '11 #7

Dormilich
Expert Mod 5K+
P: 8,639
I merged the answers from your double posts into one thread. please donít open multiple threads on the same topic in the future.
Dec 21 '11 #8

Post your reply

Sign in to post your reply or Sign up for a free account.