Generating an option box to update database

Hello. What I am trying to do is have a form that is filled with data from a database, then using a select box / option, i want to update the data.

I have managed to have the data added to the form input fields, when I use the option to select a record, only one record is shown.

Here is the HTML code for the form:

<form rol="form" method="post" action="update.php">
		
		<div class="form-group">
			
		<label for="option">Select Option</label>
		
		<select class="form-control" id="option" name="option">
			
			<option value="">No Options</option>
			
			<?php
				
				$q = "SELECT * FROM pages ORDER BY id";
				$r = mysqli_query($dbc, $q);
				
				while($datalist = mysqli_fetch_assoc($r)) { ?>
			
					<option value="<?php $datalist['id']; ?>"><?php echo $page['title']; ?></option>
				
				<?php } ?>
			
		</select>
		
		</div><!-- form group end: -->
		
		<div class="form-group">
		<label for="title">Title</label>
		<input type="text" class="form-control" id="title" name="title" value="<?php echo $page['title']; ?>">
		
		</div><!-- form group end: -->
	
		<div class="form-group">
			
		<label for="slug">Slug</label>
		<input type="text" class="form-control" id="slug" name="slug" value="<?php echo $page['slug']; ?>">
		
		</div><!-- form group end: -->
		
		<div class="form-group">
			
		<label for="body">Body</label>
		<textarea class="form-control rounded-0" name="body" id="body" rows="10"><?php echo $page['body']; ?></textarea>
		
		</div><!-- form group end: -->
		
		<div class="form-group">
			
		<label for="sidebar">Sidebar</label>
		<textarea class="form-control rounded-0" name="sidebar" id="sidebar" rows="10"><?php echo $page['sidebar']; ?></textarea>
		
		</div><!-- form group end: -->
	
		<button type="submit" class="btn btn-default">Submit</button>
		<input type="hidden" name="submitted" value="1">
		<input type="hidden" name="id" value="1">

	</form><!-- form end: -->

The online page looks like this - no options selected:

updatenolist

Once I select the option, I get the following:

list

This is not what I want. I have twenty entries for the title section, which I want to select to then enable me to update the data.

Where have I gone wrong?

Thanks in advance.

When you run this query in the database directly, what to you get as the result set? You are querying for EVERYTHING, so that is exactly what you will get back.

while($datalist = mysqli_fetch_assoc($r)) { ?>
		
<option value="<?php $datalist['id']; ?>"><?php echo $page['title']; ?></option>

$datalist vs $page …

1 Like

Thanks for the help. Did not notice the $datapage / $page variables.

Sponsor our Newsletter | Privacy Policy | Terms of Service