I'm checking whether or not a text box has value or not before saving it into my database. I'm creating a movie website, so the validation is working fine.
The problem is with the saving, I'm uploading a picture with the movie. The picture is being upload to a folder into my web site application into my directory, the only problem here is that I'm always having this error code while clicking on save
Notice: Undefined index: photoimg in C:\xampp\htdocs\star_crud\Home.php on line 233
Notice: Undefined index: photoimg in C:\xampp\htdocs\star_crud\Home.php on line 234
My code is below :
if (isset($_POST['create'])) {
// keep track post values
$cast = $_POST['cast'];
$title = $_POST['title'];
$comment =$_POST['comment'];
$year = $_POST['year'];
$tag = $_POST['tags'];
$IDBM = $_POST['idbm'];
$cast = htmlspecialchars($cast);
$title = htmlspecialchars($title);
$comment = htmlspecialchars($comment);
// validate input
$valid = true;
if (empty($cast)) {
$castError = 'Please enter Cast';
$valid = false;
}
if (empty($title)) {
$titleError = 'Please enter Title';
$valid = false;
}
if (empty($comment)) {
$commentError = 'Please enter Comment';
$valid = false;
}
if ($valid) {
$valid_formats = array("jpg", "png", "gif", "bmp");
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
echo "hi";
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}
}
else echo "error";
I have done a check removing all the statement in if(valid)
statement, and print an string, it work, I think the problem come with the statement.
<form class="form-horizontal" id="form1" action="Home.php" method="post">
<div class="control-group <?php echo !empty($titleError)?'error':'';?>">
<label class="control-label">Title</label>
<div class="controls">
<input name="title" type="text" placeholder="Title" value="<?php echo !empty($title)?$title:'';?>">
<?php if (!empty($titleError)): ?>
<span class="help-inline"><?php echo $titleError;?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($emailError)?'error':'';?>">
<label class="control-label">Year</label>
<div class="controls">
<?php
$years = range (2011, 2021);
echo '<select name="year">';
foreach ($years as $value) {
echo "<option value=\"$value\"> $value</option>\n";
}
echo '</select>';
?>
</div>
</div>
<div class="control-group <?php echo !empty($emailError)?'error':'';?>">
<label class="control-label">Category</label>
<div class="controls">
<?php
require 'db2.php';
$q1 = mysqli_query($dbc,"SELECT Name FROM Category ");
echo "<select name='Category'>";
while ($row = mysqli_fetch_array($q1)) {
echo "<option value='" . $row['Name'] . "'>" . $row['Name'] . "</option>";
}
echo "</select>";
?>
</div>
</div>
<div class="control-group <?php echo !empty($castError)?'error':'';?>">
<label class="control-label">Cast</label>
<div class="controls">
<input name="cast" type="text" placeholder="Cast" value="<?php echo !empty($cast)?$cast:'';?>">
<?php if (!empty($castError)): ?>
<span class="help-inline"><?php echo $castError;?></span>
<?php endif;?>
</div>
</div>
<div class="ajaxform">
<div class="control-group <?php echo !empty($imageError)?'error':'';?>">
<label class="control-label">Image Upload</label>
<div class="controls">
<input type="file" name="photoimg" onchange="readURL(this);" id="photoimg" /><br/>
<img id="blah" src="#" height="150" width="150" alt="your image" />
</div>
</div>
</div>
<div class="control-group <?php echo !empty($TagsError)?'error':'';?>">
<label class="control-label">Tags</label>
<div class="controls">
<input name="tags" id="mySingleField" type="hidden" > <!-- only disabled for demonstration purposes -->
<ul id="singleFieldTags"></ul><?php if (!empty($TagsError)): ?>
<span class="help-inline"><?php echo $TagsError;?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($IDMBError)?'error':'';?>">
<label class="control-label">IDBM</label>
<div class="controls">
<input name="idbm" type="textarea"><?php if (!empty($IDMBError)): ?>
<span class="help-inline"><?php echo $IDMBError;?></span>
<?php endif;?>
</div>
</div>
<div class="control-group <?php echo !empty($CommentError)?'error':'';?>">
<label class="control-label">Comment</label>
<textarea name="comment" id="comment" rows="4" style="width:780px" cols="50">
</textarea>
<?php if (!empty($commentError)): ?>
<span class="help-inline"><?php echo $commentError;?></span>
<?php endif;?>
<div class="controls">
</div>
</div>
<div class="form-actions">
<button type="submit" name="create" class="btn btn-success">Create</button>
<a class="btn" href="index.php">Home</a>
</div>
</form>