我正在嘗試在認證後向資料庫寫入數據,但無法成功。
一旦成功登錄,我會重定向到index.php
#<?php session_start(); if (isset($_SESSION["user_id"])) { $mysqli = require __DIR__ . "/database.php"; $sql = "SELECT * FROM user WHERE id = {$_SESSION["user_id"]}"; $result = $mysqli->query($sql); $user = $result->fetch_assoc(); } ?> <!DOCTYPE html> <html> <head> <title>Home</title> <meta charset="UTF-8"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"> </head> <body> <h1>Home</h1> <?php if (isset($user)): ?> <p>Hello <?= htmlspecialchars($user["name"]) ?></p> <form action="user-input.php" method="post" id="availDates"> <label for="nickName">Nickname:</label> <input id="nickName" name="nickname" /> <label for="startDate">Start Date:</label> <input type="date" id="startDate" name="startdate"> <button type="submit">Submit</button> </form> <p><a href="logout.php">Log out</a></p> <?php else: ?> <p><a href="login.php">Log in</a> or <a href="signup.html">sign up</a></p> <?php endif; ?> </body> </html>
從這裡開始,我想捕獲一個名字和一個日期並提交,但每次提交後,我在phpmyadmin中查看,表格單元格都是空白的。我正在使用xampp在本地運行。以下是我嘗試從表單中捕獲輸入的程式碼:
<?php if (empty($_POST["nickname"])) { die("Name is required"); } if (empty($_POST["date"])) { die("Name is required"); } $mysqli = require __DIR__ . "/database.php"; $sql = "INSERT INTO user (nickname) VALUES (?)"; $stmt = $mysqli->stmt_init(); if ( ! $stmt->prepare($sql)) { die("SQL error: " . $mysqli->error); } $stmt->bind_param("sss", $_POST["name"]); if ($stmt->execute()) { echo "successful"; } else { if ($mysqli->errno === 1062) { die("error"); } else { die($mysqli->error . " " . $mysqli->errno); } }###任何幫助都將不勝感激###
表單資料具有帶有值為「nickname」和「startdate」的
name
屬性。我猜你應該正確使用它們來建立插入查詢,例如$_POST["nickname"]
和$_POST["startdate"]
#如果你仍然遇到問題,請發布完整的錯誤日誌進行偵錯。