gpt4 book ai didi

PHP 表单转 MySQL - Array[] 转入 MySQL 问题

转载 作者:行者123 更新时间:2023-11-29 13:53:33 25 4
gpt4 key购买 nike

我正在开发一个允许俱乐部成员(member)注册的网站。表单中的信息应该存储到 MySQL 数据库中。我已经通过存储和读取 XML 样式文本文件来完成此任务,但现在我必须将其转换为 MySQL。以下是 index.php 的部分代码:

    <div id="rightcol">
<?php
include_once("Membership_Class.php");

$myClub = new Club("Localhost", "AMemberUser", "Pass123Word", "info_club");

if(isset($_GET['BodyContent']))
{
if($_GET['BodyContent'] == "about")
{
$myClub -> DisplayAbout();
}
else if($_GET['BodyContent'] == "register")
{
$myClub -> DisplayRegistrationForm();
}
else if($_GET['BodyContent'] == "processregistration")
{
$myClub -> ProcessRegistrationForm();
}
else if($_GET['BodyContent'] == "members")
{
$myClub -> DisplayMembers();
}
}
?>
</div>

下一部分来 self 的 Membership_Class.php 文件:

function DisplayRegistrationForm()
{
echo("<h2>Become a Club Member</h2>

<form name='register' method='post' action='Assign5_Solution.php?BodyContent=processregistration'>
<table>
<tbody>
<tr>
<td width='80px'>
<label>First Name: </label>
</td>
<td width='300'>
<input id='firstname' type='text' name='firstname' value='' required/>
</td>
</tr>
<tr>
<td>
<label>Last Name: </label>
</td>
<td>
<input id='lastname' type='text' name='lastname' value='' required/>
</td>
</tr>
<tr>
<td>
<label>Your Email: </label>
</td>
<td>
<input id='email' type='text' name='email' value='' required/>
</td>
</tr>
<tr>
<td>
<label>Gender: </label>
</td>
<td>
<input id='gender' type='radio' name='gender' value='male'>Male<br />
<input id='gender' type='radio' name='gender' value='female'>Female
</td>
</tr>
<tr>
<td>
<label>Interested in: </label>
</td>
<td id='check'>
<span style='font-weight: bold;'>Check All that Apply:</span><br />
<input id='interests' type='checkbox' name='interests[]' value='1'>Pizza Party<br />
<input id='interests' type='checkbox' name='interests[]' value='2'>Joining Study Groups<br />
<input id='interests' type='checkbox' name='interests[]' value='3'>Visiting Employer Sites<br />
<input id='interests' type='checkbox' name='interests[]' value='4'>Participating in Programming Competitions<br />
<input id='interests' type='checkbox' name='interests[]' value='5'>Building Games<br />
<input id='interests' type='checkbox' name='interests[]' value='6'>Becoming an Officer of the Club
</td>
</tr>
<tr>
<td colspan='2' style='text-align: center;'>
<input id='submit' type='submit' name='submit' value='Sign Up'/>
</td>
</tr>
</tbody>
</table>
</form>");
}

function ProcessRegistrationForm()
{
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$interests = $_POST['interests'];

if(!isset($_POST['firstname']) || !isset($_POST['lastname']) || !isset($_POST['email']) ||
($_POST['firstname']) == '' || ($_POST['lastname']) == '' || ($_POST['email']) == '')
{
echo("Please enter your first / last name and email.");
}
else
{
echo("<h2>Results</h2>");
echo("<div id='results'>");
echo $fname;
echo("<br />");
echo $lname;
echo("<br />");
echo $email;
echo("<br />");
echo $gender;
echo("<br />");
foreach($interests as $likes)
{
echo $likes . "<br />";
}
echo("<p style='font-weight: bold;'>Your data has been saved! We will contact you soon!</p>");
echo("</div>");
}

$myClub = new Club("localhost","A340User","Pass123Word","info_club");

$date = date("Y/m/d");

$sql="INSERT INTO member
(`FirstName`,`LastName`,`Gender`,`Email`,`MemberSince`)
VALUES
('$fname','$lname','$gender','$email','$date');";

$result = mysqli_query($this->Con,$sql);
if($result == true)
{
echo "Successful Insert<br />";
}
else
{
echo "Error Inserting class" . mysqli_error($this->Con) ." <br />";
}

for($i = 0; $i < sizeof($interests); $i++)
{
$interest = $interests[$i];

$sql="INSERT INTO member_interests
(`Email`,`InterestID`)
VALUES
('$email',$interest);";
}

$result = mysqli_query($this->Con,$sql);
if($result == true)
{
echo "Successful Insert<br />";
}
else
{
echo "Error Inserting class" . mysqli_error($this->Con) ." <br />";
}

现在我已经将其发布到我的数据库中,但是当我在测试表单数据时检查多个兴趣时,它只将我检查的兴趣之一发布到member_interests表中。显然,我的interests[]数组或循环在某处错误。

我的数据库名为 info_club,包含三个表:interest_typemembermember_interests。用户电子邮件是 key ID。在interest_type表中有两列,InterestIDInterestDescription。成员表中有 EmailFirstNameLastNameGenderMemberSince >。在我的 member_interests 表中,有一个 EmailInterestID 列。

我需要找出如何将多个兴趣添加到member_interests 表中。

最佳答案

好的,我将 $result = mysqli_query($this->Con,$sql); 行切换到 for 循环中,并且插入到数据库中效果很好。很简单。所以上面正确的代码是:

for($i = 0; $i < sizeof($interests); $i++)
{
$interest = $interests[$i];

$sql="INSERT INTO member_interests
(`Email`,`InterestID`)
VALUES
('$email',$interest);";

$result = mysqli_query($this->Con,$sql);
}

关于PHP 表单转 MySQL - Array[] 转入 MySQL 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16271144/

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