Everything works great on a test page that I have configured, but not with the full form page. Can someone please tell me what I'm missing with my copy/paste here?
My test pages are form.php and image.php and my production pages are add_inventory.php and add.php.
form.php
Code: Select all
<html>
<body>
<form name="newad" method="post" enctype="multipart/form-data" action="Forms\image.php">
<table>
<tr><td><input type="file" name="image" accept="image/jpg"></td></tr>
<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
</table>
</form>
</body>
</html>Code: Select all
<?php
define ("MAX_SIZE","10000");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if(isset($_POST['Submit']))
{
$image=$_FILES['image']['name'];
if ($image)
{
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
echo '<h1>Invalid image type.</h1>';
$errors=1;
}
else
{
$size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
echo '<h1>The image is too big!</h1>';
$errors=1;
}
$image_name=time().'.'.$extension;
$newname="../images/parts/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Could not upload!</h1>';
$errors=1;
}}}}
include '..\admin\db_inventory.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to SQL Database');
mysql_select_db($dbname);
$insert="INSERT INTO parts (partnum, quantity, description, manufacturer, model, statusid, image)
VALUES ('222666','1','Computer','HAL','9000','1','$image_name')";
$error_button = "<FORM><INPUT TYPE='button' value='Go Back' onclick='history.go(-1)' /></FORM>";
if (!mysql_query($insert,$conn))
{
die("Error: ".mysql_error().$error_button);
}
echo "Record added";
mysql_close($conn);
?>Code: Select all
<html>
<head>
<title>Add New Inventory</title>
<link rel="stylesheet" href="CSS\standard.css">
</head>
<body align="center">
<h2>Add New Inventory</h2>
<form action="Forms\add.php" method="post" enctype="multipart/form-data">
<div align='center' />
<table class="add" width="30%">
<tr>
<td>Part Number:</td>
<td><input type="text" name="partnum" /></td>
</tr>
<tr>
<td>Quantity:</td>
<td><input type="text" name="quan" /></td>
</tr>
<tr>
<td>Description:</td>
<td><input type="text" name="desc" /></td>
</tr>
<tr>
<td>Manufacturer:</td>
<td><input type="text" name="man" /></td>
</tr>
<tr>
<td>Model:</td>
<td><input type="text" name="mod" /></td>
</tr>
<tr>
<td>Status:</td>
<td>
<?php
include 'admin\db_inventory.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to database');
mysql_select_db($dbname);
$query = "SELECT DISTINCT status FROM status ORDER BY statusid;";
if($result = mysql_query($query))
{
if($success = mysql_num_rows($result) > 0)
echo "<select name='status';'>\n";
while ($row = mysql_fetch_array($result))
echo "<option value='$row[status]'>$row[status]</option>\n";
echo "</select>\n";
}
mysql_close();
?>
</td>
</tr>
<tr>
<td>Image:</td>
<td><input type="file" name="image" accept="image/jpeg"/></td>
</tr>
</table>
<input type="submit" name="Add Part" Value="Add To Inventory" />
<input type="button" Value="Cancel" onclick="history.go(-1)" />
</form>
</center>
</body>
</html>Code: Select all
<html>
<head>
<title>Add Inventory</title>
<link rel="stylesheet" href="..\CSS\standard.css">
</head>
<body>
<h2>Add Inventory</h2>
<?php
// Set Variables from POST
$partnum = "$_POST[partnum]";
$quan = "$_POST[quan]";
$desc = "$_POST[desc]";
$man = "$_POST[man]";
$mod = "$_POST[mod]";
$stat = "$_POST[status]";
define ("MAX_SIZE","10000");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if(isset($_POST['Submit']))
{
$image=$_FILES['image']['name'];
if ($image)
{
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
echo '<h1>Invalid image type.</h1>';
$errors=1;
}
else
{
$size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
echo '<h1>The image is too big!</h1>';
$errors=1;
}
$image_name=time().'.'.$extension;
$newname="../images/parts/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Could not upload!</h1>';
$errors=1;
}}}}
include '..\admin\db_inventory.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to SQL Database');
mysql_select_db($dbname);
$insert="INSERT INTO parts (partnum, quantity, description, manufacturer, model, statusid, image)
VALUES ('222666','1','Computer','HAL','9000','1','$image_name')";
$error_button = "<FORM><INPUT TYPE='button' value='Go Back' onclick='history.go(-1)' /></FORM>";
if (!mysql_query($insert,$conn))
{
die("Error: ".mysql_error().$error_button);
}
echo "Record added";
mysql_close($conn);
?>
<div align=center>
<FORM>
<INPUT TYPE="button" VALUE="Add Inventory" onclick="window.location.href='../add_inventory.php';" />
<INPUT TYPE="button" VALUE="Modify Inventory" onclick="window.location.href='../modify_inventory.php';" />
<INPUT TYPE="button" VALUE="View Inventory" onclick="window.location.href='../current_inventory.php';" />
<INPUT TYPE="button" VALUE="Home" onclick="window.location.href='../index.php';" />
</FORM>
</div>
</body>
</html>The add_inventory.php calls the add.php to upload the image and insert the data.