brandon99999 Posted May 5, 2007 Share Posted May 5, 2007 It's been bugging me that I can't seem to store user data if I don't know how to insert certain data into the correct user's table. It's hard to explain. But here is an example of the last insert code I tried: <?php $con = mysql_connect("mysqlserver","hdfhj","dhjgjfj"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("users", $con); $username = $_COOKIE['ID_my_site']; mysql_query("INSERT INTO echo $username(Pokemon) VALUES ('Peter')"); mysql_close($con); ?> The $username variable is suppose to be whatever user is logged in. I think it's impossible to echo out a variable in a mysql/php code. If there is another way to insert a user's data in a user's table please tell me how. Quote Link to comment https://forums.phpfreaks.com/topic/50173-solved-how-to-echo-a-variable-when-inserting-data-into-a-database/ Share on other sites More sharing options...
MadTechie Posted May 5, 2007 Share Posted May 5, 2007 What are you trying to insert ? whats the table name, field and data ? PS don't used echo just use the varible ie mysql_query("INSERT INTO TABLENAME (FIELD) VALUES ('$data')"); Quote Link to comment https://forums.phpfreaks.com/topic/50173-solved-how-to-echo-a-variable-when-inserting-data-into-a-database/#findComment-246338 Share on other sites More sharing options...
brandon99999 Posted May 5, 2007 Author Share Posted May 5, 2007 Actually I am just testing to see if I can insert stuff like the Pokemon, Money, Badges, ect. a user has cause I'm making an online rpg. In the testing code, I made a table called Brandon which is my name. So I tried to insert data in that table by using the $username variable. Also in the "Brandon" table there are fields called Pokemon, Money Badges, ect. It's hard to explain but I tried just using the variable without the echo in front of it and it didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/50173-solved-how-to-echo-a-variable-when-inserting-data-into-a-database/#findComment-246342 Share on other sites More sharing options...
MadTechie Posted May 5, 2007 Share Posted May 5, 2007 Heres some example, i hope they help <?php $user = "test1"; $theField = "Pokemon"; mysql_query("INSERT INTO Brandon (Pokemon) VALUES ('some data')"); //or mysql_query("INSERT INTO Brandon (Pokemon) VALUES ('$user')"); //insert test1 //or mysql_query("INSERT INTO Brandon ($theField) VALUES ('$user')"); //insert test1 ?> Quote Link to comment https://forums.phpfreaks.com/topic/50173-solved-how-to-echo-a-variable-when-inserting-data-into-a-database/#findComment-246349 Share on other sites More sharing options...
brandon99999 Posted May 5, 2007 Author Share Posted May 5, 2007 Actually, I just want the variable to be the user logged in. Say for example I login with my username being Brandon, then I click on a link that leads me to that mysql/php code page that I previously posted at the top. Then the info that was sent in that code should go to the Brandon table right? But it doesn't. When I tried the code you gave me it said this: Parse error: syntax error, unexpected T_STRING in /home/.nivie/brandon99999/thepokemonfrontier.com/example.php on line 14 Quote Link to comment https://forums.phpfreaks.com/topic/50173-solved-how-to-echo-a-variable-when-inserting-data-into-a-database/#findComment-246352 Share on other sites More sharing options...
MadTechie Posted May 5, 2007 Share Posted May 5, 2007 double check you entered it correctly Quote Link to comment https://forums.phpfreaks.com/topic/50173-solved-how-to-echo-a-variable-when-inserting-data-into-a-database/#findComment-246368 Share on other sites More sharing options...
brandon99999 Posted May 5, 2007 Author Share Posted May 5, 2007 Yea, it's entered correctly. <?php $con = mysql_connect("myfshgjsh","djdgjkjk","2847g73"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mydbbbbb", $con); $user = "test1" mysql_query("INSERT INTO Brandon (Pokemon) VALUES ('$user')"); mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/50173-solved-how-to-echo-a-variable-when-inserting-data-into-a-database/#findComment-246373 Share on other sites More sharing options...
MadTechie Posted May 6, 2007 Share Posted May 6, 2007 yep, but you missed the ; on the $user = "test1" change to $user = "test1"; Quote Link to comment https://forums.phpfreaks.com/topic/50173-solved-how-to-echo-a-variable-when-inserting-data-into-a-database/#findComment-246508 Share on other sites More sharing options...
brandon99999 Posted May 6, 2007 Author Share Posted May 6, 2007 Thanks Mad Techie, it works now! Now that you gave me that code, I now know how to user vaiables in my mysql/php codes. This means that I can release the Rpg quite soon. This topic is solved. Quote Link to comment https://forums.phpfreaks.com/topic/50173-solved-how-to-echo-a-variable-when-inserting-data-into-a-database/#findComment-246562 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.