SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Drop downs and php

    I'm trying to use a form and drop downs to send data to a database table named "computers"
    Here's where I'm at, the errors say
    Undefined index: name
    Undefined index: category
    Undefined index: category found here:
    $name = $_POST['name'] ;
    $category = $_POST['category'] ;
    if ($_POST['category']

    Isn't it reading these fields in the form? Can someone see why the php isn't reading these named form fields? Am I supposed to have some php in the form itelf? Thanks very, very, much.

    PHP Code:
    PHP Code:
    <?php 
    ini_set
    ('error_reporting'8191); 
    ini_set('display_startup_errors'1); 
    ini_set('display_errors'1);{ 
    ?> 
    </span> 
    <form action="<?php $_SERVER['PHP_SELF'] ;?>" method="post"> 
    <table> 
    <tbody> 
    <tr> 
    <td><input name="name" size="25"/></td> 
    </tr> 
    <tr> 
    <td> 
    <select name="category"> 
    <option value="first">first</option> 
    <option value="second">second</option> 
    <option value="third">third</option> 
    </select> 
    </td> 
    </tr> 
    <tr> 
    <td><input value="Add" type="submit" /></td> 
    </tr> 
    </tbody> 
    </table> 
    </form> 
    <?php 

    $con mysql_connect("","",""); 
    if (!
    $con
    { die(
    'Could not connect: ' mysql_error()); 

    mysql_select_db(""$con); 
    $name $_POST['name'] ; 
    $category $_POST['category'] ; 
    if (
    $_POST['category'] == 'first'){ 
    $sql="INSERT INTO computers (name,category,date) 
    VALUES ('" 
    $_POST['name'] . "','" $_POST['category'] . "',CURDATE())"
    if (
    mysql_query($sql,$con)) 
    {echo 
    "row added";} 
    else if (!
    mysql_query($sql,$con)) {mysql_error();}} 
    ?>

  2. #2
    SitePoint Enthusiast CorkyMcDoogle's Avatar
    Join Date
    Aug 2006
    Location
    Chesapeake, Maryland
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The errors mean that the variables have no value.

    You need to add some type of statement to see if the form has been submitted first... if it has, then assign the values.

    Also, you have to give the host, username, and password to connect to the DB. You've left all 3 blank -- $con = mysql_connect("","","");

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Location
    Perth, Australia
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    surround the php code at the bottom with:

    PHP Code:
    if($_SERVER['REQUEST_METHOD'] == 'POST'){

        
    //do database stuff here




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
  •