用户变量表单以插入数据


user variable form to insert data

我想使用一个变量来接收要插入数据的表单。我写了一篇关于壁橱数据库的文章。

<?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>

我只能在表中成功插入"衣服"(默认)。如果有人能说出查询出了什么问题,那就太好了!!感谢您的帮助或建议。:)

试试这个

switch ($_POST['category'])
{
    case "clothes":
        $category="clothes";
        break;
    case "under":
        $category = "under";
        break;
    case "coat":
        $category = "coat";
        break;
    case "accessory":
        $category = "accessory";
        break;
}

请从语句的开头添加按钮isset条件。例如,在之间尝试php代码

if(isset($_POST['save']))
{
     // place your code here;
}

并取消注释像这样的语句

$category = $_POST['category'];