Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php和mySQL将表单数据输入到数据库_Php_Html_Mysql_Forms - Fatal编程技术网

使用php和mySQL将表单数据输入到数据库

使用php和mySQL将表单数据输入到数据库,php,html,mysql,forms,Php,Html,Mysql,Forms,所以我尝试创建一个表单,将图像、描述和日期上传到数据库中。一切正常,但无论我怎么做,日期都保持在0000-00-00,有什么想法吗?大部分时间我都在遵循一个教程,但它没有添加日期,我想这样做,因为以后我希望能够在提交日期之前调用这些条目 <?php require('check.php'); require_once('settings.php'); include('header.php'); // Create database connectio

所以我尝试创建一个表单,将图像、描述和日期上传到数据库中。一切正常,但无论我怎么做,日期都保持在0000-00-00,有什么想法吗?大部分时间我都在遵循一个教程,但它没有添加日期,我想这样做,因为以后我希望能够在提交日期之前调用这些条目

    <?php require('check.php');
    require_once('settings.php'); 
    include('header.php');
      // Create database connection
      $db = mysqli_connect("localhost", "-----", "-----", "----");

      // Initialize message variable
      $msg = "";

      // If upload button is clicked ...
      if (isset($_POST['upload'])) {
        // Get image name
        $image = $_FILES['image']['name'];
        // Get text
        $image_text = mysqli_real_escape_string($db, $_POST['image_text']);
        // Get Date
        $sermon_date = mysqli_real_escape_string($db, $_POST['sermon_date']);
        // image file directory
        $target = "images/sermons/".basename($image);

        $sql = "INSERT INTO images (image, image_text, sermon_date) VALUES ('$image', '$image_text', '$sermon_date')";
        // execute query
        mysqli_query($db, $sql);

        if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
            $msg = "Image uploaded successfully";
        }else{
            $msg = "Failed to upload image";
        }
      }
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
      $result = mysqli_query($db, "SELECT * FROM images");
    ?>

    <h1>Update Sermon Series</h1>
      <form method="POST" action="success.php" enctype="multipart/form-data">
        <input type="hidden" name="size" value="1000000">
        Sermon Graphic <br />
          <input type="file" name="image"><br /><br />

          <textarea 
            id="text" 
            cols="40" 
            rows="4" 
            name="image_text" 
            placeholder="Describe Sermon"></textarea><br />
    Beginning Date of Sermon:<br />
    <input type="date" name="sermon_date">
            <button type="submit" name="upload">POST</button>
      </form>
    </body>
    </html>

日期需要以
YYYY-MM-DD
格式保存到数据库中,而HTML中的日期字段应该已经这样做了

如果您的输入没有以这种格式发送日期(无论出于何种原因),您可以使用PHP将其转换为这种格式。下面的代码获取发布日期并将其转换为上述格式(在PHP中,这将转换为
Y-m-d


请注意,如果您接受的日期不是PHP可以检测到的标准格式,
strotime
可能不起作用。

db表中的
sermon\u date
字段类型是什么?您是否已从html表单中转储发布的值,以查看脚本中发布的内容?此问题和答案可能会有所帮助。查看问题的“编辑”部分,了解OP为解决问题做了哪些工作(本质上,插入前日期必须格式化为Y-m-d):您应该提供
创建表
语法。我正在查看什么是RToyo linkedf,我还编辑了我的帖子,以获得用于创建表的内容。字段类型是数据库中的日期
    $connect = new MySQLi($db_server, $db_user, $db_password, $db_database);
if ($connect->connect_error) {
    die("Connection failed: " . $connect->connect_error);
}

$sql = "CREATE TABLE images (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
image VARCHAR(100) NOT NULL,
image_text LONGTEXT NOT NULL,
sermon_date DATE NOT NULL
)";

if ($connect->query($sql) === TRUE) {
    echo "Table created successfully";
} else {
    echo "Error creating table:" . $connect ->error;
}
// add this above the insert query
$sermon_date = date('Y-m-d', strtotime($sermon_date));