INSERT INTO ...在DUPLICATE KEY UPDATE sprintf混乱

时间:2013-01-09 04:10:29

标签: php mysql

我有一个页面,有多个简单的输入供用户选择信息更新选项。用户可能尚未定义更新配置文件,因此我尝试使用“INSERT INTO ... ON DUPLICATE KEY UPDATE”语句。我也尝试使用sprintf来避免可能的SQL注入问题。我的问题是我无法弄清楚如何编写SQL语句。这是代码的相关部分。

$updateSQL = sprintf("INSERT INTO reminders_cfg 
                    (id, day_of, day_of_advance, day_prior, default_time, week_prior, 2_week_prior, 3_week_prior, 4_week_prior, 5_week_prior, day_after, 2_week_after, 50_day_after) 
                    VALUES %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s
                    ON DUPLICATE KEY UPDATE ",
                   GetSQLValueString($jeweler, "text"),
                   GetSQLValueString($_POST['DayOf'], "text"),
                   GetSQLValueString($_POST['Advance'], "text"),
                   GetSQLValueString($_POST['DayPrior'], "text"),
                   GetSQLValueString($_POST['Time'], "text"),
                   GetSQLValueString($_POST['WeekPrior'], "text"),
                   GetSQLValueString($_POST['2WeeksPrior'], "text"),
                   GetSQLValueString($_POST['3WeeksPrior'], "text"),
                   GetSQLValueString($_POST['4WeeksPrior'], "text"),
                   GetSQLValueString($_POST['5WeeksPrior'], "text"),
                   GetSQLValueString($_POST['DayAfter'], "text"),
                   GetSQLValueString($_POST['2WeeksAfter'], "text"),
                   GetSQLValueString($_POST['50DaysAfter'], "text"));

这是在Dreamweaver中完成的PHP页面中。这就是GetSQLValueString函数的用武之地。在实际的'ON DUPLICATE KEY UPDATE'部分之后,我真的无法弄清楚要做什么,而不必再次复制所有的变量和占位符。我希望这是有道理的。

更新: 好。这是我最近的尝试仍未奏效。

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
    $stmt = $mysqli->prepare("INSERT INTO reminders_cfg 
                    (id, day_of, day_of_advance, day_prior, default_time, week_prior, 2_week_prior, 3_week_prior, 4_week_prior, 5_week_prior, day_after, 2_week_after, 50_day_after) 
                    VALUES :id, :day_of, :day_of_advance, :day_prior, :default_time, :week_prior, :2_week_prior, :3_week_prior, :4_week_prior, :5_week_prior, :day_after, :2_week_after, :50_day_after
                    ON DUPLICATE KEY UPDATE day_of = :day_of, day_of_advance = :day_of_advance, day_prior = :day_prior, default_time = :default_time, week_prior = :Week_prior, 2_week_prior = :2_week_prior, 3_week_prior = :3_week_prior, 4_week_prior = :4_week_prior, 5_week_prior = :5_week_prior, day_after = :day_after, 2_week_after = :2_week_after, 50_day_after = :50_day_after")
                    or die($mysqli->error);
    $stmt->bindParam(':id', $jeweler, PDO::PARAM_INT);      
    $stmt->bindParam(':day_of', $_POST['DayOf'], PDO::PARAM_STR, 6);
    $stmt->bindParam(':day_of_advance', $_POST['Advance'], PDO::PARAM_INT);
    $stmt->bindParam(':day_prior', $_POST['DayPrior'], PDO::PARAM_STR, 6);
    $stmt->bindParam(':default_time', $_POST['Time'], PDO::PARAM_STR, 9);
    $stmt->bindParam(':week_prior', $_POST['WeekPrior'], PDO::PARAM_STR, 6);
    $stmt->bindParam(':2_week_prior', $_POST['2WeeksPrior'], PDO::PARAM_STR, 6);
    $stmt->bindParam(':3_week_prior', $_POST['3WeeksPrior'], PDO::PARAM_STR, 6);
    $stmt->bindParam(':4_week_prior', $_POST['4WeeksPrior'], PDO::PARAM_STR, 6);
    $stmt->bindParam(':5_week_prior', $_POST['5WeeksPrior'], PDO::PARAM_STR, 6);
    $stmt->bindParam(':day_after', $_POST['DayAfter'], PDO::PARAM_STR, 6);
    $stmt->bindParam(':2_week_after', $_POST['2WeeksAfter'], PDO::PARAM_STR, 6);
    $stmt->bindParam(':50_day_after', $_POST['50DaysAfter'], PDO::PARAM_STR, 6);

    $stmt->execute() or die("Insert query error: " . mysql_error());
    $stmt->close(); 
}

这会产生SQL语法错误,因为我是新手'INSERT INTO ... ON DUPLICATE KEY UPDATE'我有点卡住了。这是错误...

您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以便在':id,:day_of,:day_of_advance,:day_prior,:default_time,:week_prior,:2_week_p'第3行附近使用正确的语法

2 个答案:

答案 0 :(得分:1)

ON DUPLICATE KEY UPDATE之后,您只需输入要更新的值,并保留原封不变的值。 e.g

纯mysql示例

insert into my_table(name, lastSeen, salary)
              values("Some Name", now(), 25334)
on duplicate key update salary = 25334, lastSeen = now()

name仅为此示例primary key 现在假设一个记录已经存在于具有相同名称的数据库中,该条目将通过更改工资和lastSeen来更新。如果不是,则将使用提供的值创建条目。在声明的第一部分

insert into my_table(name, lastSeen, salary) values("Some Name", now(), 25334)

希望有所帮助。

<强> ADITION 如果您在问题中添加了要在重复密钥上更新的字段,那将是一件好事,因此我们可以使用您的代码回答您的问题。

修改 好的, 我知道这不是有史以来最好的格式化代码,但也许这有帮助吗? (也许那里有语法错误,但如果这是你想要的,你可能会得到这个想法)

 $updateSQL = sprintf("INSERT INTO reminders_cfg 
                (id, day_of, day_of_advance, day_prior, default_time, 
                 week_prior, 2_week_prior, 3_week_prior, 4_week_prior,
                 5_week_prior, day_after, 2_week_after, 50_day_after) 
                VALUES %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s
                ON DUPLICATE KEY UPDATE id = %s, day_of = %s, day_of_advance = %s, 
                     day_prior = %s, default_time = %s, week_prior = %s,
                     2_week_prior = %s, 3_week_prior = %s, 4_week_prior = %s,
                     5_week_prior = %s, day_after = %s, 2_week_after = %s, 
                     50_day_after = %s ",
               GetSQLValueString($jeweler, "text"),
               GetSQLValueString($_POST['DayOf'], "text"),
               GetSQLValueString($_POST['Advance'], "text"),
               GetSQLValueString($_POST['DayPrior'], "text"),
               GetSQLValueString($_POST['Time'], "text"),
               GetSQLValueString($_POST['WeekPrior'], "text"),
               GetSQLValueString($_POST['2WeeksPrior'], "text"),
               GetSQLValueString($_POST['3WeeksPrior'], "text"),
               GetSQLValueString($_POST['4WeeksPrior'], "text"),
               GetSQLValueString($_POST['5WeeksPrior'], "text"),
               GetSQLValueString($_POST['DayAfter'], "text"),
               GetSQLValueString($_POST['2WeeksAfter'], "text"),
               GetSQLValueString($_POST['50DaysAfter'], "text"),
               GetSQLValueString($jeweler, "text"),
               GetSQLValueString($_POST['DayOf'], "text"),
               GetSQLValueString($_POST['Advance'], "text"),
               GetSQLValueString($_POST['DayPrior'], "text"),
               GetSQLValueString($_POST['Time'], "text"),
               GetSQLValueString($_POST['WeekPrior'], "text"),
               GetSQLValueString($_POST['2WeeksPrior'], "text"),
               GetSQLValueString($_POST['3WeeksPrior'], "text"),
               GetSQLValueString($_POST['4WeeksPrior'], "text"),
               GetSQLValueString($_POST['5WeeksPrior'], "text"),
               GetSQLValueString($_POST['DayAfter'], "text"),
               GetSQLValueString($_POST['2WeeksAfter'], "text"),
               GetSQLValueString($_POST['50DaysAfter'], "text"));

答案 1 :(得分:-1)

如果有一个可能的碰撞密钥,您应该只使用ON DUPLICATE KEY UPDATE。在这种情况下,我猜它是ID。

您最好定义触发器。像这样:

CREATE TRIGGER `reminders_cfg_duplicates` BEFORE INSERT ON `reminders_cfg` FOR EACH ROW
  DELETE FROM `reminders_cfg` WHERE `id`=new.`id`;

运行一次,例如从phpMyAdmin的控制面板运行。然后,数据库将自动删除任何导致重复的行,从而允许插入新行而无需复制更新中的所有值。