0
$fname = addslashes($fname);
$lname = addslashes($lname);
$dob = addslashes($dob);
$email = $_POST['email'];
$sql = 
 "INSERT INTO subscriber 
    (fname, lname, dob) 
  VALUES
    ('".$fname."', '".$lname."', '".$dob."') 
  WHERE email='".$email."'";

$register = mysql_query($sql) or die("insertion error");

SQL クエリ「挿入エラー」でエラーが発生しています。クエリは、WHERE ステートメントを削除した後、DB にデータを挿入しています。エラーは何ですか。

4

3 に答える 3

9

You can't use where in an insert statement. You might be thinking of an update instead?

$sql = "update subscriber set fname='".$fname."', lname = '".$lname."', dob = '".$dob."'  WHERE email='".$email."'";

If your email is a unique value, you can also combine an insert with an update like this:

insert into 
    subscriber (fname, lname, dob, email) 
    values ('".$fname."', '".$lname."', '".$dob."', '".$email."') 
        on duplicate key update set fname='".$fname."', lname='".$lname."', dob='".$dob."'

This second syntax will insert a row if there isn't one with a matching email (again, this has to be set to a unique constraint on the table) and if there is one there already, it will update the data to the values you passed it.

于 2012-09-12T07:24:55.727 に答える
5

Basically INSERT statement cannot have where. The only time INSERT statement can have where is when using INSERT INTO...SELECT is used.

The only syntax for select statement are

INSERT INTO TableName VALUES (val1, val2, ..., colN)
and
INSERT INTO TableName (col1, col2) VALUES (val1, val2)

The other one is the

INSERT INTO tableName (col1, col2)
SELECT col1, col2
FROM tableX
WHERE ....

basically what it does is all the records that were selected will be inserted on another table (can be the same table also).

One more thing, Use PDO or MYSQLI

Example of using PDO extension:

<?php

    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    $stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
    $stmt->bindParam(1, $name);
    $stmt->bindParam(2, $value);

    // insert one row
    $name = 'one';
    $value = 1;
    $stmt->execute();

?>

this will allow you to insert records with single quotes.

于 2012-09-12T07:25:19.040 に答える
1

おっとっと !!!!WHEREステートメントで句を使用することはできませんINSERT..特定の行をターゲットにしている場合は、使用してくださいUPDATE

$sql = "Update subscriber set fname = '".$fname."' , lname = '".$lname."' , dob = '".$dob."' 
WHERE email='".$email."'";
$register = mysql_query($sql) or die("insertion error");
于 2012-09-12T07:32:36.473 に答える