2
$result = mysql_query("SELECT * 
                       FROM `tblquestion` 
                       WHERE questiontype = 'Methods' ", $connect);

<?php

echo "<br>";
while($row = mysql_fetch_array($result))
{
    echo "<br>";
    echo "<strong>" . $row["questionno"] . ".</strong> " . $row["question"] . "";
    echo "<br>";
    echo "<input type = radio name = ". $row ["questionid"] . "' id = '". $row ["questionid"] . "' value = 5/>5";
    echo "<input type = radio name = ". $row ["questionid"] . "' id = '". $row ["questionid"] . "' value = 4/>4";
    echo "<input type = radio name = ". $row ["questionid"] . "' id = '". $row ["questionid"] . "' value = 3/>3";
    echo "<input type = radio name = ". $row ["questionid"] . "' id = '". $row ["questionid"] . "' value = 2/>2";
    echo "<input type = radio name = ". $row ["questionid"] . "' id = '". $row ["questionid"] . "' value = 1/>1";
    echo "<br>";
    }
    echo "<br>";
?>

Is it possible to insert/post the values with this kind of radio buttons using mysql_fetch_array $row["questionid"]? I'm asking this since I cant really find a way to get the values of each respective $row["questionid"].

PICTURES:

Output on PHP

Database on phpMyAdmin

Francisco
  • 10,918
  • 6
  • 34
  • 45
Raniel Quirante
  • 315
  • 2
  • 15
  • 1
    Why not test it and see if it's possible ? – Epodax Jan 21 '16 at 13:15
  • Database = **MYSQL**. Tool to maintain and fiddle with a MYSQL Database = **phpMyAdmin** – RiggsFolly Jan 21 '16 at 13:18
  • Don't use "mysql" it is deprecated, use "mysqli" instead. – Naqash Malik Jan 21 '16 at 13:18
  • Im trying alot of possible codes like $_POST but $row['questionid'] is always being labeled as undefined index, even inside while or I make a variable for it. like $try = $row['questionid'] – Raniel Quirante Jan 21 '16 at 13:18
  • @malik I tried turning it to mysqli and an error pops up. Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, resource given in C:\xampp\htdocs\EvaluationDB\evaluationsheet.php on line 194 – Raniel Quirante Jan 21 '16 at 13:21
  • @RiggsFolly , sir what do you mean? I'm just a newbie about these things... please teach me about this tool. – Raniel Quirante Jan 21 '16 at 13:28
  • Just a note: You are giving all your radio buttons the same ID. An ID needs to be unique for one single element on the page. – M. Eriksson Jan 21 '16 at 13:28
  • @Magnus they are all different, considering every while is 1, 2, 3, 4, 5 ,6 up to 16, my only problem is how do I post the values in each radio buttons? – Raniel Quirante Jan 21 '16 at 13:29
  • @ChristianQuirante - I think he refers to your tag: `phpMyAdmin` when the question has nothing to do with it. – M. Eriksson Jan 21 '16 at 13:30
  • I am afraid that is not your only problem. You output **5** radio button for each ROW you get from the MySQL database. – RiggsFolly Jan 21 '16 at 13:31
  • 1
    @ChristianQuirante - Every while is different, but you're echoing 5 radiobuttons with the same ID for each while-iteration – M. Eriksson Jan 21 '16 at 13:31
  • @Magnus I see, so what kind of different approach should I do instead of this that I came up with? please see the 2nd picture so you can see my database. also, its not like im lazy to think of another way, please give me even just an idea on how to deal with this. I'm working on a evaluation sheet to evaluate a professor. – Raniel Quirante Jan 21 '16 at 13:35
  • @RiggsFolly is there any other way to approach this evaluation sheet to evaluate a professor, sir? please give me an idea so I can start working with it, asap. :( – Raniel Quirante Jan 21 '16 at 13:37
  • @Malik or PDO. You forgot to mention that when somebody is handling user input. Use prepared statements because otherwise the code is open to SQL-injections here is [the question](http://stackoverflow.com/q/60174/5396496) – BRoebie Jan 21 '16 at 14:59

1 Answers1

1

First off, if you're not using the ID of the radio buttons for something specific, remove that attribute. A HTML-element doesn't need an ID unless you need to target that specific element.

Secondly, your name attribute is invalid. In your current set up, you will only get a number as the name. Ex: name='1'. Or rather name=1'(you're missing the first ' in your code). Change your code to:

name='questions[". $row ["questionid"] . "]'

This will give you a name like name='question[1]'

When the page is posted, you can get the values like this:

foreach($_POST['questions'] as $questionId => $answer) {

    //...insert into your DB.

}

Don't forget to properly escape $questionId and $answer before you use them in your DB-queries.

Oh, yes... your values aren't quoted... value=5 should be value='5'

These are the most obvious issues.

...and as others have pointed out, you shouldn't use mysql_* functions since they are deprecated. Use MySQLi and if you get an error, read the PHP-docs on how to use them.

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • I'll try this, Thank you for the time to post this solution. – Raniel Quirante Jan 21 '16 at 13:58
  • Just remember that if someone posts your form without selecting anything in the radio buttons, the `$_POST['questions']` won't be posted so you need to check `if (isset($_POST['questions'])` before the loop. – M. Eriksson Jan 21 '16 at 14:02
  • I'll take note on that sir. to tell you the truth, I'm not familiar with the foreach code. where do I put it? inside the while code? – Raniel Quirante Jan 21 '16 at 14:17
  • ...no. You put it on the page you post the form to. You do know how to post data from a form to a PHP-page? – M. Eriksson Jan 21 '16 at 14:24
  • Yes, I do know sir. I'm now on the part where I'm already putting the foreach code inside the post form. I'm stuck on what kind of insert query I will put and what I would do to the "as $questionid => $answer". I'm actually confused on the escape code even though I've read the manual about it on php manual... – Raniel Quirante Jan 21 '16 at 14:41
  • trying this code... if(isset($_POST['questions'])) { foreach($_POST['questions'] as $questionId => $answer) { $AddQuery = "INSERT INTO tblanswer (answervalue) VALUES ($_POST['questions'])"; mysql_query($AddQuery, $connect); } } and got errors... – Raniel Quirante Jan 21 '16 at 14:41
  • Within the foreach-loop, `$questionId` is the ID for the question and `$answer` is the value. It's those values you need to insert into MySQL. Regarding the escaping, you really should look into mysqli and prepared statements instead. That's a bit outside the scope of this question though. – M. Eriksson Jan 21 '16 at 15:49
  • Thanks be to God for you patience to help me, may God bless you. Everything works fine now. :) – Raniel Quirante Jan 22 '16 at 09:53