Jump to content

insert into MySQL from form list-box


marcs910

Recommended Posts

Ive searched and found a couple of ways this can be done but they arent matching up with what Ive used.
Im currently reading "PHP and MySQL For Dynamic Web Sites" by Larry Ullman and this is where I am learning my code.

What I'm trying to do is create a dropdown menu item to insert into a db. The codes I've tried from here isnt working with what I have. It creates the dropdown but never inserts the value. I'm including two textboxes that are working correctly, above and below in the form code.

What will fix this or what am I doing wrong? Thanks [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

The SQL code im using:

[code]if (isset($_POST['submitted'])) {

  require_once ('mysql_connect.php');
  
   $query = "INSERT INTO homes (category, type, address, location, zip, year, bedroom, bathroom, garage, lotsize, stories, active, price, description, date_entered)
   VALUES ('$category', '$type', '$address', '$location', '$zip', '$year', '$bedroom', '$bathroom', '$garage', '$lotsize', '$stories', '$active', '$price', '$description', NOW())";
   $result = @mysql_query ($query);
  
[/code]

The form code I'm using:

[code]<p>Category:<input type="text" name="category" size "15" maxlength="15" value="<?php if
    (isset($_POST['category'])) echo$_POST['category']; ?>" /></p>
    
    <p>Active:<select size="1" name="active">
    <option selected value="<?php if
    (isset($_POST['active'])) echo$_POST['active']; ?>">Y</option>
    <option value="<?php if
    (isset($_POST['active'])) echo$_POST['active']; ?>">N</option>
    </select></p>


    <p>Type:<input type="text" name="type" size "15" maxlength="15" value="<?php if
    (isset($_POST['type'])) echo$_POST['type']; ?>" /></p>
[/code]
Link to comment
Share on other sites

Your form looks fine.
The problem I see is with the variable on the insert. You need to set $catagory, $type, and $active.
I believe you rcode would only work of global variable are turned on, which they are not by default and should not be.

[code]if(isset($_POST['category'])){
$category = $_POST['category''];
}else{
$category = '';
}[/code]

Each one would have to be set

Ray
Link to comment
Share on other sites

[!--quoteo(post=385779:date=Jun 19 2006, 03:45 PM:name=craygo)--][div class=\'quotetop\']QUOTE(craygo @ Jun 19 2006, 03:45 PM) [snapback]385779[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Your form looks fine.
The problem I see is with the variable on the insert. You need to set $catagory, $type, and $active.
I believe you rcode would only work of global variable are turned on, which they are not by default and should not be.

[code]if(isset($_POST['category'])){
$category = $_POST['category''];
}else{
$category = '';
}[/code]

Each one would have to be set

Ray
[/quote]

Even if the rest of the fields are working fine?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.