hey all.
i have some problems with a mysql loop
first, it doesn't show the correct number of rows where there is 3 rows in the database and the script only outputs two.
also, in the two rows it outputs, it is the same data instead of different data
here's where the problem seems to be
<?php
if ($sql3 > 0){
while($survey_choices = mysql_fetch_array($sql2, MYSQL_ASSOC)){
?>
<tr>
<td><input type = "text" name = "<?php echo $option_id; ?>-option_text" value = "<?php echo $survey_choices['option_text']; ?>" size = "15"></td>
<td><input type = "text" name = "<?php echo $option_id; ?>-option_position" value = "<?php echo $survey_choices['option_position']; ?>" size = "3" maxlength = "2"></td>
</tr>
<?php
$d++;
}}
?>
any ideas?
below is the whole file
<table width="100%" cellpadding="3" cellspacing="1" border="0" class="forumline">
<tr>
<td class="catLeft" colspan="3" height="28"><span class="cattitle">phpMySchoolSurvey: Modify a Survey</span></td>
</tr>
<tr>
<td class="row1" align="left" width="100%">
<span class="gensmall">
<?php
if (!defined('in_phpMySchoolSurvey'))
{
die('Hacking attempt');
exit;
}
if (!isset($_GET['survey_id'])){
//let's list surveys
$sql0 = mysql_query("SELECT * FROM `survey_description`");
$sql1 = mysql_num_rows($sql0);
if ($sql1 > 0){
//active surveys
$c = 0;
while ($sql2 = mysql_fetch_array($sql0)){
?>
<a href = "index.php?page=modify_survey&survey_id=<?php echo $sql2['0']; ?>"><?php echo stripslashes($sql2['2']); ?></a><br />
<?php echo stripslashes($sql2['3']); ?><br />
<br />
<?
$c++;
}
}
}
else
{
if(!isset($_POST['submit'])){
//get survey title, description, choices with full information
$survey_id = ($_GET['survey_id']);
$sql1 = mysql_query("SELECT * FROM `survey_description` WHERE `survey_id` = '$survey_id'");
$survey_title_description = mysql_fetch_array($sql1);
$sql2 = mysql_query("SELECT * FROM `survey_choices` WHERE `survey_id` = '$survey_id' ORDER BY `option_position` ASC");
$survey_choices = mysql_fetch_array($sql2);
$sql3 = mysql_num_rows($sql2);
$option_id = ($survey_choices[1]);
?>
<form action = "index.php?page=modify_survey&survey_id=<?php echo $survey_id; ?>" method = "post">
<input type = "survey_title" value = "<?php echo stripslashes($survey_title_description['2']); ?>" size = "30"><br />Survey Title<br />
<br />
<input type = "survey_title" value = "<?php echo stripslashes($survey_title_description['3']); ?>" size = "30"><br />Survey description/explanation<br />
<br />
<table>
<tr>
<td><span class="gensmall"><b>Option Text</b></span></td>
<td><span class="gensmall"><b>Option Position</b></span></td>
</tr>
<?php
if ($sql3 > 0){
while($survey_choices = mysql_fetch_array($sql2, MYSQL_ASSOC)){
?>
<tr>
<td><input type = "text" name = "<?php echo $option_id; ?>-option_text" value = "<?php echo $survey_choices['option_text']; ?>" size = "15"></td>
<td><input type = "text" name = "<?php echo $option_id; ?>-option_position" value = "<?php echo $survey_choices['option_position']; ?>" size = "3" maxlength = "2"></td>
</tr>
<?php
$d++;
}}
?>
</table>
<br />
<input type = "submit" name = "submit" value = "Submit">
</form>
<?php
mysql_free_result($sql1);
mysql_free_result($sql2);
}
else
{
$survey_id = ($_GET['survey_id']);
//process the modifications
/*$survey_title = mysql_real_escape_string($_POST['survey_title']);
$survey_description = mysql_real_escape_string($_POST['survey_description']);
$sql4 = ("UPDATE `survey_description` SET `survey_title` = '$survey_title' `survey_topic` = '$survey_description' WHERE `survey_id` = '$survey_id'");
mysql_query($sql4); */
$sql2 = mysql_query("SELECT * FROM `survey_choices` WHERE `survey_id` = '$survey_id' ORDER BY `option_position` ASC");
$survey_choices = mysql_fetch_array($sql2);
$option_id = ($survey_choices[1]);
$sql3 = mysql_num_rows($sql2);
$option_text = mysql_real_escape_string(''.$option_id.'-option_text');
$option_position = mysql_real_escape_string(''.$option_id.'-option_position');
$sql5 = ("UPDATE `survey_choices` SET `option_position` = '$option_position', `option_text` = '$option_text' WHERE `survey_id` = '$survey_id' AND `option_id` = '$survey_choices[1]'\n");
echo $sql5;
if ($sql3 > 0){
$c = 0;
while ($survey_choices = mysql_fetch_array($sql2)){
$option_text = mysql_real_escape_string(''.$option_id.'-option_text');
$option_position = mysql_real_escape_string(''.$option_id.'-option_position');
$sql5 = ("UPDATE `survey_choices` SET `option_position` = '$option_position', `option_text` = '$option_text' WHERE `survey_id` = '$survey_id' AND `option_id` = '$survey_choices[1]'\n");
echo $sql5;
}
$c++;
}
}}
?>
</span>
</td>
</tr>
</table>