将下拉菜单值发布到mysql单元格

时间:2013-06-12 00:22:06

标签: php mysql post

好的,所以我在PHP和MySQL编程方面相当初学者,所以这个问题可能很吵,但无论如何这里是我的情况。我有一个带有下拉菜单的内容页面,它应该给我$ _POST值(选项来自数据库列):这是代码

    <link href="../css/pagestyle.css" rel="stylesheet" type="text/css" />
<?php
include("../panel/config.php");

    $db = mysqli_connect($server, $username, $password, $database);
    if(mysqli_connect_errno()) { //if connection database fails
     echo("Connection not established " .
     mysqli_connect_error($db) . "</p>");
       }  

    $query = "SELECT username FROM users WHERE email = '1' ORDER BY username ASC";

    $result = mysqli_query($db,$query);

    if (!$result) { 
     echo("Error, the query could not be executed: " .
     mysqli_error($db) . "</p>");
     mysqli_close($db);
     }

    echo "
  <form action='myscript' method='post'>
   <select name='test'>
        <option value = 'none' selected = 'selected' >
          `Select a DJ:` </option>";

    while ($row = mysqli_fetch_assoc($result)){
     echo '<option value="' . $row['username'] . '">' . $row['username']. '</option>';
       }
     echo"     
     <input type='submit' value='submit' name='submit'>
     </select>
    </form> ";
    ?>  

我知道这么小的功能的相当多的代码。无论如何,下拉菜单从数据库列获取其选项,并且工作正常,现在当我按下提交按钮时,它运行另一个PHP页面,其编码如下:

<link href="../css/pagestyle.css" rel="stylesheet" type="text/css" /><html>
<?php 
include("../panel/config.php");

$con = mysqli_connect($server, $username, $password, $database);
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$id = $_POST['test'];

$order = "UPDATE  `habboxli_system`.`users` SET  `points` = points+1 WHERE  `users`.`username` ='$id'";
mysql_query($order);

echo "name is $id";

mysqli_close($con);
echo "Vote posted!";
?> 

此代码应采用在下拉菜单中选择的值并使用它来更新数据库中的特定单元格,我将其签名为名为$ id的变量仅用于测试目的,但值似乎为空白,所以我认为当从原始页面(www.mywebsite.com/#/option.php)导航到myscript.php页面时,下拉菜单没有返回值(www.mywebsite.com/# /myscript.php)。任何有关如何使其工作的帮助将非常感激。

1 个答案:

答案 0 :(得分:0)

正如评论中所说,你正在使用与mysqli函数混合的mysql_query函数。

我通过更改

获得了为我工作的代码
mysql_query($order);

要:

mysqli_query($con, $order);

您还可以通过打印$ _POST数组来调试传递给脚本的内容:

print_r($_POST);