如何自动从父表获取id字段到子表

时间:2011-09-11 08:29:24

标签: php mysql forms

我的情景;

我有两张桌子:

首先是“products”表,其中包含以下内容:

  1. 的productid
  2. 产品名
  3. 描述
  4. 第二个是包含以下内容的“productusers”:

    1. ID
    2. productid - 第一张表中的外键
    3. usertype - 一个文本字段,其中浏览器可以输入任何用户类型的产品(例如电工,护士等)。
    4. 我有一个显示产品记录的php页面,同时一个表单,其中浏览器可以填写用户类型并单击提交按钮,下面是我的此页面代码:

      mysql_connect('localhost',$username,$password);
      @mysql_select_db($database) or die( "Unable to select database");
      
      
      $id=$_GET['productid']; // Collecting data from query string
      if(!is_numeric($id)){ // Checking data it is a number or not
      echo "Data Error"; 
      exit;
      }
      
      $result=mysql_query("select productid, productname, description from products where productid=$id ");
      
      
      if (!$result) {
      $message = "Invalid query: " . mysql_error() . "\n";
      die($message);
      }
      
      while ($row = mysql_fetch_assoc($result)) {
      echo $row['productid'] . "\n";
      echo "<br>";
      echo $row['productname'] . "\n";
      echo $row['description'] . "\n";
      
      echo "<br><br><br><br>";
      
      echo "This product is used by:";
      echo '<form action="addusers.php" method="post">';
      echo 'Product ID <input type="text" name="productid" value="';
      echo $row['productid'];
      echo '"';
      echo ' maxlength="50" disabled="disabled"/>';
      echo 'User Type <input type="text" name="proffession" maxlength="50"/>';
      echo '<input type="submit" name="formSubmit" value="Submit" />' ;
      echo '</form>';
      

      点击提交时处理我的表单的页面如下:

      mysql_connect('localhost',$username,$password);
      @mysql_select_db($database) or die( "Unable to select database");
      
      
      $sql = "INSERT INTO usedby (usedbyid, productid, proffession) VALUES ('','$_POST[productid]','$_POST[proffession]')";
      
      if (!mysql_query($sql))
      {
      die('Error: ' . mysql_error());
      }
      echo "Your Information Was Successfully Posted";
      

      该页面最终出现错误:

      Notice: Undefined index: productid in D:\chlark\xampp\htdocs\testing\addusers.php on line 22
      Error: Cannot add or update a child row: a foreign key constraint fails (`test`.`usedby`, CONSTRAINT `pid` FOREIGN KEY (`productid`) REFERENCES `products` (`productid`) ON DELETE NO ACTION ON UPDATE NO ACTION)
      

      我的目标是提交表单时必须自动获取相应的产品ID,我填写了数据库存储空间。这样我就可以对每个品牌的用户类型进行查询。例如,如果填写产品ID#45的表单,将存储在mysql上的数据将是值('','45','来自表单的输入')。

      我怎么能这样做?

      是否有一种方法可以让我不再需要在我的表单上显示此代码?

      echo 'Product ID <input type="text" name="productid" value="';
      echo $row['productid'];
      echo '"';
      echo ' maxlength="50" disabled="disabled"/>';
      

      只有

      echo 'User Type <input type="text" name="proffession" maxlength="50"/>';
      echo '<input type="submit" name="formSubmit" value="Submit" />' ;
      
      可以显示

      ,但单击提交按钮时,产品ID仍会保存在表格中。

      我是一个php mysql新手,谢谢。

1 个答案:

答案 0 :(得分:0)

是的,改变一下:

echo 'Product ID <input type="text" name="productid" value="';
echo $row['productid'];
echo '"';
echo ' maxlength="50" disabled="disabled"/>';

到此:

echo '<input type="hidden" name="productid" value="' . $row['productid'] . '"/>';