473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Error message not displaying

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

7 1706
Rabbit
12,516 Expert Mod 8TB
You've populated a variable called $error and filled it with text. But you never print it to the page.
Dec 20 '11 #2
James
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
431 Expert 256MB
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
James
17
Thanks very much zorgi! Hours of stressing has finally been solved! :D
Dec 20 '11 #5
Rabbit
12,516 Expert Mod 8TB
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
johny10151981
1,059 1GB
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
8,658 Expert Mod 8TB
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

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

Similar topics

2
by: Hong | last post by:
Hi, I am trying to create a switch but I do not know why I am geting an error message, can someone tell me what is wrong, Error Message; Warning: Unexpected character in input: '\'...
2
by: Xiao Tencas | last post by:
When trying to load a png image in GD, I get the error (Mozilla Firefox) "cannot display image, because it contains errors"; IE just shows an empty 'image-error' box. When loading a large enough...
1
by: Mark Day | last post by:
Hi All, I have an application that links tables to an ODBC source. The DSN parameters are obtained from a custom .ini file. An incorrect DSN string gives the user a 'friendly' connection error...
1
by: Daniel | last post by:
How can I get in my custom ErrorPage the error message from the page that produced the error ?? This is just for displaying the error.
1
by: henry | last post by:
Hi all, When user upload a file, there is a size limit (default is 4M). And we can change this size limit by changging the config file. If a user upload a file bigger than this size, an DNS...
2
by: Sugan | last post by:
Hi All, I have a peculiar kind of issue after converting VC and VB code from Visual studio 6.0 to Visual Studio 2005. We have the business logic in the VC code which uses the ATL framework...
0
by: Sugan | last post by:
Hi All, I have a peculiar kind of issue after converting VC and VB code from Visual studio 6.0 to Visual Studio 2005. We have the business logic in the VC code which uses the ATL framework...
4
by: atiq | last post by:
i have used a combo box on a form linked to a query. the user is to select a value from the combo box and this click 'Find'. But on an old occasion where the user has forgotten to select a value from...
1
by: =?Utf-8?B?SmFjaw==?= | last post by:
Hi, I have a simple code where I am trying to capture a recordset in a grid veiw using a button click event. Data is connected to sql server 2000. However, I am getting error at the line...
3
by: sumanta123 | last post by:
Hi All, I am using this command exp dev/dev@uatdb file='C:\database\dbbackup.dmp' for export the data from my database.It is successfully export into the dbbackup.dmp file. While import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.