JP128 Posted December 11, 2006 Share Posted December 11, 2006 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 https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/ Share on other sites More sharing options...
simcoweb Posted December 11, 2006 Share Posted December 11, 2006 How is the $user variable being set? Link to comment https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138850 Share on other sites More sharing options...
JP128 Posted December 11, 2006 Author Share Posted December 11, 2006 $user = "jtelinks"; Link to comment https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138851 Share on other sites More sharing options...
simcoweb Posted December 11, 2006 Share Posted December 11, 2006 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 https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138854 Share on other sites More sharing options...
JP128 Posted December 11, 2006 Author Share Posted December 11, 2006 its set by a post...$user = $_POST['username']; Link to comment https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138856 Share on other sites More sharing options...
simcoweb Posted December 11, 2006 Share Posted December 11, 2006 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 https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138857 Share on other sites More sharing options...
JP128 Posted December 11, 2006 Author Share Posted December 11, 2006 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''jtelinks' Link to comment https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138862 Share on other sites More sharing options...
simcoweb Posted December 11, 2006 Share Posted December 11, 2006 Try removing the single quotes from $user in your mysql statement. Link to comment https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138865 Share on other sites More sharing options...
JP128 Posted December 11, 2006 Author Share Posted December 11, 2006 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Link to comment https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138866 Share on other sites More sharing options...
littlegiant Posted December 11, 2006 Share Posted December 11, 2006 try$sql = "INSERT INTO " . $user . " VALUES('" . $linkname . "','" . $linkroute . "')";you might want to try echo'ing the sql, so you know what its like.[edit] needed a space. Link to comment https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138867 Share on other sites More sharing options...
simcoweb Posted December 11, 2006 Share Posted December 11, 2006 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 https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138869 Share on other sites More sharing options...
JP128 Posted December 11, 2006 Author Share Posted December 11, 2006 That doesnt matter if you are setting all of the values that are in a table, and in this case I am. and I tried the " . $user . " still same error Link to comment https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138870 Share on other sites More sharing options...
littlegiant Posted December 11, 2006 Share Posted December 11, 2006 could you echo the sql so we can if theres anything wrong with it ? Link to comment https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138873 Share on other sites More sharing options...
simcoweb Posted December 11, 2006 Share Posted December 11, 2006 Are there in fact just two columns in that table? Link to comment https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138874 Share on other sites More sharing options...
JP128 Posted December 11, 2006 Author Share Posted December 11, 2006 Ok, I will try to echo. and there are just 2 columns. Link to comment https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138877 Share on other sites More sharing options...
trq Posted December 11, 2006 Share Posted December 11, 2006 This whole design reaks of poor design. Why do your users have there own tables? Its just no required!!!Find yourself a good tutorial on database normalization and read it. Link to comment https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138878 Share on other sites More sharing options...
JP128 Posted December 11, 2006 Author Share Posted December 11, 2006 Lol, i am just testing this stuff out.... I have a more complex one, but right now, i am trying to see how to get a table name from a variable. Link to comment https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138883 Share on other sites More sharing options...
trq Posted December 11, 2006 Share Posted December 11, 2006 OK... seems a waste of time though. How are you creating these tables? Are you sure the table exists with the user name you are trying to use? Link to comment https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138888 Share on other sites More sharing options...
simcoweb Posted December 11, 2006 Share Posted December 11, 2006 Also, try using an error reporting in your code so you can see the actual mysql errors. Like this:$sql = "INSERT INTO '$user' VALUES('$linkname','$linkroute')";mysql_query($sql) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138891 Share on other sites More sharing options...
JP128 Posted December 11, 2006 Author Share Posted December 11, 2006 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 https://forums.phpfreaks.com/topic/30205-inserting-data-into-a-table-using-a-variable-as-the-table-name/#findComment-138892 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.