Jump to content

PHP rename image to input field


freakunleash

Recommended Posts

Hi All,

 

Need help. I have a form which collect the users information including image which are stored in the directory. I want to rename the image file to the name of the user and stores it relative path in SQL table so that when I retrieve user name it also retrieve the image of the user. 

 

My Table structure is as follow:

 

id int(5)

lecturer_name varchar(10)

lecturer_img_path varchar(100)

 

Below is the PHP and HTML code.

 

PHP Code

 

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'test';
mysql_select_db($dbname)or die ('Error connecting to mysql');
?>

<?php
if(isset($_POST['lecturer']))
{
   $lecturer = $_POST['lecturer'];
   $n        = count($lecturer);
   $i        = 0;
   echo "Your selected lecturer are \r\n" .
        "<ol>";
   while ($i < $n)
   {
if($lecturer[$i] == ""){
//do nothing
$i++;
}else{
      echo "<li>{$lecturer[$i]}</li> \r\n";
      $i++;
  }
   }
   echo "</ol>";
}
?>

<?php mysql_close($conn); ?> 

 

 

HTML FORM

 

<!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>lecturer details</title>
<style type="text/css">
body {
background-color: #ffc;
}
</style>
</head>

<body>
<b>
<h1>Enter new lecturer details :</h1>

<form id="form1" name="form1" method="post" action="submit1.php">
  <p>Lecturer:<br />
    <label for="lecturer"></label>
    <input name="lecturer[]" type="text" id="lecturer1" size="15" /> <label for="lect_img1"></label><input id="lect_img1" type="file" name="lect_img[]"><br />
    <input name="lecturer[]" type="text" id="lecturer2" size="15" /> <label for="lect_img2"></label><input id="lect_img2" type="file" name="lect_img[]"><br />
    <input name="lecturer[]" type="text" id="lecturer3" size="15" /> <label for="lect_img3"></label><input id="lect_img3" type="file" name="lect_img[]"><br />
    <input name="lecturer[]" type="text" id="lecturer4" size="15" /> <label for="lect_img4"></label><input id="lect_img4" type="file" name="lect_img[]"><br />
    <input name="lecturer[]" type="text" id="lecturer5" size="15" /> <label for="lect_img5"></label><input id="lect_img5" type="file" name="lect_img[]"><br />
    <input name="lecturer[]" type="text" id="lecturer6" size="15" /> <label for="lect_img6"></label><input id="lect_img6" type="file" name="lect_img[]"><br />
    <br />
  
<input name="submit" type="submit" id="send" value="submit">
</form>

</b>
</body>
</html> 

 

Link to comment
Share on other sites

Really sorry about the post. I have put the wrong code

I manage to insert query in Mysql database for a single file. How do I do it if I have to insert the array of names and there pics in database like from the HTML Form from my previous post.

 

<?php require("connect.php"); ?>

<?php

// Variables
$lecturer = $_POST['lecturer'];
$name = $_FILES["lect_img"]["name"];
$type = $_FILES["lect_img"]["type"];
$size = $_FILES["lect_img"]["size"];
$tmp_name = $_FILES["lect_img"]["tmp_name"];
$error = $_FILES["lect_img"]["error"];
$filename = $lecturer .".jpeg";
$path = "upload/" . $filename;


//echo $type . "<br />";
echo $lecturer . "<br />";
echo $path . "<br />";


if($type == "image/jpeg") 
{
	if($error > 0)
	{
	echo "Return Code:" . $error; 
	}
	else 
	{
	echo "You have successfully uploaded " . $name . " file" . "<br />"; 

		if(file_exists("upload/" . $name))
		{
		echo $name . "File already exists";
		}
		else 
		{
		move_uploaded_file($tmp_name, "upload/" . $filename);
		echo "File saved in upload/" . $filename . "<br />";

		$qry = "INSERT INTO lecturer VALUES ('', '$lecturer', '$path')";
		mysql_query($qry) or die('Error, query failed : ' . mysql_error());			
		}
	}
}
else
    {
echo "Invalid file";
}
?>

<?php mysql_close($conn); ?>

 

<!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>lecturer details</title>
<style type="text/css">
body {
   background-color: #ffc;
}
</style>
</head>

<body>
<b>
<h1>Enter new lecturer details :</h1>

<form id="form1" name="form1" method="post" action="submit.php" enctype="multipart/form-data">
  <p>Lecturer:<br />
    <label for="lecturer"></label>
    <input name="lecturer" type="text" id="lecturer1" size="15" /> <label for="lect_img1"></label><input id="lect_img1" type="file" name="lect_img"><br />
    <br />

<input name="submit" type="submit" id="send" value="submit">
</form>

</b>
</body>
</html> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.