Compare and inserting with php
| From: | Miller, Terion | Date: | Tue, 30 Jun 2009 14:42:01 +0000 |
| Subject: | Compare and inserting with php | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-294742@lists.php.net to get a copy of this message | ||
Okay, what I am trying to do is this:
I have a db full of inspections, daily I need to run a script that will grab
the posting of inspections, check them against the db to make sure they
aren't duplicates and then insert all the new entries..... Not sure if my
logic of going about it this way is right, or if I'm off track and need to
be doing something different please advise...so far this is how I am writing
it.....and it seems redundant ... Shouldn't there be a way to say (in plain
speak) if there are no records with the name== insert this one, if there is
one with the name then check intype if none ..insert... Etc
if (!empty($Go)) {
$query = "SELECT * FROM restaurants WHERE name LIKE '$ucName'";
$result = mysql_query ($query);
$row = mysql_fetch_object ($result);
If (mysql_num_rows($result) == 0) {
$sql = "INSERT INTO
restaurants (name, address, inDate, inType, notes,
critical, cviolations, noncritical) VALUES (";
$sql .= " '$ucName', '$ucAddress', '$inDate',
'$inType', '$notes',
'$critical', '$cleanViolations', '$noncritical')";
$result = mysql_query($sql) or die(mysql_error());
} else {
$query = "SELECT * FROM restaurants WHERE inType LIKE '$inType' AND name
LIKE '$ucName' ";
$result = mysql_query ($query);
$row = mysql_fetch_object ($result);
If (mysql_num_rows($result) == 0) {
$sql = "INSERT INTO restaurants (name, address, inDate, inType, notes,
critical, cviolations, noncritical) VALUES (";
$sql .= " '$ucName', '$ucAddress', '$inDate',
'$inType',
'$notes', '$critical', '$cleanViolations',
'$noncritical')";
$result = mysql_query($sql) or die(mysql_error());
} else {
$query = "SELECT * FROM restaurants WHERE inDate LIKE '$inDate' AND name
LIKE '$ucName' ";
$result = mysql_query ($query);
$row = mysql_fetch_object ($result);
If (mysql_num_rows($result) == 0) {
$sql = "INSERT INTO restaurants (name, address, inDate, inType, notes,
critical, cviolations, noncritical) VALUES (";
$sql .= " '$ucName', '$ucAddress', '$inDate',
'$inType',
'$notes', '$critical', '$cleanViolations',
'$noncritical')";
$result = mysql_query($sql) or die(mysql_error());
}
}