使用PHP有条件地将记录插入MYSQL表

时间:2016-11-01 14:57:32

标签: php mysql

我有一个表单,用于使用PHP和MYSQL将记录插入表中。有两个字段负责捕获日期时间,我将用它来表示时间跨度。这是我用来插入记录的代码:

    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO song_of_the_day (id, station, song_title, artist, start_date, end_date, start_listenhour, end_listenhour) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString($_POST['station'], "text"),
                       GetSQLValueString($_POST['song_title'], "text"),
                       GetSQLValueString($_POST['artist'], "text"),
                       GetSQLValueString(date("Y-m-d H:i:s", strtotime($_POST['startDay'] . $_POST['start-hour'] . ':' . $_POST['start-minute'] . $_POST['start-meridian'])), "date"),
                       GetSQLValueString(date("Y-m-d H:i:s", strtotime($_POST['endDay'] . $_POST['end-hour'] . ':' . $_POST['end-minute'] . $_POST['end-meridian'])), "date"),
                       GetSQLValueString(date("H:i:s", strtotime($_POST['start_listenhour-hour'] . ':' . $_POST['start_listenhour-minute'] . $_POST['start_listenhour-meridian'])), "date"),
                       GetSQLValueString(date("H:i:s", strtotime($_POST['end_listenhour-hour'] . ':' . $_POST['end_listenhour-minute'] . $_POST['end_listenhour-meridian'])), "date"));

  mysql_select_db($database_mysqlconnect, $mysqlconnect);
  $Result1 = mysql_query($insertSQL, $mysqlconnect) or die(mysql_error());

  $insertGoTo = "manage.php?mess=3";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_mysqlconnect, $mysqlconnect);
$query_Recordset1 = "SELECT * FROM song_of_the_day";
$Recordset1 = mysql_query($query_Recordset1, $mysqlconnect) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

如果我有一个属于该时间跨度的条目,我会认为这是一个重复的条目,并希望表单能够根据它进行INSERT或REPLACE。我想使用以下内容加入到我的应用程序中,但我对下一步该做什么感到困惑:

    $get_startdate = GetSQLValueString(date("Y-m-d H:i:s", strtotime($_POST['startDay'] . $_POST['start-hour'] . ':' . $_POST['start-minute'] . $_POST['start-meridian'])), "date");
    $get_enddate = GetSQLValueString(date("Y-m-d H:i:s", strtotime($_POST['endDay'] . $_POST['end-hour'] . ':' . $_POST['end-minute'] . $_POST['end-meridian'])), "date");
    $get_station = GetSQLValueString($_POST['station'], "text");
    $query = "SELECT * FROM song_of_the_day WHERE station = '$get_station' start_date <= CURDATE() AND end_date >= '$get_enddate'";
    $result = mysql_query($query) or die(mysql_error());
    $num_rows = mysql_num_rows($result);
    $row = mysql_fetch_array( $result );
    if(mysql_num_rows($result) > 0)
    {
    $get_id = $row['id'];
    $warning = 'onClick="Warning! An entry is already scheduled to display during this time span. Do you want to overwrite the entry?"';
    }
    else{
    $warning = null;
    }

如何将后面的代码与原始代码合并以实现表单的条件INSERT / REPLACE?

0 个答案:

没有答案