[英]Insert from one table to another MySQL over PHP
我有個問題。 我有3代表在MySQL兩個它有2列和一個與樹列這樣的:第一個表有一欄叫city
和一個叫id
,第二個有一個列名為categories
和一列名為id
,第三個有一列稱為product
的列,一列稱為city_id
,一列稱為categories_id
。 現在,用戶可以
但是,當將數據插入數據庫時,我想在第三張表中插入產品名稱,而不是插入城市和類別名稱,而是插入其表中的ID。 有人能幫我嗎? (對不起我的英語不好) 。 這是我所做的一些代碼:(judete = city)
<!DOCTYPE>
<?php
require 'conn.inc.php';
$sql = "SELECT name FROM judete";
if($result = $db->query($sql)){
// print 'Selectare judete cu succes';
}
$sqlProd = "SELECT categorie FROM categorii ORDER BY categorie";
$resultProd = $db->query($sqlProd);
@$produs = $_POST['produs'];
@$categorie =$_POST['categorie'] ;
@$judet =$_POST['judet'] ;
if(isset($produs)&&!empty($produs)){
$sqlTry = "INSERT INTO produse (produs, id_oras, id_categorie) VALUES ('$produs','$judet','$categorie')";
if($db->query($sqlTry)){
print 'Inserare cu succes!';
}else{
print 'Nu am putut sa inserez';
}
}
?>
<html>
<head>
<title>Adauga</title>
</head>
<body>
<h4>Introduceti produsul dorit : </h3>
<form method = "POST" action = "add.php">
Adauga produsul: <input type = 'text' name = 'produs' />
in categoria <select name = 'categorie'>
<?php while($row = $resultProd->fetch_object()){ print '<option>'.$row->categorie.'</option>';} ?>
</select>
din judetul
<select name = 'judet' >
<?php while($row = $result->fetch_object()){ print '<option>'.$row->name.'</option>'; }?>
</select>
<input type = 'submit' name = 'submit' value = 'adauga' />
</form>
</body>
</html>
您可以先找到其ID,然后插入其中,也可以在HTML中顯示該ID,例如<option value = "1">'.$row->categorie.'</option>
然后發布估值
在類別列表中使用選項值
<form method = "POST" action = "add.php">
Adauga produsul: <input type = 'text' name = 'produs' />
in categoria <select name = 'categorie'>
<?php while($row = $resultProd->fetch_object()){ print '<option value = "'.$row->cate_id.'">'.$row->categorie.'</option>';} ?>
</select>
din judetul
<select name = 'judet' >
<?php while($row = $result->fetch_object()){ print '<option value="'.$row->c_id.'">'.$row->name.'</option>'; }?>
</select>
<input type = 'submit' name = 'submit' value = 'adauga' />
</form>
添加期權價值
<option value="'.$row->your_table_autoincrment_id.'"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.