SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    retrieving problem

    part of form.php
    HTML Code:
     <form action="add.php" method="post">
      <table bgcolor="#EFEFEF" border="1" align="center" cellpadding="1" cellspacing="1">
        <tr> 
          <td height="23" colspan="2" bgcolor="#DDEEFF"><div align="center">Category</div></td>
        </tr>
        <tr> 
          <td height="62"><div align="center"><strong><font color="#800040" face="Verdana">Name 
              of the category::</font></strong></div></td>
          <td><strong><font color="#000000" face="Verdana"> 
            <input name="category[]" type="text" id="category" value="">
            </font></strong> <p> <span id="catAttachments"></span><strong> <a href="javascript:catAttachment(); void(0);"><font color="#000000" face="Verdana">(Add 
              More Categories)</font></a><br />
              <font size="-1">Maximum more allowed : 4</font> </strong></td>
        </tr>
        <tr> 
          <td height="41" colspan="2"> 
            <div align="center"> <strong> 
              <input type="submit" name="button" value="Add Category">
              </strong></div></td>
        </tr>
    add.php
    PHP Code:
    <?php if($_REQUEST['button']=="Add Category"){
        if(!empty(
    $_REQUEST['category'])){
        foreach(
    $_REQUEST['category'] as $category){
         
    $sql=mysql_query("INSERT INTO category(`category`) VALUE('".addslashes($category)."')");
        if(
    $sql)
         echo 
    "Category ".$category." created";
         echo 
    "<br/>";}?>
         <a href="index.php">Back</a><?php
          
    exit();
          }
         else
          { 
         echo 
    "You need to enter the name of the category";?>
         <a href="index.php">Back</a><?php 
         
    exit();
         }
     }
    the problem is in add.php the else part is not being executed
    even if i do not enter anything in the form
    it creates a category with name ""

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since you are posting the form then why not POST instead of REQUEST array?
    PHP Code:
    if($_POST['button']=="Add Category"){
        if(count($_POST['category']) >= 1){
            foreach($_POST['category'] as $category){
                $sql = mysql_query("INSERT INTO category(`category`) VALUE('".addslashes($category)."')");
                if($sql)
                    echo "Category ".$category." created";
                    echo "<br/>";
                }
                ?>
                <a href="index.php">Back</a><?php
                
    exit();
            }
        else{ 
            echo 
    "You need to enter the name of the category";?>
            <a href="index.php">Back</a><?php 
            
    exit();
        }
    }
    And try with count($_POST['category']) as i have done above.
    Last edited by Raju Gautam; Jul 13, 2009 at 00:38.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well its causing the same problem
    i echoed count($_POST['category'])
    its giving one even when i don enter a name

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh yes it always shows the number of posted in case of text boxes... i was confused. i thought you are using checkboxes there.
    Try something like this then:
    PHP Code:
    if($_POST['button']=="Add Category"){
        
    $done         0;
        
    $blank         0;
        foreach(
    $_POST['category'] as $category){
            if(!empty(
    $category)){
                
    $sql mysql_query("INSERT INTO category(`category`) VALUE('" mysql_real_escape_string($category) . "')");
                
    $done++;
            }
            else
                
    $blank++;
        }
        if(
    $done == 0
            echo 
    "You need to enter the name of the category";
        else{
            echo 
    $done " categories created.";
            if(
    $blank 0)
                echo 
    $blank " categories could not be created.";
        }
        echo 
    '<a href="index.php">Back</a>';
        exit();

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    Oh yes it always shows the number of posted in case of text boxes... i was confused. i thought you are using checkboxes there.
    Try something like this then:
    PHP Code:
    if($_POST['button']=="Add Category"){
        
    $done         0;
        
    $blank         0;
        foreach(
    $_POST['category'] as $category){
            if(!empty(
    $category)){
                
    $sql mysql_query("INSERT INTO category(`category`) VALUE('" mysql_real_escape_string($category) . "')");
                
    $done++;
            }
            else
                
    $blank++;
        }
        if(
    $done == 0
            echo 
    "You need to enter the name of the category";
        else{
            echo 
    $done " categories created.";
            if(
    $blank 0)
                echo 
    $blank " categories could not be created.";
        }
        echo 
    '<a href="index.php">Back</a>';
        exit();

    Thanks it worked


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
  •