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 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2009
    Location
    Portugal/Porto
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Problem with lookup table...

    Hello again...

    Maybe someone can help me with this:

    TABLES:

    categorias: id_cat - cat_name

    catalogo: id - id_cat - id_img - name - desc - status

    In a form I submit the content for insert in catalogo. In one input, I lookup for categorias (cat_name) but only for displaying ther name, because what i need to insert in catalogo field is the id_cat. I've the following code:
    --------------------------------------------------------------------

    $id = $_POST['id'];
    $catg = $_POST['catg']; //it passes the categorias cat_name, but i need the id_cat

    $catalogname = $_POST['catalogname'];
    $catalogdesc = $_POST['catalogdesc'];
    $stat = $_POST['stat'];

    if ($catalogname == ""){
    echo "<h3>Empty Fields...</h3>";
    }else{
    mysql_query("INSERT INTO `catalogo` VALUES ('','$catg','','$catalogname','$catalogdesc','$stat')");

  • #2
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    Are you trying to get the id for the category name so you can insert it into the record? If so you may want to user a select field and set its value to the id for each corresponding field name.

    PHP Code:
    $q 'SELECT id_cat, cat_name FROM categorias';
    $r mysql_query ($q);
    if (
    mysql_num_rows ($r) > 0) {
      echo 
    '<select name="catg">';
      while (
    $row=mysql_fetch_assoc ($r) {
        echo 
    "<option value=\"{$row['id_cat']}\">{$row['cat_name']}</option>";
      }
      echo 
    '</select>';

    Now you will have a drop down with the ids as the value for each name. When you select cat_name from the menu you will receive the id value for the $_POST['catg'] and not the name.

  • Users who have thanked Coyote6 for this post:

    macpeace (09-12-2009)

  • #3
    New Coder
    Join Date
    Sep 2009
    Location
    Portugal/Porto
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thats it Coyote6...´but i receive this error with the code that you post it:

    Parse error: parse error, unexpected '{' in C:\Programas\EasyPHP 2.0b1\www\administracao\catalogAdd.php on line 41

    line 41 is: while ($row=mysql_fetch_assoc ($r){

  • #4
    New Coder
    Join Date
    Sep 2009
    Location
    Portugal/Porto
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks...you give me the light...I turn on the switcher!!....:=)

    Code:
    <? 
    echo "<select name=\"catg\" id=\"catg\">";
    $rSQL = mysql_query("SELECT * FROM categorias");
    while($asData = mysql_fetch_array($rSQL))
    {
    echo "<option value=\"" . $asData['0'] . "\">" . $asData['1'] . "</option>";
    }
    echo "</select>"; 
    				 
    mysql_close($con); 
    ?>

  • #5
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    Sorry bout that I missed a ) on the end of that while statement.


  •  

    Posting Permissions

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