how to edit my results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tabatsoy
Forum Commoner
Posts: 29
Joined: Thu Mar 13, 2008 10:14 am

how to edit my results

Post by tabatsoy »

Please help me with my code. All i want is when i press on edit, the form will be filled out with the rows that i wanted to edit
Here is my code:

Code: Select all

<?
    session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>English Questions</title>
 
<link href="Layout.css" rel="stylesheet" type="text/css" />
<script type="text/JavaScript">
<!--
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
//-->
</script>
</head>
 
<body>
<p align="center"><img src="images/logo.jpg" width="800" height="200" /><a name="top" id="top"></a></p>
<div align="center">
<?
    $db = mysql_connect("localhost");
    mysql_select_db("examination",$db);
    
    $edit_questionNumber = $_POST["edit_questionNumber"];
    $edit_question = $_POST["edit_question"];
    $edit_choiceA = $_POST["edit_choiceA"];
    $edit_choiceB = $_POST["edit_choiceB"];
    $edit_choiceC = $_POST["edit_choiceC"];
    $edit_choiceD = $_POST["edit_choiceD"];
    $edit_answer = $_POST["edit_answer"];
    
    $len_edit_qn = strlen($edit_questionNumber);
    $len_q = strlen($edit_question);
    $len_a = strlen($edit_choiceA);
    $len_b = strlen($edit_choiceB);
    $len_c = strlen($edit_choiceC);
    $len_d = strlen($edit_choiceD);
    $len_ans = strlen($edit_answer);
        if($len_edit_qn && $len_q && $len_a && $len_b && $len_c && $len_d && $len_ans){
            if(($edit_answer == $edit_choiceA) || ($edit_answer == $edit_choiceB) || ($edit_answer == $edit_choiceC) || ($edit_answer ==            $edit_choiceD)){
                $result = mysql_query("UPDATE englishQuestions
                                        SET question = '$edit_question', choiceA = '$edit_choiceA', choiceB = '$edit_choiceB',
                                            choiceC = '$edit_choiceC', choiceD = '$edit_choiceD', answer = '$edit_answer'
                                        WHERE englishQuestionID = '$edit_questionNumber'");
            }else{
                echo '<font class = "style2">d magkaparehas</font>';
            }
        }else if($len_edit_qn || $len_q || $len_a || $len_b || $len_c || $len_d && $len_ans){
            echo '<font class = "style2">fill in all the edit fields</font>';
        }
        
    $add_question = $_POST["add_question"];
    $choiceA = $_POST["add_choiceA"];
    $choiceB = $_POST["add_choiceB"];
    $choiceC = $_POST["add_choiceC"];
    $choiceD = $_POST["add_choiceD"];
    $answer = $_POST["add_answer"];
        
        $len_question = strlen($add_question);
        $len_choiceA = strlen($choiceA);
        $len_choiceB = strlen($choiceB);
        $len_choiceC = strlen($choiceC);
        $len_choiceD = strlen($choiceD);
        $len_answer = strlen($answer);
        if($len_question && $len_choiceA && $len_choiceB && $len_choiceC && $len_choiceD && $len_answer){
            if(($answer == $choiceA) || ($answer == $choiceB) || ($answer == $choiceC) ||($answer == $choiceD)){
                        $result = mysql_query("INSERT INTO englishQuestions
                                            (englishQuestionID, question, choiceA, choiceB, choiceC, choiceD, answer)
                                            VALUES(NULL, '$add_question', '$choiceA', '$choiceB', '$choiceC', '$choiceD', '$answer')");
            }else{
                echo '<font class = "style2">di magkaparehas</font>';
            }
        }else if($len_question || $len_choiceA || $len_choiceB || $len_choiceC || $len_choiceD || $len_answer){
            echo '<font class = "style2">fill in all the add fields</font>';
        }
    
?>
</div>
<table width="400" border="0" align="center" class="session">
  <tr>
    <td colspan="2" class="session" span>
    <?
        if(!$_SESSION['loggedin']){
            echo '<br><font size = "3" color = "#000000"><strong>this page is for administrators only</font></strong><br><br>';
            echo '<font size = "1"><a href = "login.php">click here to log in </a></font><br><br>';
            exit;
        }
        echo 'English Questions -- Good day '.$_SESSION['loggedin'].'.<br><br>';
    ?>    </td>
  </tr>
  <tr>
    <td width="375"><font size="1"><a href="LogicQuestions.php">Logic Questions</a></font></td>
     <td width="415"><font size="1"><a href="ResultsPage.php">Results Page </a></font></td>
  </tr>
   <tr>
    <td width="375"><font size="1"><a href="MathQuestions.php">Math Questions</a></font></td>
     <td width="415"><font size="1"><a href="logout.php">Log out </a></font></td>
  </tr>
</table>
<p align="center">
 
<table width="850" border="0"  align="center" class="tableresults">
  <tr>
    <td align="center" width = "50" valign="center"><strong>question #</strong></td>
    <td width="250" align="center" valign="center"><strong>question</strong></td>
    <td width="100" align="center" valign="center"><strong>choice A</strong></td>
    <td width="100" align="center" valign="center"><strong>choice B</strong></td>
    <td width="100" align="center" valign="center"><strong>choice C</strong></td>
    <td width="100" align="center" valign="center"><strong>choice D</strong></td>
    <td width="100" align="center" valign="center"><strong>answer</strong></td>
    <td width="50" align="center" valign="center"></td>
  </tr>
</table>
 
<table width="850" border="0"  align="center" class="underresult">
    <?
        $query = "SELECT *
                FROM englishQuestions";
        $result = mysql_query($query);
        for($i = 0; $i < mysql_num_rows($result); $i++){
            $question_number = mysql_result($result, $i, "englishQuestionID");
            $question = mysql_result($result, $i, "question");
            $choiceA = mysql_result($result, $i, "choiceA");
            $choiceB = mysql_result($result, $i, "choiceB");
            $choiceC = mysql_result($result, $i, "choiceC");
            $choiceD = mysql_result($result, $i, "choiceD");
            $answer = mysql_result($result, $i, "answer");
            
            if($i % 2){
                $bg = "#BED9EF";
            }
            else{
                $bg = "#FFFFFF";
            }
        
            echo '<tr bgcolor = "'.$bg.'">
                    <td width = "50" align = "center" valign = "center" >'.$question_number.'<td>';
                    echo '<td width = "250" align = "center" valign = "center">'.$question.'</td>';
                    echo '<td width = "100" align = "center" valign = "center">'.$choiceA.'</td>';
                    echo '<td width = "100" align = "center" valign = "center">'.$choiceB.'</td>';
                    echo '<td width = "100"align = "center" valign = "center">'.$choiceC.'</td>';
                    echo '<td width = "100" align = "center" valign = "center">'.$choiceD.'</td>';
                    echo '<td width = "100" align = "center" valign = "center">'.$answer.'</td>';
                    echo '<td width = "50" align = "center" valign = "center"><a href = "#edit">edit</a>';
                '</tr>';
        }
    ?>
</table>
<br /><br />
<a name="edit" id="edit"></a>
 
<form id="form1" name="form1" method="post" action="englishQuestions.php">
  <table width="400" border="0" align="center" class="tableform1">
    <tr>
      <td width="85">&nbsp;</td>
      <td width="305" rowspan="2"><strong>Edit Question</strong></td>
    </tr>
   
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="85"><div align="right">question # </div></td>
      <td width="305"><input name="edit_questionNumber" type="text" id="edit_questionNumber" size="5" /></td>
    </tr>
   
    <tr>
      <td><div align="right">question</div></td>
      <td><input name="edit_question" type="text" id="edit_question" size="40" /></td>
    </tr>
    
    <tr>
      <td><div align="right">choice A </div></td>
      <td><input name="edit_choiceA" type="text" id="edit_choiceA" /></td>
    </tr>
   
    <tr>
      <td><div align="right">choice B </div></td>
      <td><input name="edit_choiceB" type="text" id="edit_choiceB" /></td>
    </tr>
   
    <tr>
      <td><div align="right">choice C </div></td>
      <td><input name="edit_choiceC" type="text" id="edit_choiceC" /></td>
    </tr>
   
    <tr>
      <td><div align="right">choice D </div></td>
      <td><input name="edit_choiceD" type="text" id="edit_choiceD" /></td>
    </tr>
   
    <tr>
      <td><div align="right">answer</div></td>
      <td><input name="edit_answer" type="text" id="edit_answer" /></td>
    </tr>
   
    <tr>
      <td><div align="right">
          <input type="submit" name="Submit" value="edit" />
      </div></td>
      <td><input type="reset" name="Submit2" value="Reset" /></td>
    </tr>
  </table>
</form>
 
<div align="center"><a name="add" id="add"></a><br />
</div>
 
<form id="form2" name="form2" method="post" action="englishQuestions.php">
  <table width="400" border="0" align="center" class="tableform1">
    <tr>
      <td width="85">&nbsp;</td>
      <td width="305" rowspan="2"><strong>Add question</strong></td>
    </tr>
   
    <tr>
      <td>&nbsp;</td>
    </tr>
    
    <tr>
      <td><div align="right">question</div></td>
      <td><input name="add_question" type="text" id="add_question" size="40" /></td>
    </tr>
    
    <tr>
      <td><div align="right">choice A </div></td>
      <td><input name="add_choiceA" type="text" id="add_choiceA" /></td>
    </tr>
    
    <tr>
      <td><div align="right">choice B </div></td>
      <td><input name="add_choiceB" type="text" id="add_choiceB" /></td>
    </tr>
    
    <tr>
      <td><div align="right">choice C </div></td>
      <td><input name="add_choiceC" type="text" id="add_choiceC" /></td>
    </tr>
   
    <tr>
      <td><div align="right">choice D </div></td>
      <td><input name="add_choiceD" type="text" id="add_choiceD" /></td>
    </tr>
   
    <tr>
      <td><div align="right">answer</div></td>
      <td><input name="add_answer" type="text" id="add_answer" /></td>
    </tr>
    
    <tr>
      <td><div align="right">
        <input name="add" type="submit" id="add" value="add" />
      </div></td>
      <td><input type="reset" name="Submit3" value="Reset" /></td>
    </tr>
  </table>
  <div align="center"><br /></div>
</form>
 
<table width="400" border="0" align="center">
  <tr>
    <td><form id="form4" name="form4" method="post" action="">
      <input name="Submit4" type="button" onmouseup="MM_goToURL('parent','#top');return document.MM_returnValue" value="Back to Top" />
    </form>
    </td>
    <td><form id="form5" name="form5" method="post" action="">
      <div align="right">
        <input name="Submit5" type="button" onmouseup="MM_goToURL('parent','logout.php');return document.MM_returnValue" value="Log out" />
        </div>
    </form>
    </td>
  </tr>
</table><br />
 
</body>
</html>
 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: how to edit my results

Post by John Cartwright »

Whats your question? I don't think anyone here is going to do the work for you, sorry.

Let me get you started by providing some logic.

Code: Select all

<?php
if (count($_POST)) { //elements in post exist
  //form has been submitted, so lets grab values from the form and not database 
  $formFields = array('username' => $_POST['username']);
} else {
  //form has NOT been submitted, grab contents from database
  //mysql_query(.. ) 
  //etc 
  $formsFields = array('username' => $row['username'])
}
 
?>
<form> 
   <input name="username" value="<?php echo $formFields['username']; ?>">
</form>
 
Basically, if the form has been submitted then simply redisplay the values from $_POST (incase any errors occured we want to show the invalid values still). If the form has been submitted, this is also a good spot to perform validation and saving to the database. Otherwise, simply retrieve the values from your database.

And to be honest, asking someone to look through all that code on a community forum will likely lend itself to no replies. (I didn't even look at your code).
Post Reply