I'm using PHP, HTML, and MySQL to create a web page that lets users create text posts. I would like to avoid js and ajax for now.
When calling the function "post" from (post.php) the system is unable to complete the post. Removing the function line in (includes/post.inc.php) lets the user post correctly. I want to have the function line so I can reference multiple methods on this file. What am I messing up for the call of this function?
Here is the relevant information from post.php
<?php
require 'includes/dbh.inc.php';
require 'includes/post.inc.php';
session_start();
date_default_timezone_set('America/Chicago');
.
.
echo '
<form action="'.post($conn).'" method="post">
<label for="text">New Post</label>
<input type="text" name="Text" placeholder="Write your post here">
<input type="hidden" name="Date" value="'.date("Y-m-d H:i:s").'">
<br>
<button type="submit" name="postSubmit">Post</button>
</form>
';
?>
Then here is the post function from includes/post.inc.php
<?php
function post($conn) { // deleting this line allows my code to run correctly
session_start();
if (isset($_POST['postSubmit'])) {
require 'dbh.inc.php';
$UserID = $_SESSION['UserID'];
$Date = $_POST['Date'];
$Text = $_POST['Text'];
$SectionID = 1;
// make a random value for $postID and then see if it exists in the DB
$PostIDLength = 11;
$PostIDString = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$PostIDDuplicate = true;
while ($PostIDDuplicate = true) {
$PostID = substr(str_shuffle($PostIDString), 0, $PostIDLength); //shuffle String, start with 0, 11 characters long
$PostIDSQL = "SELECT PostID FROM PostTable WHERE PostID = ?";
$PostIDStmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($PostIDStmt, $PostIDSQL)) {
exit();
}
else {
mysqli_stmt_bind_param($PostIDStmt, "s", $PostID);
mysqli_stmt_execute($PostIDStmt);
$PostIDResult = mysqli_stmt_get_result($PostIDStmt);
$PostIDResultCheck = mysqli_num_rows($PostIDResult);
if ($PostIDResultCheck <= 0) {
$PostIDDuplicate = false;
break;
}
else {
// repeat until you have a unique ID
}
}
}
$PostSQL = "INSERT INTO PostTable (PostID, UserID, SectionID, Date, Text) VALUES (?, ?, ?, ?, ?)";
$PostSTMT = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($PostSTMT, $PostSQL)) {
echo 'Error2';
}
else {
mysqli_stmt_bind_param($PostSTMT, "ssiss", $PostID, $UserID, $SectionID, $Date, $Text);
mysqli_stmt_execute($PostSTMT);
}
header('Location: ../home');
exit();
}
else {
header("Location: ../home.php?error");
exit();
}
}