View Full Version : Help in Mysql

12-01-2010, 07:12 PM
Hi Guys

I have two tables in my mysql

1- user which includes

id username password email nearest_station_name nearest_station_postcode

2- stations table which includes

id station_name station_postcode

I have a drop down list which populates data from stations table in html but what I need to do is once user has choosen the nearest station, then i do a query to insert these datas to my users table

so far I have the code below but what happens is when i add the table it only adds the id of stations table

could you please help me with this?

if ($_POST['register'])
$nearest_station_name = addslashes(strip_tags($_POST['station']));
$nearest_station_postcode = addslashes(strip_tags($_POST['postcode']));

$submit = mysql_query("INSERT INTO users (nearest_station_name ,nearest_station_postcode) VALUES ('$nearest_station_name', '$nearest_station_postcode')");

<form action='add.php' method='POST'>

Nearest Station 1<select name='station'><p />

$stationsdropdown=mysql_query("SELECT id ,stationname, postcode FROM stations");

while($row = mysql_fetch_array($stationsdropdown))
// echo '<option value="' .$row['stationname']. '"></option>' ;
echo "<option value=\"".$row['id']."\">".$row['stationname']."\n ";





the problem is that first of all postcode shows in the same drop down list which it shouldnt and second, I need to add details to both field which are station name and postcode to user table

can someone help me with this please as im a newbie :)

thanks in advance

12-01-2010, 07:30 PM
A few things off the bat:

You say your table has station_name and station_postcode, but you refer to them later as stationname and postcode.

addslashes(strip_tags($_POST['station'])) is bad form. Use mysql_real_escape_string($_POST['station']) for example.

I would recommend using ` backticks arounds table and field names in your SQL to avoid ambiguity.

You shouldn't have <p /> after the <select>, <select> can only contain <option>, and <p /> should be <p>content</p>

mysql_fetch_array is wasteful as-is. You're using it like an associative array, so use mysql_fetch_assoc

12-01-2010, 11:27 PM
hi, thanks but still i dont know how to do it

12-01-2010, 11:32 PM
Alright, have you made any changes as suggested? If so, post your new code.