简体   繁体   English

通过PHP将一个表插入另一个MySQL

[英]Insert from one table to another MySQL over PHP

I have a problem. 我有个问题。 I have 3 tables in MySQL two of it with 2 columns and one with tree columns like this : The first table has a column called city and one called id , the second one has a column called categories and a column called id and the third one has a column called product , one called city_id and one called categories_id . 我有3代表在MySQL两个它有2列和一个与树列这样的:第一个表有一栏叫city和一个叫id ,第二个有一个列名为categories和一列名为id ,第三个有一列称为product的列,一列称为city_id ,一列称为categories_id Now, the user can 现在,用户可以

  1. add a product, 添加产品,
  2. select city and 选择城市并
  3. select categories on a web page. 在网页上选择类别。

But, when the data are inserted to the database I want to insert in the third table the product name and, not to insert the city and the categories names, but their ids from their tables. 但是,当将数据插入数据库时​​,我想在第三张表中插入产品名称,而不是插入城市和类别名称,而是插入其表中的ID。 Can someone help me? 有人能帮我吗? (Sorry for my English) . (对不起我的英语不好) 。 Here is some code of what I done : (judete=city) 这是我所做的一些代码:(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>然后发布估值

use option value in category list 在类别列表中使用选项值

<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>

add option value 添加期权价值

<option value="'.$row->your_table_autoincrment_id.'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM