简体   繁体   中英

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>
        <?php
        foreach ($sources as $row) {
        ?>
        <option value="'".<?php $row['sources']; ?>."'"><?php echo $row['sources']; ?></option>
        <?php
        }
        ?>

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);
include('login.php');

  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!";
    else {


      if (mysqli_connect_error()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
      }

        $identifier = $_POST['identifier'];
        $sql = "SELECT unique_id FROM jpctags WHERE identifier=?";
        $stmt = $conn -> prepare($sql);
        $stmt -> bind_param('s',$identifier);
        $stmt -> execute();
        $stmt -> store_result();
        $stmt -> bind_result($uniqueid);
        $stmt -> fetch();

        if ($uniqueid) $error = "This tag already exits within the system, please edit the tag instead.";
        else {

            $tagname = $_POST['tagname'];
            $tagtype = $_POST['tagtype'];
            $identifier = $_POST['identifier'];
            $url = $_POST['url'];
            $publisher = $_POST['publisher'];
            $advertiser = $_POST['advertiser'];
            $ecpm = $_POST['ecpm'];
            $ccpm = $_POST['ccpm'];
            $datebrokered = $_POST['datebrokered'];

            $sql = "INSERT INTO jpctags (`tagname`, `tagtype`, `identifier`, `url`, `publisher`, `advertiser`, `ecpm`, `ccpm`, `datebrokered`, `user_id`) VALUES(?,?,?,?,?,?,?,?,?,?)";
            $stmt = $conn -> prepare($sql);
            $stmt -> bind_param('ssssssiisi',$tagname, $tagtype, $identifier, $url, $publisher, $advertiser, $ecpm, $ccpm, $datebrokered, $user_id);
            $stmt -> execute();

        }
      }
    }

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 (ie 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.

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']; ?>">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM