gozbay.com Posted August 6, 2007 Share Posted August 6, 2007 Hello Im quite new to PHP/ MySQL, I need to make a form where the user can upload a picture to a folder on the server such as /pics and I also need the path to be saved in a table such as 'picpath' in a MySQL database. Below Is what I came with so far, I think I just need to know what the table should look like and what the 'Upload File' field should look like so it will upload the file to a folder on my server. <?php require_once('file:///E|/Users/Zach/Documents/ntm/Connections/test.php'); ?> <?php function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue; switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } $editFormAction = $HTTP_SERVER_VARS['PHP_SELF']; if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) { $editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING']; } if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "insert_item")) { $insertSQL = sprintf("INSERT INTO top_school (itemname, description, picpath) VALUES (%s, %s, %s)", GetSQLValueString($HTTP_POST_VARS['textfield'], "text"), GetSQLValueString($HTTP_POST_VARS['textarea'], "text"), GetSQLValueString($HTTP_POST_VARS['file'], "text")); mysql_select_db($database_test, $test); $Result1 = mysql_query($insertSQL, $test) or die(mysql_error()); } mysql_select_db($database_test, $test); $query_Recordset1 = "SELECT * FROM top_school"; $Recordset1 = mysql_query($query_Recordset1, $test) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); ?> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="insert_item"> <p>Item Name <input type="text" name="textfield"> </p> <p>Description <textarea name="textarea"></textarea> </p> <p>Picture <input type="file" name="file"> </p> <p> <input type="submit" name="Submit" value="Submit"> </p> <p> </p> <p> </p> <input type="hidden" name="MM_insert" value="insert_item"> </form> Thanks For all of your Help!! -Zach Quote Link to comment https://forums.phpfreaks.com/topic/63573-uploading-picture-and-saving-the-path-in-a-mysql-database/ Share on other sites More sharing options...
Fadion Posted August 7, 2007 Share Posted August 7, 2007 Im not reviewing your code as im pretty sure uve not written it completely by yourself. If so correct me pls. Instead ill give u a working code which i just wrote and tested. It will let the user browse for a file, verify the file, upload it and make a sql query. The form <form enctype="multipart/form-data" name="uploadform" method="post" action=""> <input name="picupload" type="file" /> <input type="submit" name="Submit" value="submit" /> </form> The php code <?php if($_FILES['picupload']['name'] != ""){ $path = "pics/" . basename($_FILES['picupload']['name']); $ext = strtolower(substr(strrchr($_FILES['picupload']['name'], '.'), 1)); $allowedExt = array('jpg', 'png', 'gif', 'bmp'); if(in_array($ext, $allowedExt)){ if(move_uploaded_file($_FILES['picupload']['tmp_name'], $path)){ $query = mysql_query("INSERT INTO table (path) VALUES('$path')"); echo "The file was uploaded successfully."; } else{ echo "There was an error uploading the file. Please try again later."; } } else{ echo "The format is not supported."; } } ?> U have just to modify the path and sql query to your needs. Another thing is the require_once() thing. Sure u can include a file with absolute path, but it will give u problems after uploading in a web server as u dont have the same directory structure. Use smth like include('includes/connections/test.php'). Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/63573-uploading-picture-and-saving-the-path-in-a-mysql-database/#findComment-317219 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.