Having trouble writing contents into a file

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

<?php require '../login.php'; ?> generic style <?php echo "

" . $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 "
"; $newestSelector = "SELECT title, content FROM getName() WHERE id=$i"; $newestSelectorQuery = mysqli_query($connection, $newestSelector); while ($blogContent = mysqli_fetch_assoc($newestSelectorQuery)) { echo "

" . {$blogContent['title']} . "

"; echo {$blogContent['content']} ."
"; echo "
"; 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]

Your database design needs work. You should not be dynamically creating tables and each user does not need their own table. In order to Use session varibles, you need to start the session, first.

Well, I do not understand your code logic at all. You create a database table for a user and then write a page out, I will assume to create a different page for each user? Why waste all that database and server storage? Why not just fix one page that adjusts for each user using their data? Not sure what you are attempting to accomplish.

But, anyway,

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.

What line was the error message on? Please give us the error message so we can help.

Oh, and as Astonecipher mentioned, do you start your SESSION in the login.php file?

Sponsor our Newsletter | Privacy Policy | Terms of Service