Hi guys,
i’ve built a webapp where i can upload along with description images, and then display images on the very same page. One of the things i am trying to validate is for an empty string under the Description input field. Now obviously, this is quite simple i.e checking if it is equal to " " but when i submit my form, it does not go into my validation check. Here is a list of what i’ve been doing:
// Check if description bar is blank
$descriptionbar = mysqli_real_escape_string($link, $_POST['description']);
if($descriptionbar = " " )
{
$error = 'Sorry you must enter a description inorder to submit image' + print($description);
include 'error.html.php';
exit();
}
On submission of my form with my file and leaving a blank, it submits and outputs a value of 1
$descriptionbar = mysqli_real_escape_string($link, $_POST['description']);
if(!isset($descriptionbar) )
{
$error = 'Sorry you must enter a description inorder to submit image' + print($description);
include 'error.html.php';
exit();
}
On submission, again leaving the description blank, the image is displayed with no text
$descriptionbar = mysqli_real_escape_string($link, $_POST['description']);
if(empty($descriptionbar) )
{
$error = 'Sorry you must enter a description inorder to submit image' + print($description);
include 'error.html.php';
exit();
}
On submission, it simply outputs the value of 1.
I thought by simply testing for " ", would work, but clearly it does not.
My only guess is that there is an issue with my form:
<form enctype="multipart/form-data" action=" " method="POST">
<div>
<label for="description"> Description: </label> <input type="text" name="description" id="description" /> <br >
</div>
<div>
<label for="category"> Category </label>
<select name="category" id="category">
<option value="Cheng_Huang_Temple">Cheng Huang Temple </option>
<option value="Shanghai_Zoo">Shanghai Zoo </option>
</select> <br />
</div>
<div>
<label for="uploadimg"> Upload Photo: </label>
<input type="file" id="photo" name="photo"/> <br >
</div>
<div>
<input type="hidden" name="action" value="upload"/>
<input type="submit" value="Upload" />
</div>
</form>
index.php
if(!isset($_FILES['photo']))
{
include 'imageuploadform.html.php';
}
if(isset($_POST['action']) and $_POST['action'] == 'upload')
{
$description = mysqli_real_escape_string($link, $_POST['description']);
$category = mysqli_real_escape_string($link, $_POST['category']);
$file = ($_FILES['photo']['name']);
$target = './images/';
$target .= basename($_FILES['photo']['name']);
//print_r($_FILES['photo']);
// Check if file was uploaded
if(!is_uploaded_file($_FILES['photo']['tmp_name']))
{
$error = 'There was no file uploaded';
include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
exit();
}
// Check if file exists
if(file_exists($target))
{
$error = 'This file already exists on the server';
include 'error.html.php';
exit();
}
// Check if description bar is blank
$descriptionbar = mysqli_real_escape_string($link, $_POST['description']);
if(empty($descriptionbar) )
{
$error = 'Sorry you must enter a description inorder to submit image' + print($description);
include 'error.html.php';
exit();
}
... rest of code skiped
If somebody can give me some assistant on this that would be greate.