Jump to content

How to insert this into mySQL?


BrainCrasher

Recommended Posts

I need help badly!

 

What I want to do is insert into database the value from the selected radio group buttons.. All of them. There are 10 radio groups total (they can be less, but not more).

 

Thanks!

<?php
   require_once('Connections/strana.php'); 
mysql_select_db($database_strana, $strana);

?>
<link href="css/styles.css" rel="stylesheet" type="text/css" />

<table width="100%" height="100%" style="margin-left:auto;margin-right:auto;" border="0">
<tr>
<td align="center">
<form action="" method="post" enctype="multipart/form-data" name="form1">
<table>
<?php
$tema = mysql_query("SELECT * from prasanja where tip=2")or die(mysql_error());

function odgovor1($string)
{
$string1 = explode("/", $string);
echo $string1[0];
}
function odgovor2($string)
{
$string1 = explode("/", $string);
echo $string1[1];
}


while ($row=mysql_fetch_array($tema))
{
	$id=$row['prasanje_id'];
	$prasanje=$row['prasanje_tekst'];
	$tekst=$row['odgovor'];
?>
    <tr>
    <td>
    </td>
    </tr>
    <tr>
    <td class="formaP">
<?php echo $prasanje?>
   </td>
   </tr>
   <tr>
   <td class="formaO">
      <p>
        <label>
          <input type="radio" name="Group<?php echo $id?>" value="<?php odgovor1($tekst) ?>"  />
          <?php odgovor1($tekst) ?></label>
        <br />
        <label>
          <input type="radio" name="Group<?php echo $id?>" value="<?php odgovor2($tekst) ?>" />
          <?php odgovor2($tekst) ?></label>
        <br />
      </p></td>
</tr>
<tr>
<td>
<br />

</td>
</tr>
<?php 




}
?>
</table>
<input align="left"type="submit" name="submit" 
value="Внеси" > 
</form>
</td>
</tr>
</table>

 

prasanje = question

tekst/odgovor = answer

 

The answer table:

id - primary

question_id - the questions ID whose answer is selected in the radio group

user_id - cookie takes care of this

answer - the value from radio group

date - automatic

Link to comment
Share on other sites

Don't create your fields with names such as "field#" where '#' is an identification for that field. Instead, create them as arrays using "field[]" or "field['#']". Anyway, I don't see anything in that script that would process the code. Are you planning to have the form submit back to the same page or a separate page?

 

Also, I'm confused by the output. It looks like you are creating two duplicate answers for each question. So, what difference does it make what radio button the user selects? It will submit the same answer. Plus, the HTML code appears to be a mess.

Link to comment
Share on other sites

The form will submit back to the same page..

action="<?php $_SERVER['PHP_SELF'];  ?>"

 

I don't know how to create the Insert part with arrays (field[]) .. If that can solve this than please show me how..

 

I know how to make a radio group with fixed(not from database) value, submit it and write it in a database. But I don't know how to do it with this.. so that's why I didn't write any code for processing...

 

I store the answer to the question in one field. I separate the answers like this: answer1 /answer2

The functions basically take the whole answer field from the database and store it in a string[]. If I echo string[0] it shows answer1, if I echo string[1] it shows answer2.

 

Link to comment
Share on other sites

OK< I've made quite a few changes in your original code as it was very difficult to follow. It is in a more logical flow than before. For example I made one function to retrieve the answers into an array. But, you shouldn't be storing your answers in this fashion anyway. There should be a separate table for the answers with a foreign key reference back to the question and each answer should have it's own unique ID. But, I went ahead and worked with what you have.

 

As I said, the radio groups should have names such as "name[iD]" where ID is the question ID. You can then create a loop in the processing script to loop through the values using

foreach($_POST['answer'] as $id => $tekst)

 

Anyway, like I said, I made a lot of changes. I couldn't test this as I don't have your database. So, there may be some minor typos and such. But the logic should be good. I assumed that the ID for the answers is an auto-increment field and that the date is automatically populated in the database. So, I left them both out of the insert query.

 

<?php

function odgovor($string)
{
    return array_slice(explode("/", $string), 0, 2)
}

require_once('Connections/strana.php'); 
mysql_select_db($database_strana, $strana);

$output = ''; //Temp var for the output
if(isset($_POST['answer']))
{
    ### THE FORM WAS SUBMITTED. INSERT DATA

    //Process the post data into INSERT data
    $user_id = intval($_COOKIE['user_id']);
    $values = array(); //Array to hold insert values for query
    foreach($_POST['answer'] as $id => $tekst)
    {
        //Sanitize the data. Also add any validation that you want, i.e. ensure answer has a value)
        //Discard any records with invalid data
        $question_id = intval($id);
        $answer - mysql_real_escape_string(trim($tekst))
        $values[] = "('$user_id', '$question_id ', '$answer')";
    }

    //Create and run the insert query
    if(count($values))
    {
        $query = "INSERT INTO answer_table (`user_id`, `question_id`, `answer`)
                  VALUES . " implode(', ', $values);
        $result = mysql_query($query) or die(mysql_error());

        $output = "Your test results have been submitted";
    }
}
else
{
    ### THE FORM WAS NOTE SUBMITTED. CREATE FORM.

    $query = "SELECT prasanje_id, prasanje_tekst, odgovor
              FROM prasanja
              WHERE tip = 2";
    $result = mysql_query($query) or die(mysql_error());

    while ($row=mysql_fetch_assoc($result))
    {
        $id       = $row['prasanje_id'];
        $prasanje = $row['prasanje_tekst'];
        $tekst    = odgovor($row['odgovor']); //Return an array with 2 answers

        $output .= "<form action='' method='post' enctype='multipart/form-data' name='form1'>\n";
        $output .= "<table>\n";
        $output .= "  <tr><td class='formaP'>{$prasanje}</td></tr>\n";
        $output .= "  <tr>\n";
        $output .= "  <td class='formaO'>\n";
        $output .= "    <p>\n";
        $output .= "      <label><input type='radio' name='answer[{$id}]' value='{$tekst[0]}' /> {$tekst[0]}</label>\n";
        $output .= "      <label><input type='radio' name='answer[{$id}]' value='{$tekst[1]}' /> {$tekst[1]}</label>\n";
        $output .= "    </p>\n";
        $output .= "  </td>\n";
        $output .= "  </tr>\n";
        $output .= "</table>\n";
        $output .= "<input align='left' type='submit' name='submit' value='Внеси' />\n";
        $output .= "</form>\n";
    }
}

?>
<html>
<body>

<link href="css/styles.css" rel="stylesheet" type="text/css" />
<table width="100%" height="100%" style="margin-left:auto;margin-right:auto;" border="0">
  <tr>
    <td align="center">
    <?php echo $output; ?>
    </td>
  </tr>
</table>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.