Jump to content

How to display multiple dropdown menus from database and save all the data?


dwex

Recommended Posts

This is actually a page to edit the drama details.

Database-  3 tables

 

1. drama                          dramaID    drama_title

                                              1              friends

 

2. drama_genre            drama_genreID    dramaID    genreID

                                              1                        1              2

                                              2                        1              1

                                              3                        1              3

3. genre                              genreID      genre

                                                1            comedy

                                                2            romance

                                                3            family

                                                4            suspense

                                                5            war

                                                6            horror


 

<?php     

 

$dbhost = 'localhost';

$dbuser = 'root';

$dbpass = '';

$dbname = 'drama';

$link = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or trigger_error('Error connecting to mysql');

 

$id = $_GET['dramaID'];

 

$link2 = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or trigger_error('Error connecting to mysql');

$sql2 = "SELECT * from drama , drama_genre , genre

WHERE drama.dramaID='".$id."' AND drama_genre.dramaID='".$id."' AND drama.dramaID = drama_genre.dramaID AND drama_genre.genreID = genre.genreID";

$status2 = mysqli_query($link2,$sql2) or die (mysqli_error($link2));

 

$link3 = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or trigger_error('Error connecting to mysql');

$sql3 = "SELECT * from genre ";

$status3 = mysqli_query($link3,$sql3) or die (mysqli_error($link3));

 

<form method='Post' action='doUpdate.php' enctype="multipart/form-data">

 

<?php while ($row2 = mysqli_fetch_assoc($status2)) { ?>

<td height="1"></td>

 

                            <td><select name="gdrop">

 

<option value="<?php echo $row2['genreID'];?>"><?php echo $row2['genre'];?></option>

 

<?php while ($row3 = mysqli_fetch_assoc($status3)) { ?>

 

<option value="<?php echo $row3['genreID'];?>"><?php echo $row3['genre'];?></option>

 

 

<?php } mysqli_close($link3);  ?>

 

<input type='hidden' id='drama_genreID' name='drama_genreID' value = "<?php echo          $row2['drama_genreID']; ?>"/>

  </select>

 

<input type="hidden" id="dramaID" name="dramaID" value = "<?php echo $row['dramaID'];?>"/>

<input type="submit" Value="Update"/>


 

The result was there were 3 dropdown menus but only the first dropdown menu has all 6 genres from my database and also the genre that belongs to the drama.

 

I'm also wondering how I can bring all the drama_genreIDs to my save(doupdate.php) page and update all 3 of them because it seems like only the last dropdown menu's data is saved.

 

And also how can I display only 6 genres instead of 7 with the genre that belongs to the drama , being set as the default selection.

 

 

 

 

Link to comment
Share on other sites

$id = $_POST['dramaID'];

$dgen = $_POST['drama_genreID'];

$genre= $_POST['gdrop'];

 

$link7 = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or trigger_error('Error connecting to mysql');

$query7 = "UPDATE drama_genre SET genreID = '$genre' , dramaID = '$id' WHERE drama_genreID='".$dgen."'";

$result7 = mysqli_query($link7, $query7) or die(mysqli_error($link7));

 

}

mysqli_close($link7);

?>

 

This is how my saving code looks like which I think only saves one out of the three dropdown menu data.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.