【问题标题】:Using MySQL and a PHP form to insert data into a relational database使用 MySQL 和 PHP 表单将数据插入关系数据库
【发布时间】:2012-06-19 08:00:56
【问题描述】:

我有一个包含 3 个表的 MySQL 数据库:文章、标签和文章标签

每个的架构如下:

文章: id - 文章标题 - 文章组织 - 文章日期 - 文章网址

标签: id - tag_contents

articles_tags: id - article_id - tag_id

我还有 2 个 PHP 表单:addnew.php 和 edit.php

这个问题特别涉及 addnew.php,但也与 edit.php 有关。

addnew.php 中的表单按照其名称的含义在数据库中创建新条目。目前它使用了一些文本输入字段和 2 个复选框。因此,例如,我可以将文章的标题、作者、URL 和访问日期插入到 articles 表中,同时也将标签(通过选中一个或两个复选框)插入到 标签表。我无法弄清楚的是如何使这些表与第三个表相交?我在网上四处寻找,但似乎找不到我需要的东西。

addnew.php 的代码参考如下:

    <?php
     function renderForm($articletitle, $articleorganization, $articledate, $articleurl, $articletags )
     {
     ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    . . .
    </head>

    <body>

    <div class="container">
      <div class="header">
      . . .
      </div>
        <div class="sidebar1">
        . . .
        </div>
      <div class="content">
            <div id="stylized" class="myform">
          <form id="form" name="form" action="" method="post">
            <h1>Create a new entry in the database</h1>
            <table width="100%" border="0" cellpadding="6">
              <tr>
                <td colspan="2"><legend>Article details</legend></td>
              </tr>
              <tr>
                <td width="20%" align="right"><span class="field">Article Title:</span></td>
                <td width="80%" align="left"><span class="field">
                  <input name="articletitle" type="text" value="<?php echo $articletitle; ?>" size="50"/>
                </span></td>
              </tr>
              <tr>
                <td align="right"><span class="field">Article Author:</span></td>
                <td align="left"><span class="field">
                  <input name="articleorganization" type="text" value="<?php echo $articleorganization; ?>" size="50"/>
                </span></td>
              </tr>
              <tr>
                <td align="right"><span class="field">Access Date:</span></td>
                <td align="left"><span class="field">
                  <input name="articledate" type="text" value="MM/DD/YYYY" size="50"/>
                </span></td>
              </tr>
              <tr>
                <td align="right"><span class="field">Article URL:</span></td>
                <td align="left"><span class="field">
                <input name="articleurl" type="text" value="<?php echo $articleurl; ?>" size="50"/>
                </span></td>
              </tr>
              <tr>
                <td align="right"><span class="field">Article Tags:</span></td>
                <td align="left"><span class="field">
                        <input type="checkbox" name="articletags[]" value="geology" id="articletags_0" />
                        <input type="checkbox" name="articletags[]" value="astronomy" id="articletags_1" />
                </span></td>
              </tr>
            </table>
            <footer><input type="submit" name="submit" value="Add this Article"></footer>
            </form>
        </div>
      <div class="footer">
       . . .
       </div>
    </body>
    </html>
    <?php 
     }

     include('settings.php');

     if(count($articletags) > 0)
    {
     $articletags_string = implode(",", $articletags);
    }

     if($_SERVER['REQUEST_METHOD'] == 'POST')
     { 

     $articletitle = mysql_real_escape_string(htmlspecialchars($_POST['articletitle']));
     $articleorganization = mysql_real_escape_string(htmlspecialchars($_POST['articleorganization']));
     $articledate = mysql_real_escape_string(htmlspecialchars($_POST['articledate']));
     $articleurl = mysql_real_escape_string(htmlspecialchars($_POST['articleurl']));
     $articletags = implode(',', $_POST['articletags']);


     if ($articletitle == '' || $articleorganization == '')
     {

     $error = 'ERROR: Please fill in all required fields!';


     renderForm($articletitle, $articleorganization);
     }
      else
     {

     mysql_query("INSERT INTO articles SET articletitle='$articletitle', articleorganization='$articleorganization', articledate='$articledate', articleurl='$articleurl' ");
     mysql_query("INSERT INTO articles_tags SET articletags='$articletags' ")


     or die(mysql_error()); 


     header("Location:addsuccess.php"); 
     }
     }
     else

     {
      renderForm('','','','','');
     }
    ?>

【问题讨论】:

标签: php mysql


【解决方案1】:

您可以使用 mysql_insert_id 检索查询中的最后一个插入 ID。

mysql_query("INSERT INTO articles SET articletitle='$articletitle', articleorganization='$articleorganization', articledate='$articledate', articleurl='$articleurl' ");
$article_id = mysql_insert_id();
mysql_query("INSERT INTO tags SET articletags='$articletags' ");
$tag_id = mysql_insert_id();

【讨论】:

    【解决方案2】:

    使用mysql_insert_id 获取最后一个INSERT 语句的ID。

    http://php.net/manual/en/function.mysql-insert-id.php

    【讨论】:

      【解决方案3】:

      以防万一有人遇到这种情况并使用 PDO 而不是 mysql,在 PDO 中执行此操作的方法是:

      $article_id = $pdo->lastInsertId();
      

      将“$pdo”更改为您的数据库变量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-16
        • 2016-06-03
        • 1970-01-01
        • 2015-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多