插入到已存在数据的表中

时间:2014-04-29 06:37:32

标签: php mysql

我有一个应用程序,我必须将数据插入到mysql数据库中(这部分工作正常)。

现在我必须将数据插入到已存在一些数据的表中。

第一部分,我插入row 1row2row3(其余为空)

然后我必须插入row4row5row6

但我只是给了我数据库错误。无法添加帖子!

这是我的插入代码:

//load and connect to MySQL database stuff
require("config.inc.php");


if (!empty($_POST)) {
//initial query
$query = "INSERT INTO under_etage (lys_u_s, skilt_u_s, door_u_s ) VALUES   (:lys_u_s, :skilt_u_s, :door_u_s ) where etData ='29.04.2014' ";


//Update query
$query_params = array(

    ':lys_u_s' => $_POST['lys_u_s'],
    ':skilt_u_s' => $_POST['skilt_u_s'],
    ':door_u_s' => $_POST['door_u_s']

);

//execute query
try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
    // For testing, you could use a die and message. 
    //die("Failed to run query: " . $ex->getMessage());

    //or just use this use this one:
    $response["success"] = 0;
    $response["message"] = "Database Error. Couldn't add post!";
    die(json_encode($response));
}

$response["success"] = 1;
$response["message"] = "Post Successfully Added!";
echo json_encode($response);

}

我错过了什么?

我尝试过UPDATE,但我必须更新正确的行,所以我想更新etDate(date)是一样的。

if (!empty($_POST)) {

  $lys_u_s = $_POST['lys_u_s'];
 $skilt_u_s = $_POST['skilt_u_s'];
 $door_u_s = $_POST['door_u_s'];
 $etDate = $_POST['etDate'];


$query = "UPDATE under_etage SET lys_u_s = '$lys_u_s', skilt_u_s = '$skilt_u_s',    door_u_s = '$door_u_s' where etDate ='$etDate' ";

它正在工作:)

1 个答案:

答案 0 :(得分:0)

在插入查询中将lys_u_s更改为:lys_u_s

$query = "INSERT INTO under_etage (lys_u_s, skilt_u_s, door_u_s ) VALUES (:lys_u_s, :skilt_u_s, :door_u_s ) ";

我认为您需要使用更新语句

修改

您的更新语句看起来不对

UPDATE under_etage SET lys_u_s=:lys_u_s, skilt_u_s=:skilt_u_s, door_u_s=:door_u_s where etData ='29.04.2014'