Forum Moderators: coopster

Message Too Old, No Replies

Combobox write to mysql database

         

mag1

10:23 am on Jan 19, 2005 (gmt 0)

10+ Year Member



Hi, I've got a combobox which has 2 columns, the columns are filled from a table on a mysql database. The problem I have is that I now want to write these to a different table. Any ideas how this can be done?
Below is the code I'm using to fill the combobox:
<?php>
$db = mysql_connect("Server", "Username", "Password");
mysql_select_db("Db_name", $db);
$query = "SELECT X, Y FROM aTable";
$result = @mysql_query($query);
print "<select name=aName size = 1>";
if (mysql_num_rows($result) > 0) :
while ($row = mysql_fetch_array($result)) :
$A = $row[X];
$B = $row[Y];
print ("<option value=$A>$B ¦ $A </option>");
endwhile;
print "</select>";
endif;
?>

Thanks

dreamcatcher

11:55 am on Jan 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi mag1,

You need to use explode() to seperate the data if thats what you are trying to achieve:

$aName = $_POST['aName'];

$data = explode(" ¦ ", $aName);

In your example, $data would hold values in two slots.

echo $data[0];
echo $data[1];

$data[0] would contain the value of $B and $data[1] would contain the value of $A.

You will need to lose the value part of your option field though, as this only contains $A.

print ("<option>$B ¦ $A </option>");

Hope that helps. or have I misunderstood you?

dc

mag1

3:13 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



Thanks DC,

I'll try it out. I'm fairly new to this stuff so it could take a while.

Cheers.

dreamcatcher

9:40 pm on Jan 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No problem. Give it a try and if you have problems, post again.

mag1

11:09 am on Jan 21, 2005 (gmt 0)

10+ Year Member



Hi, I tried the above but I can't seem to get it working and I'm not sure that it's going to do what I want. What I have at the moment is a combobox that is filled with the values of 2 columns from a table i.e. each column in the combobox represents a column from the database. What I'd like to do is be able to write these 2 values to 2 seperate columns of a different table in the database. (Does that make sense?) I don't know if the explode() function will do that or not.

Thanks,

dreamcatcher

11:36 am on Jan 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The explode function will seperate the data for you and assign the values into an array. What you need to do then is write each piece of information to whichever table you want to info writing to.

For example, lets say your drop down menu shows the following:

print ("<option>Second Value ¦ First Value</option>");

If you echoed the value of $_POST['aName'] it would show:

Second Value ¦ First Value

The explode function splits the data based on a seperation point which you define. Here your data is split using ¦, so you would do:

$data = explode(" ¦ ", $_POST['aName']);

You now have two seperate values in the $data array.

$data[0] == "Second Value"
$data[1] == "First Value"

Use the $data variables to add the info wherever you want in your db.

INSERT INTO table (field1,field2) VALUES ('$data[0]','$data[1]');

Hope that made some sense.

mag1

12:07 pm on Jan 21, 2005 (gmt 0)

10+ Year Member



Thanks DC.

Got it working now.

Cheers.

dreamcatcher

12:12 pm on Jan 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You`re welcome mag1, glad you got it working. :)