duandian8110 2011-06-23 08:06
浏览 53
已采纳

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

Why isn't this form sending the selected category to mysql?

I am new to the <select> tag... 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'); ?>

with my product table setup as follows:

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;

Product save function:

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条回答 默认 最新

  • dongwei3151 2011-06-23 08:32
    关注

    You need to isolate where this is going wrong.

    a) between your html form and PHP? use

    var_dump($_POST);
    

    b) between PHP and the class you are using? use

    var_dump( $product );
    

    c) between your class and the sql dbal?

    echo $sql;
    

    d) something wrong with that sql which you could not spot?

    look at the last entry in your sql log file
    

    Or turn on mysql logging http://dev.mysql.com/doc/refman/5.0/en/server-logs.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值