'populate edit form with data from 2 tables [closed]
My database looks like this:
Form
FormId,
Name,
Lastname,
age,
birthday,
email,
FavLanguage,
Vehicle
Vehicle
VehicleId,
FormId (FK),
VehSelection
I have an edit form which is a combination of both tables (everything from the Form table apart from the Vehicle attribute) and the VehSelection from the Vehicle table. The VehSelection is 3 check boxes. The problem is I can't populate forms for the user who has chosen more than 1 check box. If the user has only selected 1 check box, then I can "Edit" that record the the form loads fine. But if another user has checked more that 1 box (eg 2 or 3) every time I try to edit that record I get a blank page. The expected result that I want to get to is regardless of how many check boxes that user has chosen, the edit form loads with the populate data.
I have tried 4-5 variations of the SQL statement and even tried LEFT JOIN. I even put the FROM
in the table and the INNER JOIN
the other way round for both joins but to no avail.
This is my sql:
SELECT VehicleId, VehSelection,
Form.FormId, Form.FirstName, Form.LastName, Form.Email,
Form.Age, Form.Birthdate, Form.FavLanguage
FROM Vehicle
INNER JOIN Form ON Form.FormId = Vehicle.FormId
WHERE Form.FormId = '$FormId'";
Here is sample data:
Sample data from Form Table:
FormId Name Lastname age birthday email FavLanguage Vehicle
190 alex ferguson 38 16-03-2022 [email protected] JavaScript NULL
191 tom smith 33 09-06-1997 [email protected] CSS NULL
192 Frank Lampard 39 10-06-1992 [email protected] CSS NULL
193 John Atkins 23 11-07-2006 [email protected] JavaScript NULL
194 Chris Tucker 112 04-07-2001 [email protected] CSS NULL
Sample data from Vehicle table:
VehicleId FormId VehSelection
215 190 Plane
216 191 Plane
217 192 Yacht
218 192 SuperCar
219 192 Plane
220 193 SuperCar
221 193 Plane
Sample data from php file
188 Alex fernandes [email protected] 25 30-03-2022 JavaScript Yacht, SuperCar Edit Delete
190 alex ferguson [email protected] 38 16-03-2022 JavaScript Plane Edit Delete
191 tom smith [email protected] 33 09-06-1997 CSS Plane Edit Delete
192 Frank Lampard [email protected] 39 10-06-1992 CSS Yacht, SuperCar, Plane Edit Delete
193 John Atkins [email protected] 23 31-03-2022 CSS SuperCar, Plane Edit Delete
in the sample data from php file, only record 190 & 191 will show a form with populated data when I Click "Edit", the others will not show. I will get a blank page. This pattern is the same with other records. 190 & 191 shows because there is only one VehSelection. If there are more than one, I get a blank page
I am adding my Php code becuase I would like some help on it & I think it might be a logical error:
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
include ("db/connect.php");
include ('includes/error.php');
$FormId = isset($_GET['user_id']) ? $_GET['user_id'] : "";
$queryselect = "SELECT VehicleId, VehSelection, Form.FormId, Form.FirstName, Form.LastName, Form.Email, Form.Age, Form.Birthdate, Form.FavLanguage FROM Vehicle INNER JOIN Form ON Form.FormId = Vehicle.FormId WHERE Form.FormId = '$FormId'";
$result = mysqli_query($link, $queryselect);
echo ("Query after select executed is $queryselect</br>");
if (!$result) {
printf("Error in connection: %s\n", mysqli_error($link));
exit();
}
$table = [];
while ( $row = mysqli_fetch_assoc( $result ) ) {
$table[] = $row;
}
if ( count($table) >= 0) {
$first_name = $table[0]["FirstName"];
$last_name = $table[0]["LastName"];
$email = $table[0]["Email"];
$age = $table[0]["Age"];
$birthday = $table[0]["Birthdate"];
$favlanguage = $table[0]["FavLanguage"];
$VehSelection= $table[0]["VehSelection"];
}
else
{
exit;
}
echo ("Vehselection is $VehSelection</br>");
?>
<?php
echo ("Vehselection after select query execute is $VehSelection</br>");
echo ("Select Query after Update execution is $queryselect</br>");
var_dump($queryselect);
?>
The 2nd part of my code, from echo
of the $VehSelection
and $queryselect
I am showing for completeness & to show that I have been debugging. But I am not showing the whole code and where those echos slot in, because I am showing minimum code here to explain the problem.
Here is my HTML. This is not all of it, just relevant part:
<p>What do you have?</p>
<input type="checkbox" id="yacht" name="vehicle[]" value="Yacht" <?php echo ($VehSelection == 'Yacht') ? 'checked' : ''; ?> />
<label for="yacht" class="boxstyle"> I have a yacht</label><br>
<input type="checkbox" id="superCar" name="vehicle[]" value="SuperCar" <?php echo ($VehSelection == 'SuperCar') ? 'checked' : ''; ?> />
<label for="superCar" class="boxstyle"> I have a super car</label><br>
<input type="checkbox" id="plane" name="vehicle[]" value="Plane" <?php echo ($VehSelection == 'Plane') ? 'checked' : ''; ?> />
<label for="plane" class="boxstyle"> I have a plane</label><br><br><br>
<input type="submit" name="Submit" value="Submit">
Debugging/Trying to fix code
I thought because VehSelection is an array, and was showing for one value fine that I may need a loop. So I tried a foreach:
foreach ($table as $key => $VehSelection)
{
$VehSelection= $table[0]["VehSelection"];
}
And I tried this as well:
$arr_num=count($table);
$i=0;
while ($i < $arr_num)
{
$VehSelection[$i]= $table[0]["VehSelection"];
$i++;
}
But did not fix it.
Solution 1:[1]
Fixed
The if statement should be:
if ( count($table) == 0) { exit; }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | webdeveloper |