简体   繁体   English

在PHP表单中将复选框与MySQL和多个表一起使用

[英]Using checkboxes in a PHP form with MySQL and multiple tables

I am trying to use a PHP form (new.php) to update 2 MySQL tables and retrieve information from a 3rd. 我正在尝试使用PHP表单(new.php)更新2个MySQL表并从第3个表中检索信息。 The reason I need to use 3 tables is so that the information stored can be searched via another form (search.php) and, also, so that the information can be easily edited (edit.php). 我需要使用3个表的原因是为了可以通过另一种形式(search.php)搜索存储的信息,并且还可以方便地编辑信息(edit.php)。

New.php makes use of 4 text entry fields and, currently, 3 checkboxes (though this number will increase once I've gotten these 3 to work). New.php使用4个文本输入字段和当前3个复选框(不过,一旦我使这3个起作用,这个数字就会增加)。 The checkboxes act as "tags" that are used to describe articles that are entered into the database. 复选框用作“标签”,用于描述输入到数据库中的文章。 So, a single entry in the database contains the following information: 因此,数据库中的单个条目包含以下信息:

Article Title, Article Organization, Access Date, Article URL, and Article Tag(s) (and there is also an id column for this table. 文章标题,文章组织,访问日期,文章URL和文章标签(此表还有一个id列)。

The title, organization, date, and url are all stored in a table named articles . 标题,组织,日期和URL都存储在名为articles的表中。

The tags are all stored in a table named tags (there are 85 of them). 标签全部存储在名为tags的表中(其中有85个)。 This table has 2 columns: id and tag_contents . 该表有2列: idtag_contents

The 3rd table is a relation table (or maybe it's called an "intersection table"?) called articles_tags with 2 columns: article_id and tag_id. 第三个表是一个关系表(或者可能称为“交集表”?),称为articles_tags具有两列:article_id和tag_id。

What new.php currently does is add the title, organization, date, and url to the articles table and, then, uses mysql_insert_id() to get the ID of that entry. new.php当前要做的是将标题,组织,日期和url添加到articles表,然后使用mysql_insert_id()获取该条目的ID。

But, after that, I can't figure out what I need to do with my checkboxes. 但是,在那之后,我不知道我该如何处理复选框。

I have 3 checkboxes: 我有3个复选框:

<input type="checkbox" name="articletags[]" value="science" id="articletags_0" />
<input type="checkbox" name="articletags[]" value="geology" id="articletags_1" />
<input type="checkbox" name="articletags[]" value="astronomy" id="articletags_2" />

I need to somehow use those checkboxes to make a relation with the article. 我需要以某种方式使用这些复选框来与文章建立联系。 So, for example, after inserting an article about rocks the form would look like: 因此,例如,在插入有关岩石的文章后,表单将如下所示:

Article Title: Great new rocks!
Article Organization: US Geology Assoc.
Access Date: 09/20/2012
Article URL: www.rocks.com

Science [X]    Geology [X]    Astronomy [ ] (note that the X marks a checked checkbox)

And, the database tables would look like this: 并且,数据库表将如下所示:

(table: articles) id | articletitle     | articleorganization | articledate | articleurl
                  13 | great new rocks! | US geology assoc.   | 9/20/2012   | www.rocks.com


(table: tags)     id | tag_contents
                   5 | geology
                   9 | science

(table: articles_tags)   article_id  | tag_id
                            13       |   5
                            13       |   9

Note that the tags table doesn't change, it only stores the available tags and allows them to be referenced by the table articles_tags . 请注意, tags表不会更改,它仅存储可用标签,并允许表articles_tags引用它们。

I can not figure this situation out and I've been hacking at it for a week now. 我无法弄清楚这种情况,现在已经有一个星期了。 For reference, the code for new.php is added below: 作为参考,下面添加了new.php的代码:

    <?php 
 }

 // connect to the database
 include('settings.php');

 if(count($articletags) > 0)
{
 $articletags_string = implode(",", $articletags);
}
 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if($_SERVER['REQUEST_METHOD'] == 'POST')
 { 
 // get form data, making sure it is valid
 $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']));

 // check to make sure both fields are entered
 if ($articletitle == '' || $articleorganization == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
 renderForm($articletitle, $articleorganization);
 }
 else
 {
 // save the data to the database
 mysql_query("INSERT INTO articles SET articletitle='$articletitle',
      articleorganization='$articleorganization',
      articledate='$articledate',
      articleurl='$articleurl' ")

 or die(mysql_error()); 

 $article_id = mysql_insert_id();

 foreach( $POST_['articletags'] as $newtag )
{
   mysql_query('INSERT INTO articles_tags (article_id,tag_id) VALUES ($article_id, $newtag)');
}

 // once saved, redirect to success page
 header("Location:addsuccess.php");  
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','','','');
 }
?>
<input type="checkbox" name="articletags[]" value="5" id="articletags_0" />Science
<input type="checkbox" name="articletags[]" value="9" id="articletags_1" />Geology

then in php 然后在PHP

foreach( $POST_['articletags'] as $newtag )
{
   mysql_query('INSERT INTO articles_tags(article_id,tagID) VALUES( $idFromArticleInsert, $newtag )')
}

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

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