jan-vandenberg / cruddiy

No-code Bootstrap PHP CRUD generator
http://cruddiy.com
GNU Affero General Public License v3.0
253 stars 80 forks source link

Create/update display issue #48

Closed JPPhoto closed 2 years ago

JPPhoto commented 2 years ago

The following generated code for a foreign key doesn't display correctly, especially when the table (task_types) has only an ID and one other column:

                                <label>TaskID</label>
                                    <select class="form-control" id="TaskID" name="TaskID">
                                    <?php
                                        $sql = "SELECT *,ID FROM task_types";
                                        $result = mysqli_query($link, $sql);
                                        while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
                                            array_pop($row);
                                            $value = implode(" | ", $row);
                                            if ($row["ID"] == $TaskID){
                                            echo '<option value="' . "$row[ID]" . '"selected="selected">' . "$value" . '</option>';
                                            } else {
                                                echo '<option value="' . "$row[ID]" . '">' . "$value" . '</option>';
                                        }
                                        }
                                    ?>
                                    </select>
                                <span class="form-text"><?php echo $TaskID_err; ?></span>

The row returned by mysqli_query has ID as its first column and the resulting array_pop removes Name entirely, leaving just ID in the dropdown.

The same issue happens for other queries in this generated code - ID is the first column returned by mysqli_fetch_array and the last column always gets stripped out.

A generalized solution would be something like the following:

                                        while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
                                            $duprow = $row;
                                            unset($duprow['ID']);
                                            $value = implode(" | ", $duprow);
                                            // ...
jan-vandenberg commented 2 years ago

Aah, I've hit this bug a couple of times before (I noticed this especially with only two columns), thanks for pinpointing where this is!

I'll have to find the time to take a look at changing the code, but that might take some time.

Feel free to send a PR in the meanwhile!

JPPhoto commented 2 years ago

See #49 for my suggested fix.

jan-vandenberg commented 2 years ago

Merged! Thanks a lot!