/    Sign up×
Community /Pin to ProfileBookmark

INSERT INTO table help please

Hi, i have 2 tables in a database, both tables are identical exept for the name

i have a form in a html file, it feeds the data to a php file that connect to the database and inserts the data in one of the tables.

i want the users to choose which table in the database to submit the data to, therefore i added a dropdown menu with the names of the 2 tables in the form.

now the part im strugling with, i need a code to add into the php so that the line;

[COLOR=”Lime”]INSERT INTO table1[/COLOR]

becomes like this

[COLOR=”lime”]INSERT INTO {$table_chosen}[/COLOR] [COLOR=”Silver”]//$table_choosen will become the value selected by the user from the form drop down menu.[/COLOR]

this is my html code

[code=html]

<table border=”1″ width=”36%”>
<tr>
<td valign=”top” width=”109″>Album Name</td>
<td valign=”top”>
<form method=”POST” action=”insert.php”>
<input type=”text” name=”album_name” size=”32″></td>
</td>
</tr>
<tr>
<td width=”109″>Artist</td>
<td><input type=”text” name=”album_artist” size=”32″></td>
</tr>
<tr>
<td width=”109″>Screenshot Url</td>
<td><input type=”text” name=”album_ssurl” size=”32″></td>
</tr>
<tr>
<td width=”109″>Latin Unit</td>
<td><input type=”text” name=”album_mirror0″ size=”32″></td>
</tr>
<tr>
<td width=”109″>Rapid Share</td>
<td><input type=”text” name=”album_mirror1″ size=”32″></td>
</tr>
<tr>
<td width=”109″>Deposit Files</td>
<td><input type=”text” name=”album_mirror2″ size=”32″></td>
</tr>
<tr>
<td width=”109″>Torrent</td>
<td><input type=”text” name=”album_mirror3″ size=”32″></td>
</tr>
<tr>
<td width=”109″>Category</td>
<td>&nbsp;

<p><select size=”1″ name=”album_tables”>
<option selected value=”albums_country”>Country</option>
<option value=”albums_alternative”>Alternative</option>
</select></p>

</td>
</tr>
<tr>
<td colspan=”2″ valign=”middle”>
<p align=”center”>
<input type=”submit” value=”Submit” name=”B1″><input type=”reset” value=”Reset” name=”B2″>
</form>
</td>
</tr>
</table>

[/code]

and this is my php file

[code=php]
<?php
$con = mysql_connect(“mysql1034.domain.net”,”albums”,”password”);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

mysql_select_db(“albunes”, $con);

$sql=”INSERT INTO albums_country (album_name, album_artist, album_ssurl, album_mirror0, album_mirror1, album_mirror2, album_mirror3)
VALUES
(‘$_POST[album_name]’,’$_POST[album_artist]’,’$_POST[album_ssurl]’,’$_POST[album_mirror0]’,’$_POST[album_mirror1]’,’$_POST[album_mirror2]’,’$_POST[album_mirror3]’)”;

if (!mysql_query($sql,$con))
{
die(‘Error: ‘ . mysql_error());
}
echo “1 added!”;

mysql_close($con)
?>
[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@PETTESep 27.2009 — The first idea would be storing the selected table into a variable and then inporting into the query as below.

[code=php]<?php
$con = mysql_connect("mysql1034.domain.net","albums","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("albunes", $con);


$album_table_selected = $_REQUEST["album_tables"];
$sql="INSERT INTO ".$album_table_selected." (album_name, album_artist, album_ssurl, album_mirror0, album_mirror1, album_mirror2, album_mirror3)
VALUES
('$_POST[album_name]','$_POST[album_artist]','$_POST[album_ssurl]','$_POST[album_mirror0]','$_POST[album_mirror1]','$_POST[album_mirror2]','$_POST[album_mirror3]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 added!";

mysql_close($con)
?> [/code]


But the above idea is such a very bad idea since your code can be edited to point to any random table in the database.

So it would be better this way.

Replacing this part in HMTL;
[code=html]<p><select size="1" name="album_tables">
<option selected value="0">Country</option>
<option value="1">Alternative</option>
</select></p>
[/code]


And using this in PHP:

[code=php]<?php
$con = mysql_connect("mysql1034.domain.net","albums","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("albunes", $con);


$album_table_selected = ($_REQUEST["album_tables"])?"country":"alternative";
$sql="INSERT INTO ".$album_table_selected." (album_name, album_artist, album_ssurl, album_mirror0, album_mirror1, album_mirror2, album_mirror3)
VALUES
('$_POST[album_name]','$_POST[album_artist]','$_POST[album_ssurl]','$_POST[album_mirror0]','$_POST[album_mirror1]','$_POST[album_mirror2]','$_POST[album_mirror3]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 added!";

mysql_close($con)
?> [/code]


I haven't tested the codes. Just edited it here.
Copy linkTweet thisAlerts:
@LatinUnitauthorSep 27.2009 — Hi there pette, thanks alot for the help, another friend already helped me out with it and my code works now, i edited only the php code, added an array here it is

[code=php]
<?php
$con = mysql_connect("mysql1034.****.net","albunes","a*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("albunes", $con);

$tables = array('albums_reggaeton', 'albums_bachata');
if(!in_array($_POST['album_tables'], $tables)){
die("Invalid table name selected.");
}

$sql="INSERT INTO {$_POST['album_tables']} (album_name, album_artist, album_ssurl, album_mirror0, album_mirror1, album_mirror2, album_mirror3)
VALUES
('$_POST[album_name]','$_POST[album_artist]','$_POST[album_ssurl]','$_POST[album_mirror0]','$_POST[album_mirror1]','$_POST[album_mirror2]','$_POST[album_mirror3]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 ALBUM AGREGADO!";

mysql_close($con)
?>
[/code]
Copy linkTweet thisAlerts:
@MindzaiSep 28.2009 — Your current code is extremely insecure. You should read up on SQL injection, and make sure to clean and validate (and in the case of building SQL queries like this, escape) all user input before using it.
×

Success!

Help @LatinUnit spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...