Jump to content

Inserting data into a table using a variable as the table name.


JP128

Recommended Posts

Ok, this is another part of my code. I got all of the rows to show, and now, I would like the user to insert a link into their table.

[code]
<?php
if((isset($_POST['linkname'])&&(isset($_POST['linkroute'])))){
dbConnect();
echo"We are set";
$linkname = $_POST['linkname'];
$linkroute= $_POST['linkroute'];
$sql = "INSERT INTO '$user' VALUES('$linkname','$linkroute')";
mysql_query($sql);
}
?>
[/code]

The "we are set" echo does show up....
Link to comment
Share on other sites

Ok, but in that snippet of code there's no setting of the variable value like you've done with your $_POST statementss. So, if you're passing the value via a URL for example then you'd need a $_GET statement in there like this:

$user = $_GET['userid'];

If it's passed as a form field then add another $_POST statement for that.

Then the mysql insert statement would know what to populate for that variable.
Link to comment
Share on other sites

Cool. Then just add this line:

$user = $_POST['username'];

To your code like this:

[code]<?php
if((isset($_POST['linkname'])&&(isset($_POST['linkroute'])))){
dbConnect();
echo"We are set";
$user = $_POST['username'];
$linkname = $_POST['linkname'];
$linkroute= $_POST['linkroute'];
$sql = "INSERT INTO '$user' VALUES('$linkname','$linkroute')";
mysql_query($sql);
}
?>[/code]
Link to comment
Share on other sites

I just noticed, your sql statement is missing the column names. Right now, the way it's written, it's trying to insert the two values into a table. It should name them like this:

[code]<?php
$sql = "INSERT INTO $user ('col_1', 'col_2') VALUES('$linkname','$linkroute')";
mysql_query($sql)
?>[/code]
Link to comment
Share on other sites

I did that... and I already posted my errors. What I did now was just created two if loops to test the only two users that will be using it... and if one used it, then it would use the table in that if... I don't but for now, it works.
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.