为mysql和php在线相册创建相册

时间:2015-03-17 02:00:29

标签: php mysql mysqli phpmyadmin

我正在创建一个支持mysql的在线相册并使用php访问我的mysql数据库,我在创建一个允许用户创建新相册的表单时遇到了麻烦,到目前为止,我有以下代码:

<h1>My Photo Collection</h1>
<h3>Add, search and  browse through our collection!</h3>
<section id="collection">
<h4>Create Album</h4>
<form method='post'>
        <?php
        require_once 'config.php'; 
        $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
        if(isset($_POST['title'])){
        $name = $_POST['title'];
        if(empty($name)){ 
            echo "Please enter album name <br/>";
        } else{
            $mysqli->query("INSERT INTO albums VALUES('', '$title')");
            echo "Album was successfully created <br/>";
        }
    }
    ?>
    Album Name: <input type="text" name="title" /> <input type="submit" value="create" />
</form>
</section>

我的'相册'表的架构是: title,date_created,date_modified(时间戳),albumID(主键)

进一步的问题: 如何允许用户输入日期而不是表格(并确保其格式正确),date_modified时间戳如何工作(我需要将其放在表格中吗?)

我在我的php查询中使用mysqli而不是mysql。 到目前为止,我的代码返回“未定义的对象'标题'” 任何人都可以帮助我,弄清楚我的代码出了什么问题吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

目前,$title未定义,您已将$_POST['title']分配给$name。由于您使用的是MySQLi API,请改用预编译语句。

至于日期,只需使用NOW()

<?php

require_once 'config.php'; 
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if(isset($_POST['submit'])) {
    if(empty($_POST['title'])) {
        echo 'Please enter album name <br/>';
    } else {
        $name = $_POST['title'];
        $insert = $mysqli->prepare('INSERT INTO albums (title, date_created, date_modified) VALUES(?, NOW(), NOW())') or die($mysqli->error);
        $insert->bind_param('s', $name);
        if($insert->execute()) {
            echo 'Album was successfully created <br/>';
        } else {
            echo $mysqli->error;
        }

    }
}

?>

<h1>My Photo Collection</h1>
<h3>Add, search and  browse through our collection!</h3>
<section id="collection">
    <h4>Create Album</h4>
    <form method='post'>
        Album Name: <input type="text" name="title" /> 
        <input type="submit" name="submit" value="create" />
    </form>
</section>