hi
I have created a form that inputs text and an image into a web page. The text updates fine but the image doesnt show. Can anyone see the what I am doing wrong?
The text and image are extracted from two different mysql tables photographs and cms_careshowkit. When the user clicks on the submit button they are taken to a list of hyperlinks referring to pages they can view. When They click on the page they have just updated it should show the new text and image.
The code for the form is:
<?php
// include("session.php");
include("../../connect/connection.php");
include("functions.php");
//confirm_logged_in();
//in the tutorial this is changed to find selected subjects function (below down to header)
/*--------------------------------------------script starts here--------------------------------
validation*/
if (isset($_POST['submit'])) { //if the page has been submitted submit is the submit button
$errors = array(); // the array called errors will hold all the errors.
$required_fields = array('page','title', 'content1');
foreach($required_fields as $fieldname){
if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && !is_numeric($_POST[$fieldname]))){
$errors[] = $fieldname; //the errors will be $fieldname.
}
}
if (empty($errors)){
// catch form data-------------------------------------------------------------------------------
$id = ($_GET['entry']);
$page = mysqli_prep($_POST["page"]);
$title = mysqli_prep($_POST["title"]);
$content1 = mysqli_prep($_POST["content1"]);
$tmp_file = $_FILES['file_upload']['tmp_name'];
$target_file = basename($_FILES['file_upload']['name']);
$type = $_FILES['file_upload']['type'];
$size = $_FILES['file_upload']['size'];
$caption = $_POST['caption'];
$upload_dir = "uploads";
//query
$query = "INSERT INTO cms_careshowkit (page,title,content1)".
"Values('$page','$title','$content1')";
// execute query
$result = mysqli_query($connection, $query);
// test to see if the update worked
if (mysqli_affected_rows($connection) == 1){
// success
$message = "The subject was successfully updated";
if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file)){
$message = "File uploaded successfully!";
$query3 = "INSERT INTO photographs (filename,type,size,caption)".
"Values('$target_file','$type','$size','$caption')";
$message = "File $fileName uploaded";
$result = mysqli_query($connection, $query3);
}else{
$error = $_FILES['file_upload']['error'];
$message = $upload_errors[$error];
}
$query2 = "SELECT id, title FROM cms_careshowkit";
$result = mysqli_query($connection, $query2) or die('Error, query failed');
if(mysqli_num_rows($result) == 0)
{
echo "Database is empty <br>";
}
else
{
while(list($id, $title) = mysqli_fetch_array($result))
{
?>
<a href="formStreet.php?id=<?php echo $id ;?>"><?php echo $title;?></a></br>
<?php
}
}
//return $result;
}else{
//update failed
$message = "The subject update failed";
$message .= "".mysqli_error($connection);
}
}else{
// errors occured
$message = "There were " .count($errors). " errors in the form";
}
} // end of issset post
/*----------------------------------------------script ends here-----------------------------------*/
if(isset($_GET['entry'])){
$sel_name = $_GET['entry'];
}else{
$sel_name = "";
}
$allpages = get_subjects_by_id($sel_name);
//include("../includes/header.php");
?>
<link href="../articles/content1.css" rel="stylesheet" type="text/css">
<div class="menu">
<?php //navigation($sel_name); ?>
</div><!--end menu-->
<div class="maincontent1"><h2>Edit Subject: <?php echo $allpages['page'];?></h2><br/>
<?php if(!empty($message)){
echo "<p>".$message."</p>";
}
?>
<form style="background:#FFFFFF; padding:15px; border:thin ridge #666666" action="2inputform.php?entry=<?php echo urlencode($allpages['id']);?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input type="file" name="file_upload" /><br/>
<input type="text" name="caption" /><br/>
<p>Page:<input type="text" name="page" id="page" ></p><br/>
<p>Title:<input type="text" name="title" id="title" ></p><br/>
<p>Content1:<br/><textarea name="content1" cols="50" rows="10></textarea></p><br/>
<input type="submit" name="submit" value="Edit" />
</form>
</body>
</html>
The recieving web page has php:
<div id="mainbody">
<div class="banner"></div>
<div id="headline3"><h2 align="center"><?php insertshowkit("title", $id );?></h2>
</div>
<div id="headline3"><p style="font-size:14px"><?php insertshowkit("content1", $id);?></p></div>
<div id="headline3"><h3 align="left" style="color:#3399CC"><?php insertshowkit("subtitle1", $id);?></h3></div><br/>
<div id="headline3"><p style="font-size:14px">
<?php insertshowkit("content2", $id);?></p></div>
<div id="headline3"><h3 align="left" style="color:#3399CC"><?php insertshowkit("subtitle2", $id);?></h3></div>
<div id="headline3"><p style="font-size:14px"><?php insertshowkit("content3", $id);?></p></div>
<div id="headline3"><h3 align="left" style="color:#3399CC"><?php insertshowkit("subtitle3", $id);?></h3></div>
<div id="headline3"><p style="font-size:14px"> <?php new_image("filename", $id);?> <?php insertshowkit("content4", $id);?></p></div>
</div>
The function new_image() is:
function new_image($field, $id){
global $connection;
$id = $_POST['id'];
$query = "SELECT filename, caption FROM photographs Where id = '{$id}' ";
$result = mysqli_query($connection, $query) or die ('Error, Query Failed' );
if($row = mysqli_fetch_array( $result )){
echo "<img src=\"uploads/".$row['filename']."\"/><br>".$row['caption'];
}
}
The form can be seen at http://www.louandelcats.co.uk/testingphp/astafftest/2inputform.php
any help is greatly appreciated thank you
cass27