我正在开发一个动态网站。我有两个页面,第一个名为" testcreatorpd1003.php"和另一个" databaseconnectpd1003.php" 。一切都工作正常,直到我发现每次插入一些数据时,databaseconnectpd1003.php的其他部分正在执行。
以下是代码:
testcreatorpd1003.php
<body oncontextmenu="return false;">
<?php
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true && $_SESSION['user']=='administrator')
{
echo'<div class="usercheck"><code><strong>WELCOME , YOU ARE LOGGED IN. QUICK LINKS :</strong>  <a class="panelbox" href="admin.php">ADMIN PANEL</a></code> || <code><a class="panelbox" href="logout.php">LOGOUT</a></code> </div>';
}
elseif (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true && $_SESSION['user']=='user')
{
echo'<div class="usercheck"><code><strong>WELCOME , YOU ARE LOGGED IN. QUICK LINKS :</strong>  <a class="panelbox" href="user.php">USER PANEL</a></code>   ||  <code><a class="panelbox" href="logout.php">LOGOUT</a> </code></div>';
}
else
{
header('Location: faculty-login.php');
}
?>
<div class="addquestion">
<form action="databaseconnectpd1003.php" method="post">
<center>
<b>QUESTION</b> :<br>
<textarea name="question" rows="2" cols="50" placeholder="Please write your question here..." style="margin: auto; width: 70%;"></textarea>
</textarea><br>
<b>OPTIONS</b> :<br>
<strong>OPTION ID : 1</strong>
<input type="text" class="input-generic" name="o1" placeholder="Write first option here"><br>
<strong>OPTION ID : 2</strong>
<input type="text" class="input-generic" name="o2" placeholder="Write second option here"><br>
<strong>OPTION ID : 3</strong>
<input type="text" class="input-generic" name="o3" placeholder="Write third option here"><br>
<strong>OPTION ID : 4</strong>
<input type="text" class="input-generic" name="o4" placeholder="Write fourth option here"><br>
<strong>OPTION ID : 5</strong>
<input type="text" class="input-generic" name="o5" placeholder="Write fifth option here"><br>
<hr>
<b>Correct ID number</b> :
<input type="text" name="answer" class="input-generic" placeholder="1 OR 2 OR 3 OR 4 OR 5"><br>
<b>Max. Marks alloted</b> :
<input type="text" class="input-generic" name="marks"><br>
<input type="submit" name="submit">
<input type="reset" name="reset">
</center>
</form>
</div>
<小时/> 的 databaseconnectpd1003.php
<body oncontextmenu="return false;">
<?php
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true && $_SESSION['user']=='administrator')
{
echo'<div class="usercheck"><code><strong>WELCOME , YOU ARE LOGGED IN. QUICK LINKS :</strong>  <a class="panelbox" href="admin.php">ADMIN PANEL</a></code> || <code><a class="panelbox" href="logout.php">LOGOUT</a></code> </div>';
}
elseif (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true && $_SESSION['user']=='user')
{
echo'<div class="usercheck"><code><strong>WELCOME , YOU ARE LOGGED IN. QUICK LINKS :</strong>  <a class="panelbox" href="user.php">USER PANEL</a></code>   ||  <code><a class="panelbox" href="logout.php">LOGOUT</a> </code></div>';
}
else
{
header('Location: faculty-login.php');
}
?>
<div class="testcategory">
<?php
$servername="localhost";
$username="root";
$pass="";
$db="onlinetest";
$connect=mysqli_connect($servername,$username,$pass,$db);
if($connect)
{
$question=$_POST["question"];
$option1=$_POST["o1"];
$option2=$_POST["o2"];
$option3=$_POST["o3"];
$option4=$_POST["o4"];
$option5=$_POST["o5"];
$answer=$_POST["answer"];
if($answer==1)
{
$answertext=$_POST["o1"];
}
elseif($answer==2)
{
$answertext=$_POST["o2"];
}
elseif($answer==3)
{
$answertext==$_POST["o3"];
}
elseif($answer==4)
{
$answertext==$_POST["o4"];
}
else
{
$answertext==$_POST["o5"];
}
$marks=$_POST["marks"];
$sql="INSERT into pd1003 values ('$question','$option1','$option2','$option3','$option4','$option5',$answer','$answertext','$marks')";
if(mysqli_query($connect,$sql))
{
echo"Question was added";
echo"<em> <a href='testcreatorpd1003.php'>Add another question</a></em>";
}
else
{
echo"Some error occurred . If error persists , please contact administrator".mysqli_connect_error();
echo"<em> <a href='testcreatorpd1003.php'>Add another question</a></em>";
}
}
else{
die("There was some error connecting to database. Please try again".mysqli_connect_error());
}
mysqli_close($connect);
?>
</div>
请告诉我我做错了什么。每次我插入一个带有选项的问题及其说的所有内容&#34;发生了一些错误。如果错误仍然存在,请联系管理员&#34;。一切看起来都很好。请帮忙!!
数据库名称为:onlinetest 表名:pd1003
答案 0 :(得分:0)
为安全起见,请尝试按以下方式查询
INSERT INTO pd1003 (field1, field2, field3, field4, field5, field6, field7, field8, field9) VALUES ('$question', '$option1', '$option2', '$option3','$option4', '$option5', '$answer', '$answertext', '$marks')