PHP错误:预期不同的插入

I have some PHP code that inserts data into a database. When I echo the table it has inserted the id instead of the category which is in the dropdown list.

The PHP code for the dropdown is:

<select name='category' id=category class="text">
     <option value='' name='option' selected>Select one</option>
<?Php
   require "config.php";// connection to database 
   $sql="select * from category ORDER BY `cat_id` ASC"; // Query to collect data 

   foreach ($dbo->query($sql) as $row) {
   echo "<option value=$row[cat_id]>$row[category]</option>";
   }
?>
</select>

The PHP code for inserting values is:

<?Php

    $for= $_GET['category']; //Take the value.
    $request= $_GET['option']; //Take the value.

 myquery="INSERT INTO request (`request for category`, `request`) VALUES ('$for','$request')";
 $query = mysql_query($myquery);

Because the name of your <select> field is category, so when it is submitted to your insert query, it will insert the id of that category.

So if you want to display the data from your request table instead of the ID, you can use JOIN.

SELECT a.category FROM request a LEFT JOIN category b ON a.request_for_category = b.cat_id

Advice:

  • Don't use spaces() for column names
  • Use at least *_real_escape_string() or better yet, prepared statement
  • Use mysqli_* instead of deprecated mysql_*