Jump to content

[SOLVED] How to echo a variable when inserting data into a database?


brandon99999

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.