简体   繁体   English

检查数据库表是否存在行,如果不存在则创建它

[英]Check database table if row exists, then create it if not

I am trying to check the database if data for a specific date exists. 我正在尝试检查数据库是否存在特定日期的数据。 If it does not exist, then a new row needs to be inserted into the database for that date. 如果不存在,则需要将该日期的新行插入数据库。 Here's my code so far in php/sql (after db login info), but I can't get it to work: 这是我到目前为止在php / sql中的代码(在数据库登录信息之后),但是我无法使它正常工作:

// gets two data points from form submission
$tablename = $_GET['tablename'];
$date = $_GET['date'];

//Fetching from your database table.
$query = "IF NOT EXISTS (SELECT * FROM $tablename WHERE date = $date) BEGIN Insert into $tablename (date, var1, var2) VALUES ('$date', '', ''); END"
$result = mysql_query($query);

Please HELP... 请帮忙...

只需在重复的密钥更新上使用INSERT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM