【问题标题】:inserting data into a table while we had selected the primary key of the other table在我们选择了另一个表的主键时将数据插入到一个表中
【发布时间】:2013-09-10 10:55:41
【问题描述】:

这里是场景

我有一份产品清单。当用户单击产品时,它会转到显示产品详细信息的另一个页面。在产品详细信息页面上,我有一个用于对该特定产品进行评论的表格。现在我有两个表格,一个用于产品详细信息,另一个用于评论产品详细信息页面有主键 PID 和评论表有主键 RID 并且 PID 是评论表中的外键。请有人告诉我如何插入评论使用 PID 针对特定产品的数据库。

这是关系,如果有任何问题,请告诉我并建议我如何为该特定产品插入数据

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    您实际上想要的是与表评论 (1) 产品 (N) 的 1:N 关系。这意味着外键进入审查表。您的表结构实际上是正确的。

    如果您想为特定产品添加评论,您只需在添加评论的过程中保留 PID。

    如果不清楚,请更详细地说明您无法在数据库中插入评论的原因。

    干杯, 奥利维尔

    【讨论】:

      【解决方案2】:
      // pid of product being reviewed must be included on the review form
      // clean review form data
      $pid = preg_replace("/[^0-9]/", "", $_REQUEST["pid"]);
      $name = preg_replace("/[^0-9a-z. -]/i", "", $_REQUEST["name"]);
      $comments = preg_replace("/[^0-9a-z.?! -]/i", "", $_REQUEST["comments"]);
      $rating = preg_replace("/[^0-9]/", "", $_REQUEST["rating"]);
      
      // store review for pid
      // assumed pid, rating are integer, name, comment are varchar.
      $connhandle = mysqli_connect(...your database...);
      $sql = "insert into reviews (pid, name, comment, rating) values ($pid, '$name', '$comments', $rating)";
      $r = mysqli_query($connhandle, $sql);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-22
        • 1970-01-01
        • 2014-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多