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....
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.
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]
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]
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.