Create file based on ID

I recently created a registration file based off the script for a login file that I found earlier today. I am trying to make it so that when a user registers, a file is created with the same name as their user ID, but I havn’t been able to figure out how to do this. Any help is appreciated.

For some reason I made a thread a few hours ago and it was suddenly deleted without any sort of warning, I have no idea why though :frowning:

[php]<?php
ob_start();
$host=“localhost”; // Host name
$username=“root”; // Mysql username
$password=“pass”; // Mysql password
$db_name=“members”; // Database name
$tbl_name=“members”; // Table name

// Connect to server and select databse.
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);

// Define $myusername and $mypassword
$myusername=$_POST[‘myusername’];
$mypassword=$_POST[‘mypassword’];
$myemail=$_POST[‘myemail’];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myemail = stripslashes($myemail);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$myemail = mysql_real_escape_string($myemail);

$sql=“INSERT INTO $tbl_name (username, password, email) VALUES (’$_POST[myusername]’,’$_POST[mypassword]’,’$_POST[myemail]’)”;

$result=mysql_query($sql);

header(“location:index.php”);

$id;
$file=fopen(“profile’$id’.php”,“x+”);

ob_end_flush();
?>
[/php]

Have you tried removing the single quotes in the fopen function around the $id? Since you are using the double quotes, any variable within the double quotes will output the value so single quotes aren’t needed. Try that and tell me if that works.

Also, sorry that your post was deleted earlier. Sometimes glitches happen.

I have tried no quotes, but then it created a file with the variable name in the title. The problem is that I can’t quite figure out how to get the ID number from the user that was just registered and create a file for them.

Well, how most people record user id’s would be to create a column in the database that is primary and auto_increments. Each user you enter into the database, the database adds the id itself depending on how may users where added to the database since the columns conception. From there it’s just grabbing the id from the database and using it.

I already did all of that before I made this thread. I made the database, I made the ID column, i made it auto increment, and I made it primary.

From there it's just grabbing the id from the database and using it.
My problem is I don't know how to do this part

[php]
$sql = “SELECT id FROM $tbl_name WHERE username=’$username’”;
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$id = $row[‘id’];

[/php]

Of course substitute $username and $tbl_name with the variables that house them and you should be peachy. Hope this helps =D.

i think ive understood what you want,
to create the file

Replace:
$file=fopen(“profile’$id’.php”,“x+”);

With

$File = “profile$id.php”;
$Handle = fopen($File, ‘w’) or die(“can’t open file”);
fclose($Handle);

Hope that helps

Sponsor our Newsletter | Privacy Policy | Terms of Service