I’m trying to write to the database after authenticating, but unable to.
Once I log in successfully, I redirect to 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>
From here, I want to capture a name and a date and submit, but every time I submit, I look inside phpmyadmin and the table cells are blank. I’m running this locally using xampp. Here is the code where I am trying to capture input from the form:
<?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);
}
}
Any help is appreciated
2
Answers
The form data has
name
attribute with values "nickname" and "startdate". I guess you should be using them correctly to construct the insert query with$_POST["nickname"]
&$_POST["startdate"]
If you’re still facing issue, then please post the full error log to debug.
There seem to be a few issues in your code that could be causing the problem.
In your form, the input field for the nickname is using the id attribute as "nickName," but in your PHP code, you are accessing it using
$_POST["name"]
. Update$_POST["name"]
to$_POST["nickname"]
to match the input field name.You are checking if
$_POST["date"]
is empty, but in your form, the name attribute for the date input field is "startdate." Update$_POST["date"]
to$_POST["startdate"]
to match the input field name.In the SQL query, you are inserting the value of the nickname into the user table, but you haven’t specified the column to insert the value into.
You are binding the parameters using
$stmt->bind_param("sss", $_POST["name"]);
, but since you only have one parameter to bind, you should uses
instead ofsss
. Update the line as follows:After making these changes, your code should look like this:
Hope this helps