如何在输入字段

时间:2015-07-30 11:52:12

标签: php mysql html5 select phpmyadmin

我在Xampp中使用phpmyadmin,在那里我制作了两张桌子; 表1:类别,其属性为cat_id和cat_name(其中cat_id是主键) 表2:项目的属性为item_id,item_name,item_price ... cat_id(其中item_id是主键,cat_id是外键) 我也在phpmyadmin中建立了正确的关系。 问题是在php中的select标签中使用所选cat_name的值,即cat_id。 PS。我知道自己是SQL Injection的主题。

  

PHP

<?php  
require ('config.php'); 
if(isset($_POST['check']))
{
if(isset($_POST['button'])) 
{
$catname = $_POST['cat'];

$que1 = "SELECT * FROM category WHERE cat_name = '$catname'";
$res1 = mysql_query($que1);
$row = mysql_fetch_array($res1);
$cat_db = $row['cat_name'];

if($catname == $cat_db || $catname == "")
{   
    echo "Catergory: $catname already exits. Failed to be inserted.";
}
else
{
    $que = "INSERT INTO category (cat_name) VALUES('$catname')";
    $res = mysql_query($que);
    echo "Catergory: $catname inserted successfully.";

}
die();
}


  if(isset($_POST['item_name']))
  {
      $i_id = $_POST['item_id'];
      $i_name = $_POST['item_name'];
      $i_quan = $_POST['item_quantity'];
      $i_size = $_POST['item_size'];
      $i_price = $_POST['item_price'];
      $cat_id = $_POST['cat_id'];

$que = "INSERT INTO item(item_name, item_quantity, item_size, item_price, cat_id) VALUES('$i_name','$i_quan','$i_size','$i_price', '$cat_id')";
$run = mysql_query($que);
if(!$run)
   echo "Item Details failed to Update.";       
  }
}
?>
  

HTML

<!DOCTYPE html>
<html>
<head>
<link rel = "stylesheet" href = "login.css">
</head>
<body>
<form action = "" method = "POST">

<p><label class = "field">Add Category:</label></p>
<input type = "text" name = "cat" class = "textbox-300" pattern = "[a-zA-Z0-9\. ]+"  title = "Please enter your Category Name">
<button type=  "submit" onclick = "location.href = '';" id = "savebutton" name = "button">Add Now</button>
<p><label class = "field">Add Item:</label></p>


<select name="cate">
<?php 
$que1 = "SELECT * FROM category";
$res1 = mysql_query($que1);
while($row = mysql_fetch_array($res1))
{    
$cat_id_db = $row['cat_id'];                 //use array over here
$cat_db = $row['cat_name'];                  //use array over here
?>

<option value="<?php echo $cat_id_db; ?>" ><?php echo $cat_db;?></option>

<?php } ?>
</select>


<?php 
$que1 = "SELECT * FROM category WHERE cat_name = '$cat_db'";      //yahan masla hai bhai, how do i set '$cat_db' into a static variable?
$res1 = mysql_query($que1);
$row = mysql_fetch_array($res1);
$cat_db_id = $row['cat_id'];
?>

<p><label class = "field">Category ID:</label></p>
<input type = "text" name = "cat_id" value = "<?php echo $cat_id_db; ?>" class = "textbox-300" pattern = "[a-zA-Z0-9\. ]+"> <!-- this is the PROBLEM how do i use the value of a selected option ONLY? -->
 <p><label class = "field">Item ID:</label></p>
 <input type = "text" name = "item_id" class = "textbox-300" pattern = "[a-zA-Z0-9\. ]+"title = "Please enter your Item ID">
 <p><label class = "field">Item Name:</label></p>
 <input type = "text" name = "item_name" class = "textbox-300" pattern = "[a-zA-Z ]+"title = "Please enter your Item Name">
 <p><label class = "field">Item Quantity:</label></p>
 <input type = "text" name = "item_quantity" class = "textbox-300" pattern = "[a-zA-Z0-9\. ]+"title = "Please enter your Item Quantity">
 <p><label class = "field">Item Size:</label></p>
 <input type = "text" name = "item_size" class = "textbox-300" pattern = "[a-zA-Z0-9\.\, ]+"title = "Please enter your Item Size">
 <p><label class = "field">Item Price:</label></p>
 <input type = "text" name = "item_price" class = "textbox-300" pattern = "[a-zA-Z0-9\.\, ]+"title = "Please enter your Item Price">


 <input type = "hidden" name = "check">
 <input type = "submit" class = "button" name = "sub"  value = "Save">

 </form> 
 </body>
 </html>

2 个答案:

答案 0 :(得分:0)

您将拥有所选子猫的ID。所以写一个查询来获取cat_id。

从sub_category中选择*,其中item_id =&#34;已发布ID&#34;并且您将获得主要类别ID。

答案 1 :(得分:0)

感谢会员在这里我找到了答案,所以我发布在这里,以防有人来这里寻找同样的事情。     

<select name="cate" onchange="changeInput(this.value);">
    <?php //your php code here ?>
</select>

<p><label class = "field">Category ID:</label></p>
<input type = "text" name = "cat_id" id="cat_id" value = "change select to see me change" class = "textbox-300" pattern = "[a-zA-Z0-9\. ]+"  title = "Please enter your Item Price">
</form>

<script>
var changeInput = function (val){
    var input = document.getElementById("cat_id");
    input.value = val;
}
</script>