Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2003
    Location
    texas
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Adding to a database

    I was wondering where I might have gone wrong on adding information to a database, the country_id is an autonumber, so I don't have to add it. But when a user puts in a name of the country, it adds it to the database, once they click the submit button, and it returns them back to this page. It passes the variable through the url I think but I am not for sure about that one either? But any suggestions are helpful...

    PHP Code:
    <?php
    $db 
    mysql_connect("localhost""tableName""PW")or die(mysql_error());

    mysql_select_db("database")or die(mysql_error());

    $obj mysql_query("SELECT * FROM country");
    $obj=mysql_fetch_array($obj);

    INSERT INTO COUNTRY (country_idcountry_nameVALUES($country?>

    }

    echo '</select>';
    }
    ?>  
     <html>
    <head
    </head>
    <title><? echo $obj[1?></title>

    <FORM ACTION="index.php" METHOD=POST>
    Please enter the name of the country to put into the database.
    <p>TITLE:<INPUT TYPE="text" NAME="country" SIZE=25 MAXLENGTH=25>
    <p><INPUT TYPE="submit">
    </FORM>

    <?php echo $country ?>
    <body>
    </body>
    </html>
    Last edited by Spookster; 06-03-2004 at 12:25 AM. Reason: In the future please surround your code with [php] [/php] tags
    thanks
    bri

  • #2
    New Coder
    Join Date
    Jun 2004
    Location
    Newcastle, England
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by briintex1

    INSERT INTO COUNTRY (country_id, country_name) VALUES($country) ?>

    }

    I might just be being dumb here, but any particular reason why this line isn't in a mysql_query() function?

  • #3
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    $db = mysql_connect("localhost", "tableName", "PW")or die(mysql_error());
    try this......

    PHP Code:

    <?php
    $db 
    mysql_connect("localhost""ServerUserName""ServerPassWord");
    mysql_select_db("DatabaseName",$db);

    $select mysql_query("SELECT * FROM `country`");
    $obj=mysql_fetch_array($select);

    mysql_query("INSERT INTO `country` (`country_id`, `country_name`) VALUES ('','$country')") or die(mysql_error()); 

    }

    echo 
    '</select>';
    }
    ?> 
    <html>
    <head>
    </head>
    <title><? echo $obj[1]; ?></title>

    <FORM ACTION="index.php" METHOD=POST>
    Please enter the name of the country to put into the database.
    <p>TITLE:<INPUT TYPE="text" NAME="country" SIZE=25 MAXLENGTH=25>
    <p><INPUT TYPE="submit">
    </FORM>

    <?php echo $country?>
    <body>
    </body>
    </html>
    One question, tho..... where did u set $country ? you should have somewhere above the INSERT thing a line like $country = $_POST['country'];
    Last edited by boeing747fp; 06-03-2004 at 02:47 AM. Reason: In the future please surround your code with [php] [/php] tags


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •