-1

I have some names stored in a database and this is what I use to access them and submit them through a form.

<form action="takeQuiz.php" method="post" id="takeQuiz">
   <?php $singleSQL = mysql_query("SELECT * FROM examtable");
    while($row = mysql_fetch_array($singleSQL)){
        $examName = $row['name'];
        echo '<p><input type="submit" value="'.$examName.'"/>';
    } ?>
</form>

Then on the takeQuiz.php I want to set a variable with the value of the submit

$examName = $_POST['.$examName.'];

$otherSQL = mysql_query("SELECT * FROM $examName");
Nate
  • 87
  • 10
  • Consult these following links http://php.net/manual/en/function.mysql-error.php and http://php.net/manual/en/function.error-reporting.php and apply that to your code, use `var_dump();` and look at your HTML source. – Funk Forty Niner May 05 '16 at 14:47
  • 3
    so you want a form with lot of submit buttons? – mitkosoft May 05 '16 at 14:47
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 05 '16 at 14:49
  • @mitkosoft in a sense, yes. There are going to be submit buttons for each name that exist in that table, then the user will click one button and redirect to a page where it takes the value of that button and accesses the information of that value. Also each value has a table in the database. – Nate May 05 '16 at 14:53
  • @Fred-ii- I get `Undefined index: examName in /path/takeQuiz.php on line 45` – Nate May 05 '16 at 14:56

2 Answers2

4

It is strange to build a form with buttons only. However you must add name attribute on these buttons (it is bad practice to have non-unique names into single form):

<form action="takeQuiz.php" method="post" id="takeQuiz">
    <?php $singleSQL = mysql_query("SELECT * FROM examtable");
        while($row = mysql_fetch_array($singleSQL)){
            $examName = $row['name'];
            echo '<input type="submit" name="submit" value="'.$examName.'"/>';
        } ?>
</form>

Then in your takeQuiz.php you can get it as:

$examName = $_POST['submit'];
$otherSQL = mysql_query("SELECT * FROM $examName");

And yes, stop using mysql_* functions, but use mysqli_* or PDO.

mitkosoft
  • 5,262
  • 1
  • 13
  • 31
  • *"but use mysqli_\* or PDO."* - You mean: *"but use mysqli_\* or PDO with a prepared statement"*. Those don't do much on their own. – Funk Forty Niner May 05 '16 at 15:03
  • This also works, and since there are some CSS styles associated with the buttons, this will work with what I envisioned. Thanks – Nate May 05 '16 at 15:05
-1
<?php $singleSQL = mysql_query("SELECT * FROM examtable");
while($row = mysql_fetch_array($singleSQL)){
    $examName = $row['name'];
    echo "<input type='radio' name='examName' value='{$examName}'>{$examName}<br>";
    echo '<p><input type="submit" >';
} ?>

takeQuiz.php

$examName = $_POST['examName'];
$otherSQL = mysql_query("SELECT * FROM $examName");
Deepak Adhikari
  • 419
  • 2
  • 4
  • This worked, I guess I'll have to fiddle around with the CSS with the radio buttons, but thanks a lot! – Nate May 05 '16 at 15:01
  • Why should the OP try this? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. A code only answer is even less desirable as it gives little clue to potentially new programmers for how the problem was solved. – Jay Blanchard May 05 '16 at 15:23