SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    retrieve data from dBase

    I believe this is a child's play for php/mysql programmers :
    I need to print out a "category" list in a html select tag from a database. I use this script for that:
    PHP Code:
        $query = "SELECT id, parentCat FROM category ORDER BY id";
        $result  = mysql_query($query) or die('Error : ' . mysql_error()); 
        $row     = mysql_fetch_row($result, MYSQL_ASSOC); 
        $Pcategory = $row['category'];

    // here's the form
              <select name="category" class="box" id="category">
          <option value="">--- Choose category ---
          <option value="<? print ($Pcategory); ?>"><? print ($Pcategory); ?>
          </select>
    It doesn't work what I want (in fact it doesn't do anything....)
    Your help is appreciated.

    Full time ADMIN - art community
    Part time coder - dsign

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here you go:
    PHP Code:
    <?
    $query 
    "SELECT id, parentCat FROM category ORDER BY id";
    $result  mysql_query($query) or die('Error : ' mysql_error());
    ?>
    //form:
    <select name="category" class="box" id="category">
    <option value="">--- Choose category ---</option>
    <?
    while($row mysql_fetch_array($result)){
        
    print_r("<option value=\"%s\">%s</option>"$row['parentCat'], $row['parentCat']);
    }
    ?>
    </select>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    Warning : Supplied argument is not a valid MySQL result resource in PATH_TO_FILE.php on line 79
    where line 79 =
    PHP Code:
          <? while($row mysql_fetch_array($result)) {
          
    print_r("<option value=\"%s\">%s</option>"$row['parentCat'], $row['parentCat']);
          } 
    ?>
    R the data types in DBase concerned with that?

    Full time ADMIN - art community
    Part time coder - dsign

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    no, it's because for some reason $result is not pointing to a query...

    try grouping it all together:
    PHP Code:
    //form:
    <select name="category" class="box" id="category">
    <option value="">--- Choose category ---</option>
    <?
    $query 
    "SELECT id, parentCat FROM category ORDER BY id";
    $result  mysql_query($query) or die('Error : ' mysql_error());
    while(
    $row mysql_fetch_array($result)){
        
    print_r("<option value=\"%s\">%s</option>"$row['parentCat'], $row['parentCat']);
    }
    ?>
    </select>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the Warning message is not visible on the page but when I look at the page source it's still there. Before the query for retrieving inf. from the DBase there's a form for input fields into another table in the mentioned DBase. Can be the source of my problem?
    PHP Code:
             $query "INSERT INTO news (title, content, category, picname, picsize, picdate) VALUES ('$title', '$content', '$category', '$file_realname', '$size', '$date')";
             
    mysql_query($query) or die('Error ,query failed');

        
    $query1 "SELECT id, parentCat FROM category ORDER BY id";
        
    $result  mysql_query($query1) or die('Error : ' mysql_error()); 

    Full time ADMIN - art community
    Part time coder - dsign

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Can I see the rest of the file?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here you go:
    PHP Code:
    <?php
    if(isset($_POST['save']))
        {
         
    $title $_POST['title'];
         
    $content $_POST['content'];

         if(!
    get_magic_quotes_gpc())
            {
             
    $title addslashes($title);
             
    $content addslashes($content);
             }
        
    // image upload
        
    $date date('l dS F, Y');
        
    // $_FILES['userfile']['name'] is the name of the pic sent. This name needs to go into the database so then the database can call it.
        
    if (is_uploaded_file($_FILES['userfile']['tmp_name']))
            {
            
    $file_realname trim($_FILES['userfile']['name']);
            
    $uploaddir "uploaded/";

            if (
    move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir $file_realname)){
            
                }
                else{
                print 
    "<strong>$file_realname</strong> did not upload!";
                }
            
    // this is so when it displays the file size to the user it is in KB
            
    $size round(($_FILES['userfile']['size'] / 1024), 1);
            
    // image upload end
             
    include 'library/config.php';
             include 
    'library/opendb.php';

             
    $query "INSERT INTO news (title, content, category, picname, picsize, picdate) VALUES ('$title', '$content', '$category', '$file_realname', '$size', '$date')";
             
    mysql_query($query) or die('Error ,query failed');

        
    $query1 "SELECT id, parentCat FROM category ORDER BY id";
        
    $result  mysql_query($query1) or die('Error : ' mysql_error()); 

             include 
    'library/closedb.php';
             echo 
    "Artikal '$title' dodat u bazu";
             }
         }
    ?> 
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data"> 
      <table width="700" border="0" cellpadding="2" cellspacing="1" class="box" align="center"> 
        <tr> 
          <td width="100">Title</td> 
          <td><input name="title" type="text" class="box" id="title"></td> 
        </tr> 
        <tr> 
          <td width="100">Category</td> 
          <td><select name="category" class="box" id="category">
          <option value="">--- Izaberi postoje&#263u ---</option>
          <?
          
    while($row mysql_fetch_array($result)) {
          
    print_r("<option value=\"%s\">%s</option>"$row['parentCat'], $row['parentCat']);
          } 
    ?>
          </select>
        </td> 
        </tr> 
        <tr> 
          <td width="100">Content</td> 
          <td><textarea name="content" cols="60" rows="10" class="box" id="content"></textarea></td> 
        </tr> 
        <tr> 
          <td width="100">Upload Image</td> 
          <td> <input type="hidden" name="MAX_FILE_SIZE" value="5120000"> 
       <input name="userfile" type="file" size="40" class="box"></td> 
        </tr> 
        <tr> 
          <td width="100">&nbsp;</td> 
          <td>&nbsp;</td> 
        </tr> 
        <tr> 
          <td colspan="2" align="center">
          <input name="save" type="submit" class="box" value="Save Article">
          </td> 
        </tr> 
      </table> 
    </form>

    Full time ADMIN - art community
    Part time coder - dsign

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    if you go into phpmyadmin, and put
    Code:
    SELECT id, parentCat FROM category ORDER BY id
    in the "insert SQL" box.

    What's returned?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    $query1 = "SELECT id, parentCat FROM category ORDER BY id";

    $result = mysql_query($query1) or die('Error : ' . mysql_error());

    Needs to be outside of your $_POST['save'] if statement.
    print_r("<option value=\"&#37;s\">%s</option>", $row['parentCat'], $row['parentCat']);

    should be printf()

  10. #10
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    id   parentCat
    -------------------
    1    Informatika
    -------------------
    2    Elektronika
    -------------------
    I've inserted these data manually into DBase, not through a php script, just to see if the script above will list them in the <select> tag.

    Full time ADMIN - art community
    Part time coder - dsign

  11. #11
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    print_r() should be printf()
    Oops - Good eye.

    I still don't get this error that's occuring though...
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  12. #12
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you people!!!!!!!!! You helped me a lot!

    P.S.: arkinstall : the error is "gone"

    Thanks once more.

    Full time ADMIN - art community
    Part time coder - dsign

  13. #13
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Oops - Good eye.

    I still don't get this error that's occuring though...
    It's because the resource being created is inside the if statement, so there is no resource that php can create the drop down from unless the form is submitted.


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
  •