SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a very strange problem...
    I have written the following statement:
    PHP Code:
        $check  "SELECT * FROM pages WHERE subcat = '$subcatid'";
        
    $res    mysql_query($check);
        
    $num     mysql_num_rows($res);
    // i've also tried it with mysql_numrows... has the same effect 
    table pages(id, subcat,txt,rubrieken)
    'subcat' foreign key to subcat.id
    'rubrieken' foreign key to rubrieken.id

    Even if the pages.rubrieken is empty is returns a $num = 1.
    WHY??? I want to check if the pages.rubrieken is empty... if so, I want to a new rubrieken.id... if Not, I want to read out the value of pages.rubrieken, add the new rubrieken.id to it
    PHP Code:
    $rubids $rubriek ',' $rubid
    $rubriek is the old pages.rubrieken value
    $rubid is the new value to be added
    $rubids is the value that is going to be update in the pages table.

    But if $num returns one, while the pages.rubrieken is empty it will update ',23' (for instance) which is a good way to screw up your database.

    Can someone help me?
    I want a statement to check if rubrieken is EMPTY... so $num will return 0... or FILLED so $num will return 1 or more.

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jazztie, you have muddled your logic a little. You would expect $num to be 1 if there is a matching record for that subcat in table pages. $num would be zero if there are no records in pages with that subcat.

    The code below checks whether the rubrieken field is an empty string or not. Remember that an empty string evaluates to false.
    PHP Code:
    $sql "SELECT***FROM*pages*WHERE*subcat*=*'$subcatid'";
    $result mysql_query($sql);
    $row mysql_fetch_arryay($result);
    // extract each element of array $row into its own variable
    extract($row);

    if ( 
    $rubrieken ) {
       
    // rubrieken has an existing value
    } else {
       
    // rubrieken is empty


  3. #3
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suspected it was something like that...
    Well, at least I've learned one thing... don't take on an internship for graduation when you don't know the required programming-language

    Oh well...

    Thanks Freakysid!

  4. #4
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Argh...

    I have a new problem... using the following code, a new page is added to table pages, with the id of the new rubrieken.id.
    For instance:
    table pages; id(34), subcatid(7), txt(), rubrieken(34)

    I will post too pieces of code... one from add.php, which allows the user to add a new rubriek to a page, or to add an exsisting rubriek to a page.
    add1.php checks if it is a new rubriek, or an old one...

    add.php:
    PHP Code:
    elseif (isset ($subcatid)){
        
    $subcatid $subcatid;
        print     
    "<html><head><title>Adding a rubriek</title></head>";
        print     
    "<form name='toevoegenform' method='POST' action='add1.php'>";
        print     
    "Title of new rubriek: <b>";
        print     
    "<input type='text' name='rubtitel' size='40'><br>";
        print     
    "<select name='oudrubid'><option value=''>Select old rubriek...</option>";

        
    $sql     "SELECT * FROM rubrieken ORDER BY titel";
        
    $result mysql_query($sql);
        while (
    $row mysql_fetch_array($result)) {
                 
    // function extract will take the key/value pairs in 
                 // array $row and place them in their own variables
                 // being $titel and $id
               
    extract($row);
               
    $oudrubid $id;
               print 
    "<option value='$id'>$titel</option>";
        }
        print     
    "</select>";
        print     
    "<input type='hidden' name='subcatid' size='1' value='$subcatid'>";
        print     
    "<body onload=\"document.toevoegenform.rubtitel.focus();\">";


    add1.php
    PHP Code:
    elseif (isset ($subcatid)){
        
    $sql "SELECT * FROM pages WHERE subcat = '$subcatid'";
        
    $result mysql_query($sql);
        
    $row mysql_fetch_array($result);
        
    /* /// extract each element of array $row into its own variable /// */
        
    extract($row);
            
        if (
    $rubtitel){
           if (
    $rubrieken){ // rubrieken has an existing value
            
    $pageid mysql_result($result,0,'id');
            
    $sql    "INSERT INTO rubrieken(id,titel) VALUES('','$rubtitel')";
            
    $result mysql_query($sql);
            
            
    $sql1   "SELECT * FROM rubrieken WHERE titel = '$rubtitel'";
            
    $result1mysql_query($sql1);
            
    $rubid  mysql_result($result1,0,'id');
            
            
    $sql2   "SELECT rubrieken FROM pages WHERE id = '$pageid'";
            
    $result2mysql_query($sql2);
            
    $rubriekmysql_result($result2,0,'rubrieken');
            
            
    $rubids $rubriek ',' $rubid;
                    
            
    $sql3   "UPDATE pages SET rubrieken='$rubids' WHERE subcat =$subcatid";
            
    $result3mysql_query($sql3);
            print   
    " Rubrieken $rubids has been added to Page $pageid, along with rubrieken $rubriek";
           }
           else {  
    // rubrieken is empty
            
    $sql    "INSERT INTO rubrieken(id,titel) VALUES('','$rubtitel')";
            
    $result mysql_query($sql);
            
    $subcatid $subcatid;
        
            
    $sql2   "SELECT * FROM rubrieken WHERE titel = '$rubtitel'";
            
    $result2mysql_query($sql2);
            
    $rubid  mysql_result($result2,0,'id');
        
            
    $sql3   "INSERT INTO pages(id,subcat,txt,rubrieken,datum) VALUES('','$subcatid','','$rubid','')";
            
    $result3mysql_query($sql3); 
            print 
    "Rubriek <b>$rubtitel$subcatid, rubid: $rubid</b> has been correctly submitted";
           }
        }
        if (
    $oudrubid){
            
    $sql1 "SELECT * FROM pages WHERE subcat = '$subcatid'";
            
    $result1 mysql_query($sql1);
            
    $row1 mysql_fetch_array($result1);
            
    /* /// extract each element of array $row into its own variable /// */
            
    extract($row1);
          
          if (
    $rubrieken){
            print 
    "Num: $num1 subcatid = $subcatid res2 = $res2";
            
            
    $pageid mysql_result($res1,0,'id');
            
            
    $sql2   "SELECT * FROM pages WHERE id = '$pageid'";
            
    $result2mysql_query($sql2);
            
    $rubriekmysql_result($result2,0,'rubrieken');
            
            
    $rubids $rubriek ',' $oudrubid;
                    
            
    $sql3   "UPDATE pages SET rubrieken='$rubids' WHERE subcat =$subcatid";
            
    $result3mysql_query($sql3);
            print   
    " Rubrieken $rubids has been added to Page $pageid, along with rubrieken $rubriek"
            
          }
          else { 
    // rubrieken is empty     
            
    $subcatid $subcatid;
            print 
    "Num: $num1 subcatid = $subcatid";
              
                  
            
    $sql3   "INSERT INTO pages(id,subcat,txt,rubrieken,datum) VALUES('','$subcatid','','$oudrubid','')";
            
    $result3mysql_query($sql3); 
            print 
    "Rubriek <b>$rubtitel$subcatid, rubid: $rubid</b> has been correctly submitted"
            
          }
        }

    Could someone please check the code for me, and tell me where I made a mistake? I'm lost...

  5. #5
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've posted a new thread (id 24312) with the above question, as it is a new question, really...


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
  •