Aaron Bangerter
Aaron Bangerter

Reputation: 87

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:

// 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...

Upvotes: 1

Views: 403

Answers (1)

realnumber3012
realnumber3012

Reputation: 1062

Just USE INSERT ON DUPLICATE KEY UPDATE

Upvotes: 1

Related Questions