Inserting time into MySQL Database with PDO
我正在研究一个Pastebin克隆类的东西(从头开始,不是字面上克隆pastebin,只是做一个替代方案)而且我遇到了插入数据库的时间问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php require 'connection.php'; $paste = $_POST['paste']; $title = $_POST['title']; //$sql ="INSERT INTO pasteinfo (title, paste) VALUES (:title, :paste)"; $stmt = $con->prepare("INSERT INTO pasteinfo (title, paste) VALUES (:title, :paste)"); echo"hi"; $stmt->bindParam(':paste', $paste); $stmt->bindParam(':title', $title); $stmt->execute(); echo"Pasted!"; $pastetime = new DateTime(); $timeQuery = $con->prepare("INSERT INTO pasteinfo (pastetime) VALUES (:pastetime)"); $time->bindParam(':pastetime', $pastetime); $con->exec($timeQuery); //$con = null; ?> |
那就是insert.php。 我希望当用户"粘贴"他们的粘贴时它将记录时间,然后在我的viewpaste.php上它将显示粘贴的标题,粘贴和时间。
它出什么问题了?
顺便说一句,只要忽略小回音"hi"; 扔在那里。 它帮助我解决了很多问题,并继续这样做。
connection.php来源:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php try { $con = new PDO('mysql:host=;dbname=;charset=utf8mb4','',''); $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); } catch (PDOException $ex){ echo $ex->getMessage();return false; } function retrieve($query,$input) { global $con; $stmt = $con->prepare($query); $stmt->execute($input); $stmt->setFetchMode(PDO::FETCH_OBJ); return $stmt; } |
@Drew:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php require 'connection.php'; $paste = $_POST['paste']; $title = $_POST['title']; $timeQuery ="SELECT NOW()"; //$sql ="INSERT INTO pasteinfo (title, paste) VALUES (:title, :paste)"; $stmt = $con->prepare("INSERT INTO pasteinfo (title, paste, pastetime) VALUES (:title, :paste, :pastetime)"); echo"hi"; $stmt->bindParam(':paste', $paste); $stmt->bindParam(':title', $title); $stmt->bindParam(':pastetime', $timeQuery); $stmt->execute(); echo"Pasted!"; //$timeQuery = $con->prepare("INSERT pasteinfo(pastetime) SELECT NOW()"); //$timeQuery->execute(); //$con = null; ?> |
运行脚本一次后的架构和结束状态:
架构:
1 2 3 4 5 6 7 | drop table if exists pasteinfo2; CREATE TABLE pasteinfo2 ( ai INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, paste TEXT NOT NULL, pastetime DATETIME NOT NULL ); |
PHP脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php // turn on error reporting, or wonder why nothing is happening at times error_reporting(E_ALL); ini_set("display_errors", 1); // Begin Vault // credentials from a secure Vault, not hard-coded (so the below is just for testing) $servername="localhost"; $dbname="so_gibberish"; $username="nate"; $password="cannonBall##)x"; // End Vault try { $pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $paste="Four score and seven years ago, our fore...."; $title="Gettysburg Address"; $stmt = $pdo->prepare("INSERT INTO pasteinfo2 (title, paste, pastetime) VALUES (:title, :paste, now())"); $stmt->bindParam(':paste', $paste); $stmt->bindParam(':title', $title); $stmt->execute(); echo"Yo I am here"; } catch (PDOException $e) { echo 'pdo problemo: ' . $e->getMessage(); // dev not production code exit(); } ?> |