Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Member bwedekin's Avatar
    Join Date
    Feb 2004
    Location
    The Midwest
    Posts
    33
    Member #
    4819
    I am trying to create a form that will allow a user to add new categories to a dB.

    A simple form was created where the add a name and submit.
    Code:
    <form action="'.$PHP_SELF.'?page=add">
    		<td class="fsmall">New Category Name: </td><td class="fsmall"><input type="text" name="cat_name" size="50"></td>
    		</tr>
    		<tr class="txtbold">
    		<td class="fsmall" colspan="2" align="center"><input type="submit" name="submit" value="Add Category Name"></td>
    		</form>
    Here is the code that I made to process the form, however it doesn't work.
    PHP Code:
    if ($_GET["page"] == "add") {

        if (
    $submit) {
        
        
    $category mysql_escape_string($_POST['cat_name']);
        
        
    $sql mysql_query("SELECT * FROM chamber_category WHERE category = '$category' ");
        
    $result mysql_query($sql)
            or die(
    "Invalid query: " mysql_error());
            
        
    $num_rows mysql_num_rows($result);

        
    //check to see if category exsists in dB
        
    if($num_rows == 0){
        
            
    $sql "INSERT INTO chamber_category (category) ";
            
    $sql .= "VALUES ('$category') " ;
            
    mysql_query($sql$connection);
            
    $result mysql_query($sql)
            or die(
    "Invalid query: " mysql_error());
            
            
    $message "$category sucessfully added to the database.";
            exit;
        }
        else {
            
    $message "$category already exsists in the database.";
            exit;
        }
        echo 
    $message;
        } 
    The entire code is attached as well.

  2.  

  3. #2
    Senior Member jlgosse's Avatar
    Join Date
    Jan 2004
    Location
    Newfoundland, Canada
    Posts
    1,037
    Member #
    4570
    Liked
    7 times
    I won't go into much else besides this:
    Code:
    <form action="'.$PHP_SELF.'?page=add">
    This syntax for this is wrong. First off, you can't have PHP in HTML like that unless you are echoing it out using PHP. Second of all, $PHP_SELF is actually $_SERVER['PHP_SELF'].

    The correct syntax would be something like this:
    PHP Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>?page=add">


  4. #3
    Member bwedekin's Avatar
    Join Date
    Feb 2004
    Location
    The Midwest
    Posts
    33
    Member #
    4819
    Originally posted by newfiewebguru
    I won't go into much else besides this:


    Code:
    <form action="'.$PHP_SELF.'?page=add">
    That is inside a print()


    Originally posted by newfiewebguru
    This syntax for this is wrong. First off, you can't have PHP in HTML like that unless you are echoing it out using PHP. Second of all, $PHP_SELF is actually $_SERVER['PHP_SELF'].

    The correct syntax would be something like this:
    PHP Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>?page=add">
    Actually the $PHP_SELF works just the same.

  5. #4
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    What specifically happens? Are you getting any mysql errors, or is it just the update query that never gets executed.
    In your attached form you're using a page reference in the forms action field instead of $PHP_SELF. Did it work then?

    Why do you need the variable "page==add/show"? If they're really neccessary, why not add a hidden field to your form where you set "page=add" and skip the variable from the action field of your form.
    S. Rosland

  6. #5
    Senior Member jlgosse's Avatar
    Join Date
    Jan 2004
    Location
    Newfoundland, Canada
    Posts
    1,037
    Member #
    4570
    Liked
    7 times
    PHP_SELF is a $_SERVER[''] "global" isn't it?


  7. #6
    Member bwedekin's Avatar
    Join Date
    Feb 2004
    Location
    The Midwest
    Posts
    33
    Member #
    4819
    Originally posted by rosland
    What specifically happens? Are you getting any mysql errors, or is it just the update query that never gets executed.
    In your attached form you're using a page reference in the forms action field instead of $PHP_SELF. Did it work then?

    Why do you need the variable "page==add/show"? If they're really neccessary, why not add a hidden field to your form where you set "page=add" and skip the variable from the action field of your form.
    Nothing happens.

    There are no SQL errors that appear, and I recieve a blank page.

    I think i found something that I am working now. I forgot to add method="Post" in my <form>.

  8. #7
    Senior Member teal's Avatar
    Join Date
    Dec 2003
    Posts
    247
    Member #
    4319
    PHP Code:
    if ($_GET["page"] == "add") {

        if (
    $submit) {
        
        
    $category mysql_escape_string($_POST['cat_name']);
        
        
    $sql mysql_query("SELECT * FROM chamber_category WHERE category = '$category' ");
        
    $result mysql_query($sql)
            or die(
    "Invalid query: " mysql_error());
            
        
    $num_rows mysql_num_rows($result);

        
    //check to see if category exsists in dB
        
    if($num_rows == 0){
        
            
    $sql "INSERT INTO chamber_category (category) ";
            
    $sql .= "VALUES ('$category') " ;
            
    mysql_query($sql$connection);
            
    $result mysql_query($sql)
            or die(
    "Invalid query: " mysql_error());
            
            
    $message "$category sucessfully added to the database.";
            exit;
        }
        else {
            
    $message "$category already exsists in the database.";
            exit;
        }
        echo 
    $message;
        } 
    Correct me if I am wrong, but shouldn't that middle if be an elseif, and shouldn't you close the if above it?

    Also, I don't think a method is important

    ** nevermind, I see what you are doing. It was nested, you just didn't put in a lot of "else" statements. It is confusing.
    Whoth steps forth to mock the teal? Whom couldst say a color superior!

    Proclamith me, it cannot be done.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 03:02 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com