gpt4 book ai didi

php - 在带有 MySQL 和多个表的 PHP 表单中使用复选框

转载 作者:行者123 更新时间:2023-11-30 23:30:09 25 4
gpt4 key购买 nike

我正在尝试使用 PHP 表单 (new.php) 来更新 2 个 MySQL 表并从第 3 个表中检索信息。我需要使用 3 个表的原因是存储的信息可以通过另一个表单 (search.php) 进行搜索,同时也可以轻松地编辑信息 (edit.php)。

New.php 使用 4 个文本输入字段和目前的 3 个复选框(尽管一旦我让这 3 个起作用,这个数字将会增加)。复选框充当“标签”,用于描述输入到数据库中的文章。因此,数据库中的单个条目包含以下信息:

文章标题、文章组织、访问日期、文章 URL 和文章标签(此表还有一个 id 列。

标题、组织、日期和 url 都存储在名为 articles 的表中。

所有标签都存储在名为tags 的表中(共有 85 个)。该表有 2 列:idtag_contents

第三张表是一个名为 articles_tags 的关系表(或者它可能被称为“交集表”?),有 2 列:article_id 和 tag_id。

new.php 目前所做的是将标题、组织、日期和 url 添加到 articles 表中,然后使用 mysql_insert_id() 获取 ID该条目的。

但是,在那之后,我不知道我需要用我的复选框做什么。

我有 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" />

我需要以某种方式使用这些复选框来与文章建立联系。因此,例如,在插入一篇关于岩石的文章后,表单将如下所示:

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)

而且,数据库表看起来像这样:

(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

请注意,tags 表不会更改,它仅存储可用标签并允许它们被表 articles_tags 引用。

我无法弄清楚这种情况,我已经研究了一个星期了。作为引用,下面添加了 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

然后在 php 中

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

关于php - 在带有 MySQL 和多个表的 PHP 表单中使用复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11121472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com