Forum Moderators: coopster
The template has this in the header:
$id = "id hasen't been replaced";
I was just wondering if any of you guys could help find any mistakes in this code as im only a begginer at php :)
here is a brief description on whats going on :)
1. = Posting the valuables that the user submitted into MYSQL
2. = I am now trying to find the 'id' that they created in MYSQL when submitting the valubles above in step 1!
3. = Now im trying to replace the $id that i found in MYSQL with the one in the template file
4. = After replacing the template $id above with the found MYSQL 'id' we are now writing a new userpage.html for the user!
CODE:
///////1. Posting data to mysql
if($_POST['submit']){
mysql_query("INSERT INTO users (username, firstname, lastname, password, email, gender) VALUES ('$username', '$firstname', '$lastname', '$password1', '$email', '$gender')");
////////2. Finding id!
$result = mysql_query("SELECT * FROM users WHERE username = '$username'");
while($row = mysql_fetch_array($result)){
$id = $row['id'];
}
/////////3. replacing $id in the template with mysql $id!
$new_member_file = str_replace($id, $id, $template);
/////////4. we are writting the new username.html to our server!
$fp = fopen(/home/rickyb/public_html/$username.html, "w");
fwrite($fp, $new_member_file);
fclose($fp);
Thanks in advance :)
Ricky
Also use mysql_query() or die(mysql_error()) to see what mysql spits out.
Also from the command line check the table with
desc tableName;
it will show you the columns of the tables with additional info.
Change ...
////////2. Finding id!
$result = mysql_query("SELECT * FROM users WHERE username = '$username'");
while($row = mysql_fetch_array($result)){
$id = $row['id'];
}
$result = mysql_query('SELECT LAST_INSERT_ID() AS lastInsertID FROM users');
if ($row = mysql_fetch_array($result)) {
$id = $row['lastInsertID'];
} in the template file with the actual ID that mysql gives to the record.
So if for example you just added person number 14, you want to wind up with
$id = 14;
when you write the file.
In that case, this:
/////////3. replacing $id in the template with mysql $id!
$new_member_file = str_replace($id, $id, $template);
isn't doing anything for you - you're looking for the generated number and replacing it with the generated number.
Follow coopster's advice above to get the value for $id.
then
$new_member_file=str_replace('"id hasen\'t been replaced"',$id,$template);
For the function call, search is the phrase you want to replace, and replace is the number you got.