【问题标题】:php form does not save date into databasephp表单不将日期保存到数据库中
【发布时间】:2013-11-27 22:26:17
【问题描述】:

我的问题是我有一个表单应该将有关漫画书的信息保存到数据库中,它保存的信息是标题、描述等,而且它正在将漫画的图像上传到我的服务器。

现在它确实将图像上传到服务器,但它没有将任何信息放入我的表中,简单不知道为什么?

我是相当新的 php 和 mysql,所以也许这是一个简单的问题,但我无法弄清楚,而且我无法在网上找到答案。

我的表结构:

  1. id - int(11)
  2. 标题 - varchar(50)
  3. 说明 - 文字
  4. publicer - varchar(50)
  5. 图像 - varchar(30)
  6. 价格 - int(10)
  7. 状态 - tinyint(1)

我的表单在我的 index.php 上,看起来像这样:

<form method="post" action="newcomic.php" enctype="multipart/form-data">
<p>Comic name:
<br><input type="text" name="title"/></p>

<p>Description of the comic:
<br><textarea name="description"></textarea></p>

<p>Publicer:
<br><input type="text" name="publicer" /></p>

<p>Image:
<br><input type="file" name="image" /></p> 

<p>Price:
<br><input type="text" name="price" /></p>

<p><input type="submit" name="add" title="Add new comic to database" value="Add Comic"/></p>
</form>

我的 newcomic.php 文件看起来像这样

<?php

//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['image']['name']);

//This gets all the other information from the form
$title = $_POST['title'];
$description = $_POST['description'];
$publicer = $_POST['publicer'];
$image = ($_FILES['image']['name']);
$price = $_POST['price'];


// Connects to your Database
mysql_connect("localhost", "root", "root") or die(mysql_error()) ;
mysql_select_db("comic_express") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO products (id, title, description, publicer, image, price, status)
VALUES ('', '$title', '$description', '$publicer', '$image', '$price', '1')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>

希望任何人都可以帮助我:)

【问题讨论】:

  • 你可以尝试输出这样的mysql错误:mysql_query(...) or die(mysql_error());。我注意到的一件事是您正试图将一个字符串插入id,该字符串设置为 int(11)。如果 id 是一个自动递增的字段,请将其排除在您的 INSERT 语句之外。
  • 如果您的id 列设置为AUTO_INCREMENT,请删除查询中的第一个条目。 (编辑)似乎我们同时输入了相同的评论哈哈@showdev
  • 所以我现在感觉有点傻,我按照你们说的做了,删除了id,还是不行,尝试了mqsql_error,结果是我尝试的图像名称上传更长的 30 个字符,它是 31 -.- 但无论如何。感谢大家的帮助。

标签: php mysql database forms insert


【解决方案1】:

错误的 sql 语法。您正在尝试将空字符串放入 id。

【讨论】:

    【解决方案2】:

    您应该在查询执行中添加一些错误处理,以帮助查找正在发生的事情。

    在 php 中处理的基本 mysql 错误是这样的:

    <?php
    $link = mysql_connet(CREDS HERE);
    $query = "YOUR QUERY HERE";
    $result = mysql_query($query, $link);
    if(!$result)
        echo mysql_error();
    else
       //Query was successful, do whatever here
    ?>
    

    您总是希望确保查询成功,即使您确信它会成功。

    我相信 Guarana 也是正确的,只需取出 id(如果您正确设置表格,id 将自动生成),否则您将需要实际插入 id 而不是空字符串。

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      您必须在查询中使用 mysql_error() 函数。这样您就可以在您的 sql 查询字符串中找到弹出问题。

      使用给定的编辑代码并尝试。

       <?php
      
      //This is the directory where images will be saved
      $target = "images/";
      $target = $target . basename( $_FILES['image']['name']);
      
      //This gets all the other information from the form
      $title = $_POST['title'];
      $description = $_POST['description'];
      $publicer = $_POST['publicer'];
      $image = ($_FILES['image']['name']);
      $price = $_POST['price'];
      
      
      // Connects to your Database
      mysql_connect("localhost", "root", "root") or die(mysql_error()) ;
      mysql_select_db("comic_express") or die(mysql_error()) ;
      
      //Writes the information to the database
      mysql_query("INSERT INTO products (title, description, publicer, image, price, status)
      VALUES ('$title', '$description', '$publicer', '$image', '$price', '1')") or die(mysql_error()) ;
      
      //Writes the photo to the server
      if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
      
      //Tells you if its all ok
      echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
      }
      else {
      
      //Gives and error if its not
      echo "Sorry, there was a problem uploading your file.";
      }
      ?>
      

      哦,是的,您在整数类型字段中插入空白值。检查它是否是具有自动增量的主键。如果是,则离开此列意味着您无需插入它。

      【讨论】:

        猜你喜欢
        • 2016-03-16
        • 1970-01-01
        • 1970-01-01
        • 2017-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多