I'm not sure how I would write a PHP statement that says "if a record with this value exists, then update it, if it doesn't, then insert it."
Can someone give me a hand? Thanks!
If Record Exists, Update It. If Not, Insert It.
Moderator: General Moderators
-
WithHisStripes
- Forum Contributor
- Posts: 131
- Joined: Tue Sep 13, 2005 7:48 pm
-
WithHisStripes
- Forum Contributor
- Posts: 131
- Joined: Tue Sep 13, 2005 7:48 pm
Re: If Record Exists, Update It. If Not, Insert It.
Code: Select all
$sql = "SELECT COUNT(*) AS total FROM tableName WHERE recordName` = '$record'";
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
$total = $row['total'];
if ($total == 1) {
} else {
}
Last edited by Benjamin on Mon May 11, 2009 6:00 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Reason: Changed code type from text to php.
Re: If Record Exists, Update It. If Not, Insert It.
Look up the REPLACE() statement. This can be done in MySQL without necessarily needing any PHP logic.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.