-3
我想用變量來接收插入數據的表單。 我寫了一個關於壁櫥數據庫。用戶插入數據的變量形式
<?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>
我只能在表格「clothes」(默認)中成功插入。 如果有人可以告訴查詢有什麼問題,那就太好了! 感謝您的任何幫助或建議。 :)
在開關位置使用if-else。並檢查$ _POST ['category']'對於不同的表單是不同的提交與否.. – Avishake
感謝您的幫助。 我厭倦了你的建議。 看來我的查詢有問題。 它顯示「查詢失敗:您的SQL語法中有錯誤」 在我的查詢中是否有任何問題:< – Echo
做一件事,讓您的變量像''「。$ name。」''。這是PHP連接。我發現你編寫SQL的方式是正確的,但PHP有時無法處理它。使用上面顯示的變量。它不應該是任何錯誤。 – Avishake