【问题标题】:updating mysql records based on option selection根据选项选择更新 mysql 记录
【发布时间】:2011-11-07 11:58:57
【问题描述】:

您好,我无法弄清楚我的代码有什么问题。还没有太多的php经验。有人可以告诉我我做错了什么吗?

这是我的代码:

<?php
   include 'mysql_connect.php';
if (!isset($_POST['submit'])) {
    $fuelQuery2 = sprintf("UPDATE fuel_price SET `Price` = '%s' WHERE FuelType = '%s' LIMIT 1",
                        mysql_real_escape_string($_POST['inputPrice']),
                        mysql_real_escape_string($_POST['fueltype']));

    $Result = mysql_query($fuelQuery2);
    if($Result){
        echo 'Price has been updated!';
    } else{
        echo 'Failed to update price!';
    }
} else{
    echo 'No form submitted';
}


?>

<h1>Update Oil Price</h1>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Oil Price:<input name="inputPrice" type="text" value=""/>

Product
<select name="fueltype">
<option value="Oil">Kero</option>
<option value="Diesel">Diesel</option>
<option value="RedDiesel">Red Diesel</option>
</select>

<input type="submit" name="submit" value="Modify" />
</form>

【问题讨论】:

  • 还有什么问题?你做错了一切。看看this question,刚刚解释了这种功能应该如何实现
  • 在执行之前有没有试过echo $fuelQuery2

标签: php mysql


【解决方案1】:

其实很简单,改一下

if (!isset($_POST['submit'])) {

if(isset($_POST['submit'])) { //Only execute the query when the form is submitted

您的原始代码告诉 PHP 在未提交表单时执行查询(请注意我删除了!),而不是在提交时。您收到的通知告诉您,您为查询获取的 $_POST 变量不存在(因为代码在提交表单之前运行)。

另外,请查看PDOmysql_ 系列函数不再是与数据库层交互的首选方法。

【讨论】:

  • 谁知道会这么简单! :D
【解决方案2】:

PHP

mysql_connect.php,请确保您致电mysql_connect()mysql_select_db()

然后您可以将代码调整为以下内容:

<?php
    include 'mysql_connect.php';
    if ('POST' == $_SERVER['REQUEST_METHOD'] and
        isset($_POST['fuel_type']) and
        isset($_POST['oil_price'])) {
        $fuel_type = mysql_real_escape_string($_POST['fuel_type']);
        $oil_price = mysql_real_escape_string($_POST['oil_price']);

        $SQL = "UPDATE `fuel_price`
                SET `Price` = '$oil_price'
                WHERE `FuelType` = '$fuel_type'";

        if(mysql_query($SQL)) {
            echo 'Price updated.';
        } else {
            echo 'Failed to update.';
        }
    }
?>

HTML 表单

action 中不需要 PHP_SELF,只需将其留空即可提交到同一页面。

<form action="" method="post">
    <label for="oil_price">Oil Price</label>
    <input name="oil_price" id="oil_price" type="text" value="" />

    <label for="fuel_type">Product</label>
    <select name="fuel_type" id="fuel_type">
        <option value="Oil">Kerosene</option>
        <option value="Diesel">Diesel</option>
        <option value="RedDiesel">Red Diesel</option>
    </select>

    <input type="submit" name="submit" value="Modify" />
</form>

【讨论】:

  • 我猜你的意思是FuelType` = `$fuel_type而不是price
  • 获取未定义的索引:fueltype
  • @JensMühlenhoff 正确。接得好。谢谢。
  • @user921334 我从您的代码中复制了它!这意味着您的表单字段被命名为fueltype 以外的其他名称,但是没有看到您的表单,我不知道应该叫什么。
  • @user921334 我已经用 HTML 和 PHP 更新了我的答案,我认为你需要让它工作。
猜你喜欢
  • 1970-01-01
  • 2013-01-10
  • 1970-01-01
  • 2014-02-15
  • 1970-01-01
  • 1970-01-01
  • 2014-02-25
  • 2023-04-09
  • 2019-10-01
相关资源
最近更新 更多