PHP - 在插入之前检查数据库的值

时间:2014-01-18 18:31:45

标签: php ms-access-2010

我正在编写一个脚本,从文件中提取数据,根据“msubject”字段的字符数进行检查,然后将其放在屏幕上。之后,我试图将屏幕上的值插入数据库,但在此之前,我需要检查该行是否已存在。我遇到的问题是检查几个值,如果确实存在,则不将它放在数据库中。目前,如果为true或false,则数据仍被放入数据库中。我没有使用mysql,我使用msaccess作为我的数据库。不幸的是,这是我唯一可以使用的数据库。以下是我正在使用的代码。你知道为什么如果真或假,数据被放入db?

<?php
$dbco = realpath('C:\xampp\htdocs\db.accdb');
$conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$dbco",'','') or die ("Unable to connect to server");

$query = "SELECT * FROM CriticalMeetings";
$result = odbc_exec ($conn, $query);
$filename = "test.tsv";
$today = date('Y-m-d');
$file = file($filename);

foreach($file as $line)
{
    list($start_datetime, $end_datetime, $organizer, $msubject)= explode("\t", $line);
    $count = mb_strlen($msubject);
    if ($count > 79)
    {

    //Start Date-Time split
    $StartDatetime = new DateTime($start_datetime);
    $start_date = $StartDatetime->format('Y-m-d');
    $start_time = $StartDatetime->format('H:i:s');

    //End Date-Time split
    $EndDatetime = new DateTime($end_datetime);
    $End_date = $EndDatetime->format('Y-m-d');
    $End_time = $EndDatetime->format('H:i:s');

        if ($start_date = $today)
        {
        echo "<tr>";
        echo "<td>$start_date</td>";
        echo "<td>$start_time</td>";
        echo "<td>$organizer</td>";
        echo "<td>$msubject</td>";
        echo "</tr>";
        }
    while($row = odbc_fetch_array($result)) 
    {
        $db_start_date = ($row["StartDate"]);
        $db_start_time = ($row["StartTime"]);
        $db_organizer = ($row["Organizer"]);
        $db_MeetingSubject = ($row["MeetingSubject"]);
        echo "<br />";
        echo "$db_organizer";

        if ($organizer == $db_organizer)
        {
        //Replace apostrophe    
        $msubject = str_replace( "'", "&#39;", $msubject);
        //Insert values into db
        $input="INSERT INTO CriticalMeetings (StartDate, StartTime, Organizer, MeetingSubject)
        VALUES ('$start_date', '$start_time', '$organizer', '$msubject')";
        $rs=odbc_exec($conn,$input);
        if (!$rs)
            {
            die('Error: ' . odbc_error());
            }
        }
        else
        {
        echo "data not in db";
        }   

    } 

    }
}
?>

0 个答案:

没有答案