0

I want to make questionnaire to show the test has done.
But, the answer button can't upload the answer to database.

I'm really confused.
This is my code named soal.php (thats means question.php)

<?php
include "header2.php";
?>
<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "magang";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * from kuesioner";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  echo "<div style='width:100%; border: 1px solid #EBEBEB; overflow:scroll;height:900px;'>";
  echo '<table width="100%">';
  $urut = 0;
  while($row = $result->fetch_assoc()) {
    $id_soal=$row["id_soal"];
    $soal=$row["soal"];
    $pilihan_a=$row["A"];
    $pilihan_b=$row["B"];
    $pilihan_c=$row["C"];
    $pilihan_d=$row["D"];

    ?>
    <form name="form1" method="post" action="jawab.php">
      <input type="hidden" name="id_user[]" value=<?php echo $id_soal; ?>>
      <input type="hidden" name="id_soal[]" value=<?php echo $id_soal; ?>>  
      <input type="hidden" name="jumlah" value=<?php echo $result->num_rows; ?>>
      <tr>
        <td width="17"><font color="#000000"><?php echo $urut=$urut+1; ?></font></td>
        <td width="430"><font color="#000000"><?php echo "$soal"; ?></font></td>
      </tr>
        <td height="21"><font color="#000000">&nbsp;</font></td>
        <td><font color="#000000">
          A.  <input name="pilihan[<?php echo $id_soal; ?>]" type="radio" value="1"> 
          <?php echo "$pilihan_a";?></font> </td>
      </tr>
      <tr>
        <td><font color="#000000">&nbsp;</font></td>
        <td><font color="#000000">
        B. <input name="pilihan[<?php echo $id_soal; ?>]" type="radio" value="2"> 
        <?php echo "$pilihan_b";?></font> </td>
      </tr>
      <tr>
        <td><font color="#000000">&nbsp;</font></td>
        <td><font color="#000000">
        C.  <input name="pilihan[<?php echo $id_soal; ?>]" type="radio" value="3"> 
        <?php echo "$pilihan_c";?></font> </td>
      </tr>
      <tr>
        <td><font color="#000000">&nbsp;</font></td>
        <td><font color="#000000">
        D.   <input name="pilihan[<?php echo $id_soal; ?>]" type="radio" value="4"> 
        <?php echo "$pilihan_d";?></font> </td>
      </tr>


    <?php 
  }
} else {
  echo "0 results";
}
$conn->close();


?>

<tr>
  <td>&nbsp;</td>
  <td><center><input type="submit" name="submit" value="Jawab" onclick="return confirm('Apakah Anda yakin dengan jawaban Anda?')"></center></td>

</tr>
</table>
</form>
</p>
</div>

But I already tried to make button for the answer, but I can't.
I can't create the right code for the answer button.

This my code for answers, named jawab.php (answer.php)

<?php
mysql_connect("localhost","root","");
mysql_select_db("magang");

$id_soal=$_POST["id_soal"];
$pilihan = $_POST["pilihan"];
$no = 0 ;

foreach ($_POST['id_soal'] as $row) {
  echo "<br>";
  $a = $id_soal[$no];
  echo $a;
  echo "<b r>";
  echo $pilihan[$a];
  $no = $no + 1 ;
}   

?>
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
  • 2
    The `mysql_` constructor is [**deprecated as of PHP 5.5**](https://wiki.php.net/rfc/mysql_deprecation), and is outright [**removed in PHP 7**](https://wiki.php.net/rfc/remove_deprecated_functionality_in_php7#extmysql). Also, the problem could be that you are mixing `mysql` and `mysqli`. See [**this post**](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) for more info, and use `mysqli` for the `jawab.php` page instead, ensuring that you also use [**parameterised queries**](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) to prevent SQL Injection :) – Obsidian Age Jul 21 '17 at 03:55
  • but int soal.php There is no wrong code right? because its work,just the button answer itss not work – Yosafat Tobing Jul 21 '17 at 04:13
  • i read the article said i must use array for multiinsert to database,but i confuse because have the radiobutton.Can you help me? – Yosafat Tobing Jul 21 '17 at 04:16
  • 1
    You code looks like a mess. Seriously. There is no `` opening tag and a missing `` tag. -- The `` opening tag is in the `while` loop... And the `` is outside. So you are having **many** forms named "form1", but only the last is closed and is including the `submit` button. -- Just start with a clean up on this.
    – Louys Patrice Bessette Jul 21 '17 at 05:05
  • Your code is really a mess.. Rearrange your code and add broken tags – DEarTh Jul 21 '17 at 05:59
  • echo ''; is it not table opening? thanks,i already fix about .
    – Yosafat Tobing Jul 24 '17 at 02:21
  • how about jawab.php guys? help me please – Yosafat Tobing Jul 24 '17 at 02:24

0 Answers0