1

I'm sure this has been answered a dozen times but no matter what example I try, I cannot get it to work.

I used to be good at PHP, since they made it more secure, my brain has melted.

I have a table that is built dynamically from records in a MySQL database. I have turned this into a form that populates with values from the database if they exist.

I now want to insert these rows into the database using the hidden user_id and toon_id. How?

This is the form:

//Create Form
echo '<form method="post">';
        //start table
        echo '<table border="0.5" cellspacing="4" cellpadding="4" id="toons"> 
                <tr> 
                    <th> Toon Name </th> 
                    <th> Side </th> 
                    <th> Area </th> 
                    <th> Origin </th> 
                    <th> Role </th>
                    <th> Star </th>
                    <th> Red Star </th>
                    <th> Level </th>
                    <th> Basic </th>
                    <th> Special </th>
                    <th> Ultimate </th>
                    <th> Passive </th>
                    <th> Gear </th>
                    <th> Power </th>
                    <th> Shards </th>
                </tr>';

        foreach($toons as $toon){

            echo '<tr>';
            echo '<td>'.$toon['toon_name'].'</td>';
            echo '<td><input type="hidden" id="toon_id" name="toon_id[]" value="'.$toon['toon_id'].'">'.$toon['toon_id'].'</td>';
            echo '<td>'.$toon['ToonID'].'</td>';
            echo '<td><input type="hidden" id="user_id" name="user_id[]" value="'.$toon['user_id'].'">'.$toon['user_id'].'</td>';
            echo '<td>'.$toon['role'].'</td>';               
            echo '<td> <select name="stars">';
                foreach($stars as $star) { 
                                            if($star['stars']==$toon['star']){echo '<option name="stars[]" selected value ='.$star['id'].'> ' .$star['stars'].'</option>';} 
                                        echo '<option name="stars[]" value ='.$star['id'].'> ' .$star['stars'].'</option>';
                                        }
            echo '                </select>
                </td>';
            echo '<td> <select name="redstars">';
                foreach($redstars as $redstar) { 
                                            if($redstar['redstars']==$toon['red_star']){echo '<option name="redstars[]" selected value ='.$redstar['id'].'> ' .$redstar['redstars'].'</option>';}
                                        echo '<option name="redstars[]" value ='.$redstar['id'].'> ' .$redstar['redstars'].'</option>';
                                        }
            echo '                </select>
                </td>';
            echo '<td> <select name="level">';
                foreach($level as $lvl) { 
                                            if($lvl['level']==$toon['level']){echo '<option name="level[]" selected value ='.$lvl['id'].'> ' .$lvl['level'].'</option>';}
                                        echo '<option name="level[]" value ='.$lvl['id'].'> ' .$lvl['level'].'</option>';
                                        }
            echo '                </select>
                </td>';
            echo '<td> <select name="basic">';
                foreach($basic as $bas) { 
                                        if($bas['level']==$toon['basic']){echo '<option name="basic[]" selected value ='.$bas['id'].'> ' .$bas['level'].'</option>';}
                                        echo '<option name="basic[]" value ='.$bas['id'].'> ' .$bas['level'].'</option>';
                                        }
            echo '                </select>
                </td>';
            echo '<td> <form><select name="special">';
                foreach($special as $spec) { 
                                            if($spec['level']==$toon['special']){echo '<option name="special[]" selected value ='.$spec['id'].'> ' .$spec['level'].'</option>';}
                                        echo '<option name="special[]" value ='.$spec['id'].'> ' .$spec['level'].'</option>';
                                        }
            echo '                </select>
                </td>';
            echo '<td> <select name="ultimate">';
                foreach($ultimate as $ult) { 
                                            if($ult['level']==$toon['ultimate']){echo '<option name="ultimate[]" selected value ='.$ult['id'].'> ' .$ult['level'].'</option>';}
                                        echo '<option name="ultimate[]" value ='.$ult['id'].'> ' .$ult['level'].'</option>';
                                        }
            echo '                </select> 
                </td>';
            echo '<td> <select name="passive">';
                foreach($passive as $pass) { 
                                            if($pass['level']==$toon['passive']){echo '<option name="passive[]" selected value ='.$pass['id'].'> ' .$pass['level'].'</option>';}
                                        echo '<option name="passive[]" value ='.$pass['id'].'> ' .$pass['level'].'</option>';
                                        }
            echo '                </select>
                </td>';
            echo '<td><select name="gear">';
                foreach($gear as $gr) { 
                                            if($gr['tier']==$toon['gear']){echo '<option name="gear[]" selected value ='.$gr['id'].'> ' .$gr['tier'].'</option>';}
                                        echo '<option name="gear[]" value ='.$gr['id'].'> ' .$gr['tier'].'</option>';
                                        }
            echo '                </select> 
                </td>';
                //IIF Power is null then empty textbox else fetch power value

            if($toon['power']===NULL){echo '<td> <input type="text" name="power[]" value=""> </td>';}else{
            echo '<td> <input type="text" name="power[]" value="'.$toon['power'].'"> </td>';}
            echo '<td> <select name="shards">';
                foreach($shards as $srds) { 
                                            if($srds['shards']==$toon['shards']){echo '<option name="shards[]" selected value ='.$srds['id'].'> ' .$srds['shards'].'</option>';}
                                        echo '<option name="shards[]" value ='.$srds['id'].'> ' .$srds['shards'].'</option>';
                                        }
            echo '                </select>

                </td>';
            echo '</tr>';
                         }
            echo '<td colspan=15><input type="submit" name="txtbutton" value="UPDATE ROSTER" /></td>';
            echo '</form>'; 
            echo '</table>';

The insert part is what I'm struggling with. I have this which echos the correct statement, but when I try to add an insert statement, I get unexpected errors or incorrect values get entered into the database. I'm sure there is something I'm doing wrong with arrays, but I don't know what.

        if(isset($_POST['txtbutton']))
        {
            echo "this has worked";
        $instsql = "INSERT INTO user_roster (user_id, toon_id, star, red_star, level, basic, special, ultimate, passive, gear, power, shards)
            VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";

        $inststmt = mysqli_prepare($link, $instsql);

        $inststmt->bind_param("ssssssssssss", $user_id, $toon_id, $_POST['star'], $_POST['red_star'], $_POST['level'], $_POST['basic'], $_POST['special'], $_POST['ultimate'], $_POST['passive'], $_POST['gear'], $_POST['power'], $_POST['shards']);

        $inststmt->execute();
    }
else
       { 
             echo 'nope';
        }

Any help is greatly appreciated.

  • 1
    I don't see the failed attempt to query. Too Broad. Also, you don't need to re-write the whole option conditionally, only the `selected` string. – mickmackusa Oct 27 '19 at 11:27
  • `select` tags get `name` attributes AND they need the names with square braces. The `option` tags don't get `name` attributes at all. (example: https://stackoverflow.com/q/47833626/2943403) – mickmackusa Oct 27 '19 at 11:42

0 Answers0