我正在开发一个应该允许俱乐部会员注册的网站。表单中的信息应该存储到 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>
下一部分来自我的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 表中。显然我的兴趣[] 数组在某处或循环中出错了。
我的数据库名为 info_club,包含三个表:interest_type
、member
和member_interests
。用户电子邮件是key id
. 在interest_type 表中有两列InterestID
, 和InterestDescription
。在成员表中有Email
、FirstName
、LastName
、Gender
和MemberSince
。在我的member_interests
表中有一个Email
和InterestID
列。
我需要找出如何将多个兴趣放入 member_interests 表中。