【问题标题】:I only want to update one MySQL row with PHP我只想用 PHP 更新一个 MySQL 行
【发布时间】:2016-10-06 19:37:38
【问题描述】:

我正在处理一个页面,我需要能够使用 PHP 从我的 MySQL 数据库中更新单个记录。有人可以帮帮我吗?当我尝试更新一条记录时,我的所有其他记录都会更新。

    <form action="" method="post">
    <input type="hidden" name="id" value="<?php echo $id; ?>"/>
    <div>
    <p><strong>ID:</strong> <?php echo $id; ?></p>
    <strong>Name: *</strong> <input class="form-control" type="text" name="name" value="<?php echo $name; ?>" /><br/>
    <strong>Month: *</strong> <input class="form-control" type="text" name="month" value="<?php echo $month; ?>" /><br/>
    <strong>event1: *</strong> <input class="form-control" type="text" name="event1" value="<?php echo $event1; ?>" /><br/>
    <strong>event2: </strong> <input class="form-control" type="text" name="event2" value="<?php echo $event2; ?>" /><br/>
    <strong>event3: </strong> <input class="form-control" type="text" name="event3" value="<?php echo $event3; ?>" /><br/>
    <strong>event4: </strong> <input class="form-control" type="text" name="event4" value="<?php echo $event4; ?>" /><br/>
    <strong>timesub: </strong> <input class="form-control" type="text" name="timesub" value="<?php echo $timesub; ?>" readonly /><br/>
    <p>* Required</p>
    <input type="submit" name="submit" value="Submit" class="btn btn-info">
    <input type=reset name="reset" value="Reset" class="btn btn-danger">
    </div>
    </form>

这是我的表格,然后我用这段代码跟随它:

    <?php
    }
    include('db_connect.php');

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

    // confirm that the 'id' value is a valid integer before getting the form data
    if (is_numeric($_POST['id'])) {

    $id = $_POST['id'];
    $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
    $month = mysql_real_escape_string(htmlspecialchars($_POST['month']));
    $event1 = mysql_real_escape_string(htmlspecialchars($_POST['event1']));
    $event2 = mysql_real_escape_string(htmlspecialchars($_POST['event2']));
    $event3 = mysql_real_escape_string(htmlspecialchars($_POST['event3']));
    $event4 = mysql_real_escape_string(htmlspecialchars($_POST['event4']));
    $timesub = mysql_real_escape_string(htmlspecialchars($_POST['timesub']));

    // check thatfields are filled in
    if ($name == '' || $month == '' || $event1 == '' || $timesub == ''){

    // generate error message
    $error = 'ERROR: Please fill in all required fields!';

    //error, display form
    renderForm($id, $name, $month, $event1, $event2, $event3, $event4, $timesub, $error);
    } else {
    // save the data to the database
    mysql_query("UPDATE announcement SET name='$name', month='$month', event1='$event1', event2='$event2', event3='$event3', event4='$event4', timesub='$timesub'")
    or die(mysql_error());

    // once saved, redirect back to the view page
    header("Location: view.php");
    }
    } else {
    // if the 'id' isn't valid, display an error
    echo 'Error!';
    }
    } else
    // if the form hasn't been submitted, get the data from the db and display the form
    {// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)

    if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) {
    // query db
    $id = $_GET['id'];
    $result = mysql_query("SELECT * FROM announcement WHERE id=$id")
    or die(mysql_error());

    $row = mysql_fetch_array($result);

    // check that the 'id' matches up with a row in the databse
    if($row) {

    // get data from db
    $name = $row['name'];
    $month = $row['month'];
    $event1 = $row['event1'];
    $event2 = $row['event2'];
    $event3 = $row['event3'];
    $event4 = $row['event4'];
    $timesub = $row['timesub'];

    // show form
    renderForm($id, $name, $month, $event1, $event2, $event3, $event4, $timesub, '');

    } else {// if no match, display result

    echo "No results!";
    }
    } else {// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error

    echo 'Error!';

    }
    }
    ?>

我的代码来自:http://www.killersites.com/community/index.php?/topic/1969-basic-php-system-vieweditdeleteadd-records/

好文章!

提前感谢您的帮助。

【问题讨论】:

  • 您需要学习基本的 SQL,特别是 WHERE 子句。请注意,您很容易受到sql injection attacks 的攻击。
  • 因为您需要在查询中添加 where 子句,例如 WHERE id = $id 。另请注意 mysql_* 在 PHP 7 中已弃用并关闭。使用 mysqli_* 或 PDO
  • 谢谢马克 B;我刚刚开始使用 SQL,我的 SQL 注入漏洞在哪里?
  • stop using mysql_* functions These extensions 已在 PHP 7 中删除。了解preparedPDO 和 @987654328 语句@ 并考虑使用 PDO,it's really pretty easy

标签: php html mysql sql forms


【解决方案1】:

首先停止使用 mysql_*,它在 PHP 7 中已被弃用和关闭。您可以使用 mysqli_*PDO

您的查询有什么问题:

这非常重要,如果您在 UPDATE STATEMENT 中不使用 WHERE CLAUSE,它将更新所有行。

您必须在查询末尾添加WHERE CLAUSE,例如:

WHERE id = '$id'

另外需要注意的是,你的代码是对 SQL INJECTION 开放的,你必须用 SQL INJECTION 来防止,你可以了解prepared statement。这篇文章将帮助你理解:How can I prevent SQL injection in PHP?


*如果您想了解mysqli_* 的工作原理,本文档将为您提供帮助:http://php.net/manual/en/book.mysqli.php

如果你想在 PDO 上工作,可以按照本手册操作:http://php.net/manual/en/book.pdo.php

【讨论】:

  • 如何将当前代码更改为 mysqli_* ?
  • @SamFonseca:查看我在底部分享的参考链接
  • 谢谢,我正在检查它们...我对 SQL 很陌生;我只使用了 UPDATE、SELECT 和 INSERT INTO 等基本命令
  • @SamFonseca 如果答案对您有用,请接受。会帮助别人
【解决方案2】:

仔细查看您的查询:

mysql_query("UPDATE announcement SET name='$name', month='$month', event1='$event1', event2='$event2', event3='$event3', event4='$event4', timesub='$timesub'")

您的查询缺少WHERE 子句。如果没有WHERE 子句,您的查询将更新表announcement 中的所有行。您需要添加如下内容:

WHERE id='$id'

这会将更新限制在您要更新的行。

【讨论】:

  • 很高兴我能帮上忙。
猜你喜欢
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 2021-11-22
相关资源
最近更新 更多