如何使用PHP动态创建的选项将值发布到数据库

时间:2016-02-08 20:34:32

标签: javascript php mysql

对不起,这个钝的标题不太清楚如何描述这个。我有通过使用php调用数据库动态创建的选项。下拉列表选项设置如下:

<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
        }
        ?>

当我更新数据库时,我认为它会将值更新为我使用php设置的值:

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

但它没有正确更新数据库。我的猜测是我必须编写一个javascript函数来设置发布到db的值,但欢迎任何指令!

编辑:这是我更新数据库的方式

$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();

        }
      }
    }

2 个答案:

答案 0 :(得分:1)

您需要将表单发送到php脚本以更新数据库。

在这里查看php表单处理:http://www.w3schools.com/php/php_forms.asp

确保正确处理输入(即使用http://php.net/manual/en/mysqli.real-escape-string.php转义输入),因为用户可以编辑<select>下拉列值并执行SQL注入。

答案 1 :(得分:1)

您只是将表格中的行作为值返回到option。实际上你应该echo他们:

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