prohor
prohor

Reputation: 21

How to send data using select tag with php to mysql table?

I am trying to use select tag to send data in mysql table using PHP. I can insert using text type by the following code

<td>
    <label class="control-label">Image Type </label>
</td>
<td>
    <input class="form-control" type="text" name="user" placeholder="Image Catagory value="<?php echo $user; ?>" />
</td>

I have tried the following code using select tag but it is not showing in html

<td>
    <label class="control-label">Image Type </label>
</td>
<td>
    <input class="form-control" type="<select>
    <option>dog</option>
    <option>cat</option>
    <option>car</option>

    </select>" name="user" placeholder="Image Catagory- ex. Pathology or Diagnostic or Dialysis" value="<?php echo $user; ?>" />
</td>

what I am doing wrong here? how to declare select tag here?

Upvotes: 0

Views: 420

Answers (3)

pardeep
pardeep

Reputation: 359

<html>
<head>
<title>insert data using select</title>
</head>
<body>
<form action="demo.php" method="post">
Name :<select name="data">
<option value="samsang">samsang</option>
<option value="nokia">nokia</option>
<option value="Lg">Lg</option>
<option value="Apple">Apple</option>
</select><br>
<input type="submit" name="submit" value="Insert">
</form>
</body>
</html>

<?php
if(isset($_POST['submit']))
{
    $data=$_POST['data'];
    $c=mysql_connect("localhost","root","");
    mysql_select_db("test");
    $ins=mysql_query("insert into option (name) values ('$data')",$c);
    if($ins)
    {
        echo "<br>".$data."inserted";
    }
    else
    {
        echo mysql_error();
    }
}
?>

Upvotes: 0

Bikesh M
Bikesh M

Reputation: 8383

<td><label class="control-label">Image Type </label></td>
<td>
 <select name="user">
  <option value="dog" <?php echo $user=="dog" ? "selected":""; ?> >Dog</option>
  <option value="cat" <?php echo $user=="cat" ? "selected":""; ?> >Cat</option>
  <option value="car" <?php echo $user=="car" ? "selected":""; ?>>Car</option>
</select> 
</td>

Upvotes: 0

Devsi Odedra
Devsi Odedra

Reputation: 5322

You cannot mix up input and select. here is code for select. in php you will get select option valu by select name.

 <td><label class="control-label">Image Type </label></td>
    <td><select class="form-control" name="selectName">
                <option value="dog">dog</option>
                <option value="cat">cat</option>
                <option value="car">car</option>
                </select>
    </td>

Upvotes: 1

Related Questions