SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Aug 2001
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help Please (Select Problems)

    Hello,

    I've been looking at Sitepoint for a few days now. Very nice site. Lots of good information. Keep up the good work!!!

    I am trying my hand at PHP/mySQL for the first time. I followed Kevin Yank's tutorial and learned a great deal. I've got everything working but one item. I have a form on my site with a <SELECT> populated by PHP from a mySQL database. For some reason, the VALUE always ends up being the last record in the array, not the value that is selected. As far as I can tell I coded everything correctly. Can someone please look at my code below and tell me what I'm doing wrong. I greatly appreciate it. Thank you.

    <?php

    $connect = @mysql_connect("localhost", "username", "password");

    if (! $connect) {
    echo( "<p>Unable to connect</p>");
    exit();
    }

    mysql_select_db("mydatabase");

    $artists= mysql_query("SELECT ID_Artist, Name from Artist");

    ?>


    <h2 align="center">CD Entry Screen</h2>

    <p>Enter the title of the CD here and press SUBMIT to continue.</p>

    <FORM ACTION="cd_entry_screen.php" METHOD=POST>

    <P>Artist<br>
    <select size="1" name="artistdd" tabindex="1">
    <OPTION SELECTED VALUE ="">Select One
    <OPTION VALUE="">----------
    <?php
    while ($artist = mysql_fetch_array($artists)) {
    $artistdd = $artist[ID_Artist];
    $aname = $artist[Name];
    echo( "<OPTION VALUE='$artistdd'>$aname\n");
    }
    ?>
    </select></P>

    <P>CD TitleBR>
    <INPUT NAME="Title" size="35" tabindex="2">

    <P>Yearbr>
    <input type="text" name="year" size="5" tabindex="3">

    <P>Genrebr>
    <input type="text" name="genre" size="10" tabindex="4">

    <P>Orginalbr>
    <select name="original" size="1" tabindex="5">
    <OPTION SELECTED VALUE="N">N
    <OPTION VALUE="Y">Y
    </select>
    <P>

    <INPUT TYPE=submit NAME="submitcd" VALUE="SUBMIT" tabindex="6">
    </select></FORM>

    <?php

    if ("SUBMIT" == $submitcd) {

    $sql = "INSERT INTO CD_Info SET " .
    "Artist_ID*='$artistdd', Title='$Title', Year='$year', Genre='$genre',original='$original', Date_Added=CURDATE()";

    if (mysql_query($sql)) {
    echo( "<P>CD has been added.</P>");
    } else {
    echo( "<P>Error adding CD: " .
    mysql_error() . "</P>");
    }
    }


    ?>

    BTW - This is basically a CD Collection database. I am trying to get the correct artist in with the correct CD title by inserting the Artist_ID in the CD_Info table.
    signature

  2. #2
    SitePoint Member
    Join Date
    Aug 2001
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem Solved

    DUH! I figured it out. I made the variable $artistdd = artist.id_artist. $artistdd is also the name of my control on my form. Of course this will make it not work. I change the variable to $artistid and it works. Thanks to anyone who looked at this. The correct and working version is copied below.

    <select size="1" name="artistdd" tabindex="1">
    <OPTION SELECTED VALUE ="">Select One
    <OPTION VALUE="">----------
    <?php
    while ($artist = mysql_fetch_array($artists)) {
    $artistid = $artist[ID_Artist];
    $aname = $artist[Name];
    echo( "<OPTION VALUE='$artistid'>$aname\n");
    }
    signature


Bookmarks

Posting Permissions

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