Home Ask Login Register

Developers Planet

Your answer is one click away!

bpr February 2016

How to post values to database with php from options dynamically created with php

Sorry for the obtuse title not quite sure how to describe this one. I have options that are dynamically created through a call to a database with php. The dropdown list options are set like this:

<div class="input-group col-md-12"><span class="input-group-addon">Tag Source</span>
    <select class="form-control" name="tagtype" value="<?php echo addslashes($_POST['tagtype']); ?>">
        <option value="">Tag Source</option>
        foreach ($sources as $row) {
        <option value="'".<?php $row['sources']; ?>."'"><?php echo $row['sources']; ?></option>

When I update the database I thought it would update the value to what I have set it as with php:

<option value="'".<?php $row['sources']; ?>."'">

But instead it does not update the database properly. My guess is that I have to write a javascript function to set the value to post to the db but would welcome any instruction!

EDIT: This is how I update the database

$conn = new mysqli(intentionally left blank);

  if($_POST['submit']) {
    if ($_POST['tagname']=="") $error.="<br />Please enter a tag name!";
    if ($_POST['tagtype']=="") $error.="<br />Please enter a tag type!";
    if ($_POST['url']=="") $error.="<br />Please enter a tag URL!";
    if ($_POST['publisher']=="") $error.="<br />Please enter a publisher!";
    if ($_POST['advertiser']=="") $error.="<br />Please enter an advertiser!";
    if ($_POST['identifier']=="") $error.="<br />Please enter an ID!";
    if ($_POST['ecpm']=="") $error.="<br />Please enter the eCPM rate!";
    if ($_POST['ccpm']=="") $error.="<br />Please enter the eCPM rate!";
    if ($_POST['datebrokered']=="") $error.="<br />Please enter the date brokered!";


Nathan Chapman February 2016

You need the form to POST to a php script to update the db.

Check out php form handling here: http://www.w3schools.com/php/php_forms.asp

Make sure to handle the input properly (i.e. escape the input with http://php.net/manual/en/mysqli.real-escape-string.php) because a user could edit the <select> dropdown values and execute a SQL injection.

che-azeh February 2016

You are just returning the row from your tables as values to your option. You should actually echo them:

<option value="<?php echo $row['sources']; ?>">

Post Status

Asked in February 2016
Viewed 2,953 times
Voted 5
Answered 2 times


Leave an answer

Quote of the day: live life