I want to use a variable to receive which form to insert data. I wrote about a closet database.
<?php
$category="";
include('db.php');
if(isset($_POST['save']))
{
$name = $_FILES['file']['name'];
$color = $_POST['color'];
$season = $_POST['season'];
$pattern = $_POST['pattern'];
$type = $_POST['type'];
$imagetype = $_FILES['file']['type'];
switch ($_POST['category'])
{
case "clothes":
echo $category="clothes";
break;
case "under":
echo $category = "under";
break;
case "coat":
echo $category = "coat";
break;
case "accessory":
echo $category = "accessory";
}
if($imagetype =='image/jpeg' || $imagetype == 'image/gif' || $imagetype =='image/png')
{
//move image to folder length
$uploadfile = move_uploaded_file($_FILES['file']['tmp_name'],'upload/'.$_FILES['file']['name']);
//insert to database
$query = mysql_query("INSERT INTO $category(name,color,season,pattern,type,imagetype) VALUES('$name','$color','$season','pattern','$type','$imagetype')");
if($uploadfile && $query)
{
echo "image have been store and save successfully.";
}
else if(!$uploadfile)
{
echo "image not upload";
}
else if(!$query)
{
echo "image not save";
}
}
else
{
echo "Invalid File Type";
}
}?>
<!DOCTYPE html>
<html>
<head>
<title>Upload Image</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
Category :
<SELECT name="category">
<option value="clothes">Clothes</option>
<option value="under">Under</option>
<option value="coat">Coat</option>
<option value="accessory">Accessory</option>
</SELECT>
ImageColor :
<input type="text" name="color">
Season :
<input type="text" name="season">
Pattern :
<input type="text" name="pattern">
Type :
<input type="text" name="type">
File:
<input type="file" name="file">
<input type="submit" value="Upload Data" name="save">
</form>
<table>
<thead>
<tr>
<th>ID</th>
<th>ImageNmae</th>
<th>ImageColor</th>
<th>Season</th>
<th>Pattern</th>
<th>Type</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
//$category = $_POST['category'];
$query ="SELECT * FROM " . $category;
$result = mysql_query($query) or die ("Query failed: ".mysql_error()." Actual query: ".$query);
while($row = mysql_fetch_object($result))
{
?>
<tr>
<td><?php echo $row->id ?></td>
<td><img style="width:200px;height:200px;"src="<?php echo 'upload/'. $row->name ?>"></td>
<td><?php echo $row->name ?></td>
<td><?php echo $row->color ?></td>
<td><?php echo $row->season ?></td>
<td><?php echo $row->pattern ?></td>
<td><?php echo $row->type ?></td>
<td><?php echo $row->imagetype ?></td>
<td>
<a href="edit.php?id=<?php echo $row->id?>">Edit</a>
<a href="delete.php?id=<?php echo $row->id?>">Delete</a>
</td>
</tr>
<?php
}
die(mysql_error());
?>
</tbody>
</table>
</body>
</html>
I can only insert successfully in table "clothes"(default). If somebody can tell what's wrong with the query, that would be so great!! Thanks for any help or suggestion. :)
Try this one
switch ($_POST['category'])
{
case "clothes":
$category="clothes";
break;
case "under":
$category = "under";
break;
case "coat":
$category = "coat";
break;
case "accessory":
$category = "accessory";
break;
}
Please add button isset condition from the beginning of statement.ie.you try your php code in between
if(isset($_POST['save']))
{
// place your code here;
}
And uncomment the statement like this
$category = $_POST['category'];