Jump to content

Recommended Posts

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

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.

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.