0

I am creating a user panel in which users can register and upload a image. Once they login they may decide to edit that image therefore I am wanting to perform a update query to add a new image to the location and attach the new image to their mysql data.

So whats happening when i submit the code: I get a error saying:: Sorry, there was a problem uploading your file.

Hopefully someone can let me know why this is giving me a error. PS I am already using this location when they first register to upload a file. But surely this shouldnt have any problem. Another issue i found is that nothing is being successfully inserted into the MYSQL Table for the photo.

So here is the code:

Table: admin

CREATE TABLE IF NOT EXISTS `admin` (
      `id` int(3) NOT NULL auto_increment,
      `UserName` varchar(30) default NULL,
      `PassWord` varchar(30) default NULL,
      `name` varchar(30) default NULL,
      `mainContent` varchar(2000) default NULL COMMENT 'maincontent test',
      `photo` varchar(400) NOT NULL COMMENT 'Picture1',
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=64 ;

So lets imagine the user has logged into their user panel: and they click the edit button linking to their ID.

So they appear at the edit page:

EDIT.PHP

<?php
session_start();
$UserName = $_SESSION['UserName'];
require("checkLoginSession.php");
$adminid = $_GET['id'];


// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");


echo("Logged In As: $UserName");
echo "<br />";
echo("We are editing Data for ID: $adminid");
echo "<br />";
echo "<a href=test.php>Go back to panel</a>";

$id=$_GET['id'];
// Retrieve data from database 
$sql="SELECT * FROM admin WHERE id='$id'";
$result=mysql_query($sql) or die(mysql_error());

$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Main Content</strong></td>
<td align="center"><strong>Image Locatoin</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="mainContent" type="text" id="mainContent" value="<? echo $rows['mainContent']; ?>" size="15"></td>
<td align="center"><input name="photo" type="file" id="photo">
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?
mysql_close();
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Page</title>
</head>

<body>
<h2>Edit Page (<?php echo ("$adminid"); ?>)</h2>
<a href="test.php"><img src="backtopanel.jpg" alt="back to panel" width="454" height="85" border="0" longdesc="back to panel" /></a>
</body>
</html>

update_ac.php

<?php
session_start();
$UserName = $_SESSION['UserName'];
require("checkLoginSession.php");
include "common.php";
DBConnect();
$Link = mysql_connect($Host, $User, $Password);

$id = $_POST['id'];

$target = "images/"; 
$target = $target . basename( $_FILES['photo']['name']); 
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// update data in mysql database 
$_POST = array_map("mysql_real_escape_string", $_POST); 
$firstName = $_POST["name"];
$mainText = $_POST["mainContent"];
$pic=($_FILES['photo']['name']); 


//



$sql="UPDATE admin SET name='$firstName', mainContent='$mainText', photo='$pic' WHERE id='$id'";

echo $sql;

//

if (mysql_db_query ($DBName, $sql, $Link)){
print ("A record was created <br><a href=index.php> return to index </a>\n");


 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else { 

 //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file."; 
 } 


} else {

print ("Record not created");   
}

mysql_close($Link);
?>
Jonathon Legg
  • 15
  • 2
  • 8

1 Answers1

0

A few things:

If nothing is coming through in $_FILES then you have probably missed out enctype="multipart/form-data" in the form tag of the submitting page where the image is being specified.

When handling the uploaded file you need to use $_FILES['inputName']['tmp_name'] to get the temp location of the file then move this using move_uploaded_file($_FILES['inputName']['tmp_name'], 'destination'); and store the destination value in the DB record.

On the edit page where you want to display the image you will need to have the image itself displayed (using and img tag) as well as a file input for changing the image if required.

I could go on, but I really think you need to go read some tutorials on form / file handling in PHP as would most which is why you have not received any replies...

Ingmar Boddington
  • 3,440
  • 19
  • 38