SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Evangelist cturner01's Avatar
    Join Date
    Jun 2006
    Location
    My computer
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding either variable to the database???

    I am wanting to add a menu name, only if the user has entered a name into the menuname text box. Or I need to add a menu name if the user has selected something from a combo box. Can someone please tell me a better way to do this? I am getting nowhere with this.

    When I tested my code that is below I got this error: Could not add the entry because: Query was empty. The query was INSERT INTO `menu` (`menuname`, `page_title`)
    VALUES ('alone', 'Home').

    Code PHP:
    // insert data into the menu table - $menuname or $selectMenuname
    			// if $menuname is not empty
    			if (!empty($menuname)) {
    				// then insert that menu name into the database
    				$insert1 = "INSERT INTO `menu` (`menuname`, `page_title`)
    						   VALUES ('$menuname', '$pagetitle')";
    				if (mysql_query ($insert1)) {
    					// select the last inserted menuid
    					$query1 = mysql_query("SELECT menuid FROM `menu`") or die ("Could not query because: ".mysql_error());
    					$row1 = mysql_fetch_assoc($query1);
    					$menuid = mysql_insert_id();
    					// then insert the menuid into the pages table
    					// along with the page's content
    					$insert3 = "INSERT INTO `pages` (`menuid`, `button`, `page_title`, `content`, `keywords`, `description`, `active`, 	`images`, `imagenames`)
    					VALUES ('$menuid', '$button', '$pagetitle', '$pageContent', '$keywords', '$description', '$active', '$images', '$imagenames')";
    				// if everything is ok
    				if (mysql_query ($insert3)) {
    					// go to the next page
    					header ("Location: page_added.php?pagetitle=".$pagetitle);
    				} else {
    					print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $insert3.</p>";
    				}
    				} else {
    					print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $insert1.</p>";
    				}						
     
    			// if $selectMenuname is not equal to none
    			} elseif ($selectMenuname != 'none') {
    				// then insert that menu name into the database
    				$insert2 = "INSERT INTO `menu` (`menuname`, `page_title`)
    						   VALUES ('$selectMenuname', '$pagetitle')";
    				if (mysql_query ($insert1)) {
    					// select the last inserted menuid
    					$query1 = mysql_query("SELECT menuid FROM `menu`") or die ("Could not query because: ".mysql_error());
    					$row1 = mysql_fetch_assoc($query1);
    					$menuid = mysql_insert_id();
    					// then insert the menuid into the pages table
    					// along with the page's content
    					$insert3 = "INSERT INTO `pages` (`menuid`, `button`, `page_title`, `content`, `keywords`, `description`, `active`, 	`images`, `imagenames`)
    					VALUES ('$menuid', '$button2', '$pagetitle', '$pageContent', '$keywords', '$description', '$active', '$images', '$imagenames')";
    				// if everything is ok
    				if (mysql_query ($insert3)) {
    					// go to the next page
    					header ("Location: page_added.php?pagetitle=".$pagetitle);
    				} else {
    					print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $insert3.</p>";
    				}
    				} else {
    					print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $insert2.</p>";
    				}						
    			}

    Thanks in advance.

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Look at this:
    Code php:
    $insert2 = "INSERT INTO `menu` (`menuname`, `page_title`)
                               VALUES ('$selectMenuname', '$pagetitle')";
                    if (mysql_query ($insert1)) {
    Look close at $insert2, then what's in mysql_query().

    And I'd really use one hammer:
    Code php:
    $menu='';
    if (!empty($menuname)) {
          $menu=$menuname;
    }elseif ($selectMenuname != 'none') {
         $menu=$selectMenuname;
    }
     
    if(!empty($menu)){
        // then insert that menu name into the database
        $insert1 = "INSERT INTO `menu` (`menuname`, `page_title`)
                   VALUES ('$menu', '$pagetitle')";
     
        if (mysql_query ($insert1)) {
            // select the last inserted menuid
            $menuid = mysql_insert_id();
            // then insert the menuid into the pages table
            // along with the page's content
            $insert3 = "INSERT INTO `pages` (`menuid`, `button`, `page_title`, `content`, `keywords`, `description`, `active`,  `images`, `imagenames`)
            VALUES ('$menuid', '$button', '$pagetitle', '$pageContent', '$keywords', '$description', '$active', '$images', '$imagenames')";
    	    // if everything is ok
    	    if (mysql_query ($insert3)) {
    	        // go to the next page
    	        header ("Location: page_added.php?pagetitle=".$pagetitle);
    	    } else {
    	        print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $insert3.</p>";
    	    }
        } else {
            print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $insert1.</p>";
        }                 
    }

    I also removed some stuff above mysql_insert_id(), whatever that was.

    Oh, and I really hope all those variables that go to the query are validated.
    Saul


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
  •