Forum Moderators: coopster

Message Too Old, No Replies

Having trouble replacing a valuable ($id) in a template!

         

ToxinMan

2:07 am on Jan 26, 2008 (gmt 0)

10+ Year Member



Hey there guys!
I am having a little trouble trying to write the 'id' from mysql to my newly created $username.html from a template

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

phparion

5:11 am on Jan 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



your INSERT command shows there is no ID column in the table that you are passing values to. ARE YOU SURE THIS COLUMN EXISTS? it should be a primary key of the table.

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.

ToxinMan

5:32 am on Jan 26, 2008 (gmt 0)

10+ Year Member



id is set to auto incrediment so that when a new row appears it sets itself

ToxinMan

7:08 am on Jan 26, 2008 (gmt 0)

10+ Year Member



Well basicly all im trying to do is get the 'ID' from the auto incrediment when the user submits its info and pull that auto incredment number out into a valuble so i can have it stored on there userpage

Any ideas still?

jatar_k

12:42 pm on Jan 26, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



does this help?
[php.net...]

coopster

3:34 pm on Jan 26, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There is a note of caution on that page, too. Rather than use the API function from MySQL you are best served running your second query as follows:

Change ...

////////2. Finding id! 
$result = mysql_query("SELECT * FROM users WHERE username = '$username'");
while($row = mysql_fetch_array($result)){
$id = $row['id'];
}

... to this ...
$result = mysql_query('SELECT LAST_INSERT_ID() AS lastInsertID FROM users'); 
if ($row = mysql_fetch_array($result)) {
$id = $row['lastInsertID'];
}

Related reading:
[webmasterworld.com...]

cameraman

5:59 pm on Jan 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I'm understanding correctly, you want to actually replace this phrase:
$id = "id hasen't been replaced";

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.