The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Dropdown Menu with mySQL backend
zer0uk
post Mar 5 2020, 11:09 AM
Post #1





Group: Members
Posts: 2
Joined: 5-March 20
Member No.: 27,219



Hello,

must admit i'm very new to this and just a hobiest ,
I am creating a page which will be used for logging bike ride information.

With a mysql database i have two tables "tbl_rides" & "tbl_courses" tbl_rides is the main table for storing information and has a foreign key associating column "course_id" with course_id in the tbl_courses

What i want to do on a HTML/PHP page is display a drop down box that has the contents of "course_name" from the tbl_courses table but when the user selects it and submits the record is written to the main table "tbl_rides" using FK "course_id" so display the friendly name the foreign key but the record is written to "tbl_rides" use the associated field course_id

my work in progress code is below

<?php

// database connect

include('../db_connect.php');



//-------- Check Data -----------

//Check if Data is sent & validate

if(isset($_POST['name_submit'])){

if(empty($_POST['name_submit'])){

Echo 'No Course passed';

} else {

$var_course_id = mysqli_real_escape_string($conn, $_POST['course_id']);



//Create sql

$sql = "INSERT INTO tbl_rides(course_id) VALUES ('$var_course_id')";

//Save to DB and check

if(mysqli_query($conn, $sql)){

//success




} else {

echo 'query error: ' . mysqli_error($conn);

}

}



}



//end of checking



?>




<!DOCTYPE html>

<html>





<form class="white" action="dropdown.php" method="POST">

<select name="course_id">

<?php



// query to create course_id dropdown

$resultset = $conn->query("SELECT course_id FROM tbl_rides");

while($rows = $resultset->fetch_assoc())

{

$var_course_id = $rows['course_id'];

echo "<option value='$var_course_id'>$var_course_id</option>";

}

?>



</select>

<div class="centre" >

<input type="submit" name="name_submit" value="Submit my information !" />

</div>

</form>




</html>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 5 2020, 07:38 PM
Post #2


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



Are you getting an error (look in the php error log)? Exactly what problem are you having with the code?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
zer0uk
post Mar 8 2020, 09:35 AM
Post #3





Group: Members
Posts: 2
Joined: 5-March 20
Member No.: 27,219



Thanks managed to resolve this now.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 18th March 2024 - 11:44 PM