I have a from that brings a record from the database, including the image that is currently stored for that record. The user can then update the information for the record, but the problem is that when I pass the new image from the php page with the form to the script that processes the update of the database record, the image is not actually passing.
This is the php that brings the record from the database to the page:
This is the form to display the record:
And this is the update.php file that processes the update:
Why is the image not passing from the form to the script?
This is the php that brings the record from the database to the page:
Code:
<?php
include ('includes/DbCon.php');
$id = intval($_GET['id']);
$target_dir = $_SERVER['DOCUMENT_ROOT'].'/pc.v.2/images/photo/';
$query = "SELECT * FROM news WHERE id = '$id'";
$result = $mysqli->query ($query);
if(mysqli_num_rows($result)>=1){
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
$headline = $row['headline'];
$body = $row['body'];
$image = $row['image'];
}
?>
Code:
<form action="update.php" method="post" class="newNews"> <input type="hidden" name="ud_id" value="<?=$id;?>"> <!-- <input type="hidden" name="old_id" value="<?=$image;?>"> --> <label for="title">Title</label><br /> <input type="text" name="ud_headline" value="<?=$headline;?>"/><br /><br /><br /> <label for="text">Body</label><br /> <textarea name="ud_body" rows="5" cols="21" value="" class="editBody"><?=$body;?></textarea><br /> <p>Current Photo</p> <img src="images/photo/<?=$image?>" alt=" " width="auto" height="auto"><br /> <!-- $target_dir. --> <input type="file" name="ud_image" class="newsImage" ><br /> <input type="submit" name="submit" value="Update news item" class='addNew' /> </form>
Code:
<?php
include ('includes/DbCon.php');
$target_dir = 'images/photo/'; // $_SERVER['DOCUMENT_ROOT'].'/pc.v.2/
$target_file = $target_dir . basename($_FILES["ud_image"]["name"]);
if (move_uploaded_file($_FILES["ud_image"]["tmp_name"], $target_file))
{
echo '<script type="text/javascript">';
echo 'alert("News Items Saved")';
echo '</script>';
}
else {
echo "Sorry, there was an error with your file.";
}
if (isset($_POST['id']))$ud_id = $_POST['id'];
if (isset($_POST['ud_headline']))$ud_headline = $_POST['ud_headline'];
if (isset($_POST['ud_body']))$ud_body = $_POST['ud_body'];
if (isset($_POST['ud_image']))$ud_image = $_POST['ud_image'];
if(isset($_POST['submit'])){
$query = "UPDATE news SET headline='$ud_headline', body='$ud_body',
image='$ud_image' WHERE id='$ud_id'";
if($mysqli->affected_rows>=1){
//$result = $mysqli->query($query); .$_FILES[ud_image][name]
echo '<script type="text/javascript">';
echo 'document.location.href = "/pc.v.2/admin-news.php";';
echo '</script>';
}
else
{
echo '<script type="text/javascript">';
echo 'alert("The news item was not able to be created. Please try again."). mysql_error()';
echo '</script>';
}}
$mysqli->close();
?>
Comment