Need Help In the PHP script and Mysql
Please help me with this:
I am creating a survey facility for the administrator. The administrator is asked for the number of questions in the survey. And based on this a row is created in the table containing survey info. Based on number of questions the same number of rows are created in the question table. This has 4 columns for the answers with each question id the corresponding number of rows are created for the same in the question table.
Now I am trying to give a form on UI controlled by a loop so that admin can enter questions and answers one by one and the question table is updated each time.
survey table (sur_id [auto increment column],sur_subject,Sur_frm_dt,sur_to_dt,sur_is_active)
question table ( sur_id, q_id [auto increment column] , q_txt,ans1,ans2,ans3,ans4)
The page asks for survey details: survey subject , dates, and no of ques
and a row is generated in survey table.The survey id created and number of question is passed to the create question page. $sid
is survey id and $noq is number of questions.
The code is as follows. Please dont mind the novice logic and script:
$sid = intval ($_GET['ids']);
$noq = intval ($_GET['qn']);
for($noq !=0;$noq >=1;$noq--)
{
$q = "insert into sur_ques (sur_id) values ('$sid')";
$ex = mysql_query($q);
$rs = mysql_affected_rows();
if($rs ==1)
{
echo" Questions Rows Created Corresponding to Survey Subject";
}
?>
<form name="form1" method="post" action="<?php echo($PHP_SELF); ?>">
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr><br><b>Ques No-<?php echo"$noq";?></b></tr><br>
<tr><textarea name = "q" rows ="10" cols = "70" wrap = "hard" ></textarea></tr><br>
<tr><td><b>Ans 1:</b></td><td><input type="text" name="a1" size="37" /></td></tr>
<tr><td><b>Ans 2:</b></td><td><inp开发者_Go百科ut type="text" name="a2" size="37" /></td></tr>
<tr><td><b>Ans 3:</b></td><td><input type="text" name="a3" size="37" /></td></tr>
<tr><td><b>Ans 4:</b></td><td><input type="text" name="a4" size="37" /></td></tr>
</table>
<input type = "submit" name="qa" Value = "Add Q&A" />
<input type ="reset" Value="Reset" />
</form>
<?
if ($_POST['qa'])
{
$id = mysql_insert_id();
$result = mysql_query("update ques set q_txt = '$q', ans1 = '$a1' ans2 = '$a2' ans4 = '$a4' ans4 = '$a4' where q_id = '$id'");
if($r = mysql_num_rows($result))
{
echo" Question and answers updated";
}
}
else
{
break;
}
}
?>
1 immediate problem I am able to see in your code is that you are using a comparison operator in your for loop where it should be an assignment operator $noq != 0
should be $noq = 0
or some other value – Trevor 0 secs ago
I think you are mixing up server side and client side. The code you posted is processed at the moment someone opens the page.
If you want the individual questions to be updated the moment the visitor presses the submit button, you will need to use ajax to send the information of that specific form to the server to be processed while the form remains the same / does not reload.
As it is now, every time you open your page, new rows are added based on the GET
variables but no row is ever updated because you are never posting, I don´t see $PHP_SELF
defined anywhere but I suppose you mean / want $_SERVER['PHP_SELF']
.
精彩评论