Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

insert query is not inserting the ID into my database table

I had it working earlier but then changed my database structure a bit, which didn’t or should not affect the code in question. I am trying to insert car models in a database, I have a number of brands in one table and then i want the actual cars in another, with the ID of the brand in it.
So for example, the ‘brands table consists of a ‘brandID’, ‘brand_name’ and ‘country’ columns, one of the brands i have is Toyota. Toyota’s ‘brandID’ is 26, therefore it should, after filling in my little form for inserting cars into my ‘cars’ table of the database, insert the number 26 into the ‘brandID’ column of the ‘cars’ table.

The ‘cars’ table only has 4 columns, ‘carID’, ‘brandID’, ‘model’ and ‘year’. When i fill in my little form, it inserts the correct carID, model and year, but not the brandID, that is 0 everytime. It might be a really simple fix, but I am just not seeing it. Below is my code, please help me so i can move on from this. At the bottom of this post are screenshots of my database setup, if my explanation wasn’t clear enough.

my form:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<h3 >Add a new car to the collection</h3><br />  
            <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"  method="POST">
                 <label>Year</label>  
                 <input type="text" name="year" class="form-control" />  
                 <br />  
                 <?php 
                 echo "Brand Name ";
                 echo "<select name='brandname' id='brandID'>";
                 while($row = $bqresult->fetch_assoc()) {
                    $bname = $row["brand_name"];
                    $brandID = $row["brandID"];
                    echo "<option value='$brandID'>$bname</option>";
                 }
                 echo "</select>";
                 ?>
                 <br>
                 <label>Model</label>  
                 <input type="text" name="model" class="form-control" />  
                 <br />  
                 <!-- <label>Engine Layout</label>  
                 <select name="engine_layout">
                    <option value="ff">FF</option> 
                    <option value="fr">FR</option> 
                    <option value="rr">RR</option> 
                    <option value="rr">AWD</option> 
                </select> -->
                 <br />  
                 <button type="submit" value="Submit" class="btn btn-primary">Submit</button>
            </form>  

the relevant php code:

$brandquery = "SELECT brand_name FROM brands ORDER BY brand_name";
$bqresult = $conn->query($brandquery);

if($_SERVER["REQUEST_METHOD"] == "POST"){
$year = $_POST['year'];
$brandname = $_POST['brandname'];
$model = $_POST['model'];

$insertquery = "INSERT INTO cars (year, brandID, model)
VALUES ('$year', '$brandname', '$model')";
$insertresult = $conn->query($insertquery);
header("location:add_car.php");
}

‘cars’ table: https://imgur.com/a/4IfBzXZ
‘brands’ table: https://imgur.com/a/o9InuhC

>Solution :

Your bqresult Select is missing the field BrandID which you assign to the option value, thats why it is 0

$brandquery = "SELECT brand_name, brandID FROM brands ORDER BY brand_name";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading