I am trying to write text into a file but I am getting unexpected errors like Undefined variable. When I look at the code, it seems like my variables are set.
[php]<?php
require ‘login.php’;
error_reporting(E_ALL);
$name = $_SESSION[‘username’];
$pagename = $name;
$connectToUserDb = mysqli_select_db($connection, ‘mydb’);
$createUserTable = “CREATE TABLE IF NOT EXISTS $name (
id INT(3) PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
content VARCHAR(10000) NOT NULL
)”;
$createTableQuery = mysqli_query($connection, $createUserTable);
if (!$createTableQuery) {
echo "could not create your space " .mysqli_error($connection);
}
$newFileName = ‘./spaces/’.$pagename.".php";
$newFileContent = <<<CONTENT
" . $name . "
"; echo "Create a post
"; ?>recent posts
<?php $getEmptyId = "SELECT id FROM $name WHERE id >=1"; $getEmptyIdQuery = mysqli_query($connection, $getEmptyId); if (!$getEmptyIdQuery) { echo "there are 0 rows"; } else { $getLastId = <<=0; $i--) { echo "
" . {$blogContent['title']} . "
"; echo {$blogContent['content']} .""; echo "
"; echo "
"; } } } } ?>
View More
CONTENT;
$checkIfTrueOrFalse = “SELECT hasspace FROM ourusers WHERE username = ‘$name’”;
$checkIfTrueOrFalseQuery = mysqli_query($connection, $checkIfTrueOrFalse);
if (mysqli_num_rows($checkIfTrueOrFalseQuery) > 0) {
while ($value = mysqli_fetch_assoc($checkIfTrueOrFalseQuery)) {
if ($value[‘hasspace’]) { //if the value is false
}
else {
if(file_put_contents($newFileName,$newFileContent)!=false){
echo “File created (”.basename($newFileName).")";
$hasSpace = “UPDATE ourusers SET hasspace = TRUE WHERE username = ‘$name’”;
$updateToTrue = mysqli_query($connection, $hasSpace);
if (!$updateToTrue) {
echo “Could not update the table " .mysqli_error($connection);
}
}else{
echo “Cannot create file (”.basename($newFileName).”)";
}
}
}
}
[/php]