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. -
-
<?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('','','','','','');
-
-
}
-
-
?>
-
-
Your form function signature is this: -
form($price, $rooms, $address, $description, $photo)
and this is how you call it: - form($price, $rooms, $address, $description, $photo, $error);
Your function doesen't know about error.
something elese I noticed. You have more than one:
Try to have only one include line per file or at least use include_once
7 1706
You've populated a variable called $error and filled it with text. But you never print it to the page.
sorry my mistake.
This is the full 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>' . '£'.$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('','','','','','');
-
-
}
-
?>
-
-
-
Your form function signature is this: -
form($price, $rooms, $address, $description, $photo)
and this is how you call it: - form($price, $rooms, $address, $description, $photo, $error);
Your function doesen't know about error.
something elese I noticed. You have more than one:
Try to have only one include line per file or at least use include_once
Thanks very much zorgi! Hours of stressing has finally been solved! :D
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.
use few debug steps:
like before line 68 add this line
and then do the process and see what happen
I merged the answers from your double posts into one thread. please don’t open multiple threads on the same topic in the future.
Sign in to post your reply or Sign up for a free account.
Similar topics
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: '\'...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
| |