-1

I am making a survey question portal in which certain questions will appear on certain conditions like if age-band='18-25' and residential status='Homeowner' then questions like what brand is your washing machine and Do you have any mortgage will appear. If age-band = 26-30 and residential-status ='Rent Council' then question "Do you have a life insurance?" This will appear. Now I have written the code to display the questions but I am unable to save the respective questions and answers in the database.

This is what I have tried so far. (//Code for first.php)

<?php
include 'connect.php';
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
</head>
<body>
<form method="POST" action="display1.php">
    <select name="age_id">
        <?php
        $query = mysqli_query($con,"select * from age");
        while ($rows = mysqli_fetch_array($query)) {
        ?>
        <option value="<?php echo $rows['id']?>"><?php echo $rows['age']; ?></option>
        <?
        }
        ?>
    </select><br>

    <select name="res_id">
        <?php
        $query = mysqli_query($con,"select * from residential");
        while ($rows = mysqli_fetch_array($query)) {
        ?>
        <option value="<?php echo $rows['id']?>"><?php echo $rows['residential_status']; ?></option>
        <?
        }
        ?>
    </select><br>
    <input type="submit" name="sub" value="SUBMIT">
</form>
</body>
</html>

(//code for display1.php)

<?php
include 'connect.php';
$age_id = $_POST['age_id'];
$res_id = $_POST['res_id'];
$res = mysqli_query($con,"select * from parent_questions where age_id ='$age_id' && residential_id = '$res_id'");
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
    <style type="text/css">
        #new{display: none;};
    </style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js"></script>
<!-- cdn link for ajax -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<form method="POST" action="submit.php">
                            <tbody>
                                <?php
                                    while($rows=mysqli_fetch_assoc($res)){
                                    $opt_arr=explode("|",$rows['qoption']);                                 
                                ?>
                                    <tr id="div_<?php echo $rows["qid"]; ?>">
                                        <td><?php echo $rows['question']; ?></td>
                                        <td>
                                         <select class="form-control" name="<?php echo $rows['qid']; ?>">
                                            <option value="">Select</option>
                                                <?php
                                                    foreach($opt_arr as $v){
                                                ?>
                                                <option value="<?php echo trim($v); ?>"><?php echo $v; ?></option>
                                                <?php
                                                }
                                                ?>
                                         </select>
                                        </td>
                                    </tr>
                                <?php
                                }
                                ?>
                            </tbody>
        
    <input type="submit" name="sub" value="SUBMIT">
 
</form>
<script type="text/javascript">

            function newfun(datavalue){
                document.getElementById('new').style.display='block';
            $.ajax({
                    url: 'backend.php',
                    type: 'POST',
                    data: { datapost : datavalue},
                    success: function(result){
                        $('#getdata').html(result);
                    }
            });
        }
</script>
</body>
</html>

Now I can't use a static name for select because it will keep changing so I am just confused how will I be able to insert the respective question's option in the database like the washing machine question's option in washing machine field and insurance and mortgage options in mortagage field respectively.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Saurabh
  • 9
  • 4
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Apr 09 '22 at 09:33

1 Answers1

0

You can use array based names like:

<select class="form-control" name="Rows[<?php echo $rows['qid']; ?>]">

and in php get it as $_POST['Rows']['mortagage'] or anything else.

all inputs will available in $_POST['Rows'] array


you can use <?= "s.th" ?> instead of <?php echo "s.th" ?>

Milad
  • 51
  • 9