我正在尝试使用MySQL数据库表interest_type
中的描述填充结果我的私人功能是
private function Get_Interests_Types_From_DB()
{
$sql = "SELECT
InterestID,
InterestDescription
FROM
interest_type";
$result = mysqli_query($this->Con, $sql);
while($row = mysqli_fetch_array($result))
{
$arrayResult[] = $row;
}
return ($arrayResult);
}
我尝试使用此函数的代码区域位于同一类中的公共函数内。表单中的值是数字。我试图将$ interest变量与表中的InterestID绑定,然后打印InterestDescription,而不是$ interest的值。
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 />");
$interestDescription = $this->Get_Interests_Types_From_DB();
foreach($interests as $likes)
{
if($likes == $interestDescription['InterestID'])
echo $$interestDescription['InterestDescription'] . "<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);
}
我得到:Notice: Undefined index: InterestID in C:\xampp-portable\htdocs\A340\Assign5\Assign5_Club_Membership_Class.php on line 287
如何打印InterestDescription而不是$ interest的值?
答案 0 :(得分:1)
有:
while($row = mysqli_fetch_array($result))
{
$arrayResult[] = $row;
}
现在$arrayResult
,实际上是您的$interestDescription
,包含类似的内容:
$interestDescription[0]["InterestID"]
$interestDescription[1]["InterestID"]
...
而不是$interestDescription[InterestID"]
<强>更新强>
也许你需要这样的东西(假设$interests
拥有一系列ID)
$interestDescription = $this->Get_Interests_Types_From_DB();
foreach($interests as $likes) {
foreach($interestDescription as $desc) {
if($likes == $desc['InterestID']) {
echo $desc['InterestDescription'] . "<br />";
}
}
}