Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Help in Mysql

  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Help in Mysql

    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 />

    <?php
    $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 ";
    $nearest_station_postcode=$row['postcode']

    }

    ?>


    </select>

    </form>



    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

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    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
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi, thanks but still i dont know how to do it

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Alright, have you made any changes as suggested? If so, post your new code.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •