简单的SQL更新语句语法

时间:2017-04-14 13:46:07

标签: php mysql

尝试使用PHP更新MySQL数据库中的记录。我是一个PHP菜鸟,所以我假设我错过了一些明显的东西。

我的新代码(update-task.php) - 它提交并重定向,但记录未更新。

<?php
$data = array(
    'id' => $_POST['id'],
    'TaskName' => $_POST['TaskName'],
    'ClientName' => $_POST['ClientName'],
    'AssignedTo' => $_POST['AssignedTo'],
    'DueDate' => $_POST['DueDate'],
    'TimeLogged' => $_POST['TimeLogged'],
    'Notes' => $_POST['Notes'],
    'Urgent' => $_POST['Urgent']
);

require('mysql.php');
$db = new mysql;
$db->connect('localhost', 'my_user', 'my_pass;', 'my_db');
$db->query("UPDATE `my_table` SET TaskName='" . mysql_real_escape_string($data['TaskName']) . "',
    ClientName='" . mysql_real_escape_string($data['ClientName']) . "',
    AssignedTo='" . mysql_real_escape_string($data['AssignedTo']) . "',
    DueDate='" . mysql_real_escape_string($data['DueDate']) . "',
    TimeLogged='" . mysql_real_escape_string($data['TimeLogged']) . "',
    Notes='" . mysql_real_escape_string($data['Notes']) . "',
    Urgent='" . mysql_real_escape_string($data['Urgent']) . "' WHERE id='" . mysql_real_escape_string($data['id']) . "'
");

header('Location:saved.htm');

我的表单看起来像这样(view-task.php)

<?php
$id = $_GET['id'];
require('mysql2.php');
$db = new mysql;
$db->connect('localhost', 'my_user', 'my_pass;', 'my_db');
$query = $db->query("SELECT * FROM `table_name` WHERE id = '" . $id ."'");
while($result = $db->fetch_array($query)) {
    $data = $result;
}
?>

<form action="update-task.php" method="post" id="form1" name="form1">

    <label>Task Name:</label>
    <input name="TaskName" type="text" id="TaskName" size="30" value="<?php echo $data['TaskName']?>"/>

    <label>Client</label>
    <input name="ClientName" type="text" id="ClientName" size="30" value="<?php echo $data['ClientName']?>"/>

    <label>Assigned To</label>
    <input name="AssignedTo" type="text" id="AssignedTo" size="30" value="<?php echo $data['AssignedTo']?>"/>

    <label>Due Date</label>
    <input name="DueDate" type="text" id="DueDate" size="30" value="<?php echo $data['DueDate']?>"/>

    <label>Time Logged</label>
    <input type="button" value="Start Count" onClick="timedCount()">
    <input type="text" value="<?php echo $data['TimeLogged']?>" id="TimeLogged" name="TimeLogged">
    <input type="button" value="Stop Count" onClick="stopCount()">
    <input type="button" value="Clear Timer" onClick="clearTimer()">

    <label>Notes</label>
    <input name="Notes" type="textarea" id="Notes" size="30" value="<?php echo $data['Notes']?>"/>

    <label>Urgent</label>
    <input name="Urgent" type="text" id="Urgent" size="30" value="<?php echo $data['Urgent']?>"/>

    <input type="submit" value="Update" class="AddButton">

</form>

mysql.php的内容

<?php
class mysql {
    var $querynum = 0;
    var $querylist = '';

    function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect=0) {
        $die = false;

        if($pconnect) {
            @mysql_pconnect($dbhost, $dbuser, $dbpw) or ($die = true);
        } else {
            @mysql_connect($dbhost, $dbuser, $dbpw) or ($die = true);
        }

        if($die) {
            die(mysql_error());
        }

        mysql_select_db($dbname) or die(mysql_error());
    }

    function fetch_array($query, $type=MYSQL_ASSOC) {
        $query = mysql_fetch_array($query, $type);
        return $query;
    } 

    function query($sql) {
        $query = mysql_query($sql) or die(mysql_error());
        $this->querynum++;
        $this->querylist .= "$sql <br />";
        return $query;
    }

    function result($query, $row) {
        $query = mysql_result($query, $row);
        return $query;
    }

    function num_rows($query) {
        $query = mysql_num_rows($query);
        return $query;
    }

    function insert_id() {
        $id = mysql_insert_id();
        return $id;
    }

    function fetch_row($query) {
        $query = mysql_fetch_row($query);
        return $query;
    }
    function affected_rows() {
        $query = mysql_affected_rows();
        return $query;
    }
    function close() {
        mysql_close();
    }
}


?>

2 个答案:

答案 0 :(得分:1)

如果您正在更新数据,那么您有uniq id,从mysql.php发布代码 mysql更新查询的工作原理为"UPDATE my_table SET WHERE id='1'"看起来您有查询错误

   <?php

$data = array(
    'id' => $_POST['id']
    'TaskName' => $_POST['TaskName'],
    'ClientName' => $_POST['ClientName'],
    'AssignedTo' => $_POST['AssignedTo'],
    'DueDate' => $_POST['DueDate'],
    'TimeLogged' => $_POST['TimeLogged'],
    'Notes' => $_POST['Notes'],
    'Urgent' => $_POST['Urgent']
);

require('mysql.php');
$db = new mysql();
$db->connect('localhost', 'my_user', 'my_pass;', 'my_db');
$db->query("UPDATE `my_table` SET TaskName='" . mysql_real_escape_string($data['TaskName']) . "',
    ClientName='" . mysql_real_escape_string($data['ClientName']) . "',
    AssignedTo='" . mysql_real_escape_string($data['AssignedTo']) . "',
    DueDate='" . mysql_real_escape_string($data['DueDate']) . "',
    TimeLogged='" . mysql_real_escape_string($data['TimeLogged']) . "',
    Notes='" . mysql_real_escape_string($data['Notes']) . "',
    Urgent='" . mysql_real_escape_string($data['Urgent']) . "' WHERE id='" . mysql_real_escape_string($data['id']) . "'
)");
header('Location:saved.htm');

?>

形式

<?php
$data['id'] = $_GET['id'];
require('mysql.php');
$db = new mysql();
$db->connect('localhost', 'my_user', 'my_pass;', 'my_db');
$query = $db->query("SELECT * FROM `my_table` WHERE id = '" . $data['id'] ."'");
while($result = $db->fetch_array($query)) {
    $data = array_merge($data, $result);
}
?>


<form action="update-task.php" method="post">

    <label>Task Name:</label>
    <input name="TaskName" type="text" id="TaskName" size="30" value="<?= $data['TaskName']?>"/>

    <label>Client</label>
    <input name="ClientName" type="text" id="ClientName" size="30" value="<?= $data['ClientName']?>"/>

    <label>Assigned To</label>
    <input name="AssignedTo" type="text" id="AssignedTo" size="30" value="<?= $data['AssignedTo']?>"/>

    <label>Due Date</label>
    <input name="DueDate" type="text" id="DueDate" size="30" value="<?= $data['DueDate']?>"/>

    <input name="id" type="hidden" id="id" value="<?= $data['id']?>"/>

    <label>Time Logged</label>
    <input type="button" value="Start Count" onClick="timedCount()">
    <input type="text" value="<?= $data['TimeLogged']?>" id="TimeLogged" name="TimeLogged">
    <input type="button" value="Stop Count" onClick="stopCount()">
    <input type="button" value="Clear Timer" onClick="clearTimer()">

    <label>Notes</label>
    <input name="Notes" type="textarea" id="Notes" size="30" value="<?= $data['Notes']?>"/>

    <label>Urgent</label>
    <input name="Urgent" type="text" id="Urgent" size="30" value="<?= $data['Urgent']?>"/>

    <input type="submit" value="Update" class="AddButton">

</form>

答案 1 :(得分:0)

你的语法错了。您需要使用带有SET子句的WHERE来确定实际更新的内容。

SQL更新语法:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;