1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to find out and use column names

Discussion in 'PHP' started by ingilizdili, Jan 26, 2011.

  1. #1
    Hello;
    I am trying to establish an online exam system. A teacher writes questions which are recorded into a Mysql table with php. Simultaneously a new, corresponding answer column is added to students table for each new question. I managed to create the question - answer form. However, I don't know how to arrange the php page which this form is posted. Below is the answer form. "ss" stands for question number
    $result = mysql_query("SELECT * FROM question
     WHERE kod='$kod' ORDER BY id asc") or die(mysql_error());
    echo "<table border='1'><form name='answers' method='post' action='get_answers.php'>";  
    echo "<input type='hidden' name='kod' value='", $kod, "' />";
    while($row = mysql_fetch_array( $result )) {  
    
    $question = $row['question'];
    echo "<tr><td>", $row['ss'], "- ", $question, "</td></tr>", "<tr><td><input type='radio' name='answer_", $row['ss'], "' value='", $row['a'], "' />", $row['a'], " <input type='radio' name='answer_", $row['ss'], "' value='", $row['b'], "' />", $row['b'], " <input type='radio' name='answer_", $row['ss'], "' value='", $row['c'], "' />", $row['c'], "<input type='radio' name='answer_", $row['ss'], "' value='", $row['d'], "' />", $row['d'], "<input type='radio' name='answer_", $row['ss'], "' value='", $row['e'], "' />", $row['e'], "<input type='hidden' name='ss' value='", $row['ss'], "' /></td></tr>"; 
    
    }
    echo "<tr><td><input type='submit' /></td></tr></form>";
    echo "</table>";
    
    PHP:
    In the get_answers.php page I can't use the traditional
    $answer_1 = $_POST['answer_1']
    PHP:
    method because I don't know how many answers there will be as there will be many different exams. This is a problem especially when I try to update the student rows in accordance with their answers because when more than enough number of columns are specified in the UPDATE clause, an error appears, saying "column answer_18 doesn't exist".
    Now, is it possible to determine the "answers_#" columns for each exam and write the update clause accordingly each time new answers are given?
     
    ingilizdili, Jan 26, 2011 IP
  2. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Let me simplify the question:
    The number of questions and the corresponding answers varies from exam to exam. The problem is how to set the UPDATE command, or the SET clause in it.
    UPDATE table_name
    SET answer_1 = '$answer_1', answer_2 = '$answer_2' 
    PHP:
    and so on. How long that will go depends on the number of answers. I can get the number but I don't know how to set it into that clause.
     
    ingilizdili, Jan 27, 2011 IP
  3. seoelk.com

    seoelk.com Peon

    Messages:
    19
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I don't think it is good idead to keep answers in columns.
    maybe try structure like:

    StudentAnswers Table
    id, student_id, exam_id, question_id, answer_id


    Or if you want to stick to your method, try to use query to check if column exists:
    
    SELECT [column_name] FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=’[table_name]’ 
    
    PHP:
     
    seoelk.com, Jan 27, 2011 IP
  4. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #4
    you want to check how many answers there will be. Easy solution

    it doesn't matter if you have $_POST var filled, it just fill blank field and then you will select only non-blank fields ;)

    It's little bit strange but I hope you will get it
     
    G3n3s!s, Jan 27, 2011 IP
  5. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I think I didn't express myself properly:) Let me try again.
    There will be one answer for each question. Answers by different students will be stored in different rows which include their numbers. So, the rows are already there. Therefore I need UPDATE. And the questions are stored in another table. All I want is to update the answer rows.
    So, how should I write the update command.
    for ($i = 1; $i<8;$i++)
    {
    ${"answer_".$i} = $_POST['answer_'.$i];
    
    
    mysql_query("UPDATE students SET ??????????????????????????????
    WHERE name='$name' and kod = '$kod'") 
    or die(mysql_error()); 
    }
    PHP:
     
    ingilizdili, Jan 27, 2011 IP
  6. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #6
    mysql_query("UPDATE students SET `somecolumn` = 'somedata'
    WHERE name='$name' and kod = '$kod'")
     
    G3n3s!s, Jan 27, 2011 IP