freakunleash Posted February 7, 2011 Share Posted February 7, 2011 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> Quote Link to comment Share on other sites More sharing options...
beegro Posted February 7, 2011 Share Posted February 7, 2011 Excuse me if I missed it but I don't see any file upload processing in the PHP. Are you not sure how to upload the image? Quote Link to comment Share on other sites More sharing options...
zenlord Posted February 7, 2011 Share Posted February 7, 2011 Also, in your HTML, you need the 'enctype="multipart/form-data"'-attribute inside the <form> Quote Link to comment Share on other sites More sharing options...
freakunleash Posted February 8, 2011 Author Share Posted February 8, 2011 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.