Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    problem using a form to add to database

    i have created a form in which enters a question, and releveant answers into a database. the questions go into one table, and the ansers go into another.
    i have a piece of code (below) which enters this information into the 2 tables, but in the question table, only the first character of the question field is entered and the rest of the question is missing, e.g. if the question is "How old are you?" only "h" is entered into the table. All of the answer information enters into the answer table correctly. i have made sure that the field length is long enough in both the database and the form (60 characters), so that is not the problem. Can anyone see anything else that is wrong?

    Code:
    <?php
    $db = pg_connect("host=database.dcs.aber.ac.uk
    port=5432 dbname=agd8 user=***** password=*****");
    
    $output = "";
    if (isset($_POST['questionno']) && (isset($_POST['answerno']) && is_array($_POST['answerno']))) { // make sure that we have both sections, just in case.
       // first lets insert the question data:
       $questionno = isset($_POST['questionno'][$i]) ? pg_escape_string($_POST['questionno'][$i]) : ''; // ternary operator (? : ) acts as a short if/else. If isset set the variable to it after escaping it, else set it to ''
       $question = isset($_POST['question'][$i]) ? pg_escape_string($_POST['question'][$i]) : '';
       $quizref = isset($_POST['quizref'][$i]) ? pg_escape_string($_POST['quizref'][$i]) : '';
       
       $query="INSERT INTO questions (questionno, question, quizref) VALUES ('".$questionno."', '".$question."', '".$quizref."')";
       pg_query($query) or trigger_error ('Error adding new question: ' . pg_last_error());
       
       $output .= "Question has been added succesfully to the quiz reference {$quizref}<br />";
       
       $cnt = count($_POST['answerno']);
       for ($i=0; $i < $cnt; $i++) {
          // first define variables to avoid notice errors and escape the data:
          $answerno = isset($_POST['answerno'][$i]) ? pg_escape_string($_POST['answerno'][$i]) : null; 
          $answer = isset($_POST['answer'][$i]) ? pg_escape_string($_POST['answer'][$i]) : null;
          $answervalue = isset($_POST['answervalue'][$i]) ? pg_escape_string($_POST['answervalue'][$i]) : null;
          
          // check if all values are null, if they are skip the insert. 
          if (is_null($answer) && is_null($answerno) && is_null($answervalue)) 
             continue; // continue the loop as there was no data entered there to insert.
          
          $queryanswers="INSERT INTO answers (answerno, answer, answervalue, questionno, quizref)VALUES ('".$answerno."', '".$answer."','".$answervalue."', '".$questionno."', '".$quizref."')";
          
          pg_query($queryanswers) or trigger_error ('Error adding new answers: ' . pg_last_error());
       }
       
       $output .= "The answers have been succesfully added to the quiz reference {$quizref}.<br />";
    }
    ?>
    <html>
    <head>
    <title>Add </title>
    </head>
    <body>
    <?php echo $output; ?>
    
    <form method="post" action="">
    
    quizref: <br/>
    <input type="text" name ="quizref" size="5" /><br/>
    
    Question Number: <br/>
    <input type="text" name="questionno" size="5" /><br/>
    
    Question: <br/>
    <input type="text" name="question" size="60" /><br/>
    
    <table>
    Answers<br/><br/>
    
    <tr><td>Answer No.</td><td>Answer</td><td>Value</td></tr>
    <tr><td><input type="text" name ="answerno[]" size="5" /></td>
    
    
    <td><input type="text" name ="answer[]" size="60" /></td>
    
    
    <td><input type="text" name ="answervalue[]" size="5" /></td></tr>
    <tr><td><input type="text" name ="answerno[]" size="5" /></td>
    
    
    <td><input type="text" name ="answer[]" size="60" /></td>
    
    
    <td><input type="text" name ="answervalue[]" size="5" /></td></tr>
    <tr><td><input type="text" name ="answerno[]" size="5" /></td>
    
    
    <td><input type="text" name ="answer[]" size="60" /></td>
    
    
    <td><input type="text" name ="answervalue[]" size="5" /></td></tr>
    <tr><td><input type="text" name ="answerno[]" size="5" /></td>
    
    
    <td><input type="text" name ="answer[]" size="60" /></td>
    
    
    <td><input type="text" name ="answervalue[]" size="5" /></td></tr>
    <br/><br/>
    </table>
    
    
    <input type="submit" value="Send and Add another Add Questions" />
    <a href="addfinish.php"><input type="submit" value="Add and finish" /></a>
    
    </form>
    
    </body>
    </html>
    Any Help would be greatly appreciated

    Thanks
    Last edited by woodplease; 01-26-2010 at 06:08 PM. Reason: wrong information

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    Go into your MySQL admin page and see what parameters are set for "question" VARCHAR.
    I wonder if the length was not specified correctly, so it's length is 1.

    Also ... Remove this line from your post ... or erase the user and password (don't show us that) ...
    $db = pg_connect("host=database.dcs..........");

  • #3
    New Coder
    Join Date
    Nov 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    i'm using postgres, but i went into the admin and the field length is correct, i've evn tried recreating the table, and i still get the same problem

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Aren't you seeing the same thing for quizref and questionno, also? See this section of code:
    PHP Code:
       $questionno = isset($_POST['questionno'][$i]) ? pg_escape_string($_POST['questionno'][$i]) : ''// ternary operator (? : ) acts as a short if/else. If isset set the variable to it after escaping it, else set it to ''
       
    $question = isset($_POST['question'][$i]) ? pg_escape_string($_POST['question'][$i]) : '';
       
    $quizref = isset($_POST['quizref'][$i]) ? pg_escape_string($_POST['quizref'][$i]) : ''
    $i is undefined at that point, I think, so a zero index will be used. Since the POSTED values are strings (not arrays), you're setting the variables to the first character of the values. Remove that second array index.
    Are you a Help Vampire?

  • #5
    New Coder
    Join Date
    Nov 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks very much, the problem is now fixed


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •