mysql update or create row

admin

Administrator
Staff member
its simple.

i have a table with a code. i check if the code exists (with an associated url). that bits fine. then i check in atable with that code name, if an entry for today exists (using date(dmY)). if its exists, increment the hit field else creat the row. here's the code. please see if you can see anything glaringly wrong! i am going nuts!! Cheers! and now for the code :

function checkredirectcode ($d) {

$quer = "SELECT * FROM " . $GLOBALS['table'] . " WHERE code = '$d'";
$result = mysql_query($quer);
if (mysql_num_rows($result) == 1) {
$row = mysql_fetch_array($result);
$theurl = "http://" . $row["url"] . "/";
//lets update or add the hit stat for today
$sql = "SELECT * FROM '" . $d . "' WHERE day = 12092001";
$result = mysql_query ($sql);
if (mysql_num_rows($sql) == 1) { //stats for today already exist - update the hit count
$row = mysql_fetch_array($result);
$thehit = $row["hits"] + 1;
$quer = "UPDATE " . $GLOBALS['d'] . " set hits='" . $thehit . "' where day='" . $GLOBALS['thedate'] . "'";
mysql_query ($quer);
}
else { //stats for today do not exist yet - add row
$quer = "INSERT INTO " . $GLOBALS['d'] . " (day, hits) VALUES (" . $GLOBALS['thedate'] . ", 0)";
mysql_query ($quer) or die("Could not create today's stats.");
echo "here";
}
print Header("Location: $theurl"); }
else { print Header("Location: unknown.html"); }

}
 
Back
Top