为什么这个PHP表单不起作用?

时间:2011-06-23 08:06:16

标签: php mysql forms

为什么这个表单没有将所选类别发送给mysql?

我是<select>代码的新手...... http://www.pastie.org/2110032

<?php
require_once('../../includes/initialize.php');


if (!$session->is_logged_in()) { redirect_to("login.php"); }
?>

<?php 
$max_file_size = 1048576;

if(isset($_POST['submit'])){
    $product = new Product();
    $product->caption = $_POST['caption'];
    $product->category = $_POST['category'];
    $product->attach_file($_FILES['file_upload']);
    if($product->save()) {
        $session->message("product uploaded successfully.");
        redirect_to('list_products.php');
    } else {
        $message = join("<<br />", $product->errors);
    }
}
?>

<?php include_layout_template('admin_header.php'); ?>

<h2>Product Upload</h2>

<?php echo output_message($message); ?>

<form action="product_upload.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size;?>" />
<p><input type="file" name="file_upload" /></p>
<p>Caption: <input type="text" name="caption" value="" /></p>
<p>Category: 
<select name="category">
<option value="Pins">Pins</option>
<option value="Busings">Bushings</option>
<option value="Miscellaneous">Miscellaneous</option>
<option value="Ejector Sleeves">Ejector Sleeves</option>
<option value="Polishing">Polishing</option>
<option value="End Mills">End Mills</option>
</select></p>
<input type="submit" name="submit" value="Upload" />
</form>

<?php include_layout_template('admin_footer.php'); ?>

我的产品表设置如下:

mysql> describe products;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| category | varchar(255) | NO   |     | NULL    |                |
| filename | varchar(255) | NO   |     | NULL    |                |
| type     | varchar(100) | NO   |     | NULL    |                |
| size     | int(11)      | NO   |     | NULL    |                |
| caption  | varchar(255) | NO   |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)



public function create() {
        global $database;
        $attributes = $this->sanitized_attributes();
        unset($attributes['id']);

      $sql = "INSERT INTO ".self::$table_name." (";
        $sql .= join(", ", array_keys($attributes));
      $sql .= ") VALUES ('";
        $sql .= join("', '", array_values($attributes));
      $sql .= "')";
        if($database->query($sql)) {
        $this->id = $database->insert_id();
        return true;
      } else {
        return false;
      }
    }

protected static $table_name="products";
    protected static $db_fields=array('id', 'category','filename', 'type', 'size', 'caption');
    public $id;
    public $category;
    public $filename;
    public $type;
    public $size;
    public $caption;

产品保存功能:

public function save() {
        // A new record won't have an id yet.
        if(isset($this->id)) {
            // Really just to update the caption
            $this->update();
        } else {
            // Make sure there are no errors

            // Can't save if there are pre-existing errors
          if(!empty($this->errors)) { return false; }

            // Make sure the caption is not too long for the DB
          if(strlen($this->caption) >= 255) {
                $this->errors[] = "The caption can only be 255 characters long.";
                return false;
            }
            if(strlen($this->category) >= 255) {
                $this->errors[] = "The category can only be 255 characters long.";
                return false;
            }

          // Can't save without filename and temp location
          if(empty($this->filename) || empty($this->temp_path)) {
            $this->errors[] = "The file location was not available.";
            return false;
          }

            // Determine the target_path
          $target_path = SITE_ROOT .DS. 'public' .DS. $this->upload_dir .DS. $this->filename;

          // Make sure a file doesn't already exist in the target location
          if(file_exists($target_path)) {
            $this->errors[] = "The file {$this->filename} already exists.";
            return false;
          }

            // Attempt to move the file 
            if(move_uploaded_file($this->temp_path, $target_path)) {
            // Success
                // Save a corresponding entry to the database
                if($this->create()) {
                    // We are done with temp_path, the file isn't there anymore
                    unset($this->temp_path);
                    return true;
                }
            } else {
            $this->errors[] = "The file upload failed, possibly due to incorrect permissions on the upload folder.";
            return false;
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

你需要找出出错的地方。

a)你的html表单和PHP之间?使用

var_dump($_POST);

b)PHP和你正在使用的类之间?使用

var_dump( $product );

c)你的班级和sql dbal之间?

echo $sql;

d)那个你无法发现的sql出了什么问题?

look at the last entry in your sql log file

或启用mysql logging http://dev.mysql.com/doc/refman/5.0/en/server-logs.html

答案 1 :(得分:0)

你有任何错误吗?重定向可能会失败,因为php部分之间有一个空行,涉及重定向。你有没有完成$ POST变量的print_r来检查你认为它们是什么值?除了$ product-&gt; save()因为没有可见的代码,你确认它的形成是正确的并且执行没有错误吗?