Detecting Rows, if not there.. create it.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

Detecting Rows, if not there.. create it.

Post by alix »

What im trying to do is have the script see if the row containing a set of information is there... if there edit what needs changed.. if not there add it to the db in new row.

what would be the best way to get this done?

Does that make sense?
Any help would be awesome, thanks.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

give examples...anyway i was thinking whether you need something like this..

Code: Select all

<div>Edit / Insert</div>
<div>
<?php
$query = "select * from `some_table` where match (`some_field`) against ('som_value' in boolean mode)";
if (is_resource($result = mysql_query($query)) === TRUE){
	if (mysql_num_rows($query) > 0){
?>
	<div>Display information for edit</div>
<?php		
	}else{
		$query = "insert into `some_table` (`some_field`) values (some_value)";
		mysql_query($query);
		if (mysql_affected_rows() > 0){
?>
			<div>Successfully inserted!!!</div>

<?php
		}
	}
}
?>
</div>
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

Post by alix »

this is what im trying... im not getting any errors, however, its not updating or creating any rows.

Code: Select all

<?

$HTTP_POST_VARS;
	include ("db.php");
// find out what needs to be done
if ($update == "instant_contact"){
	//update sql database
	$query = "
			UPDATE `table1` SET `email` = '$new_email', `capture` = '$new_capture', `redirect` = '$new_redirect', `pass` = '$new_pass' WHERE `user` = '$username_1' ;			
	";
	$result = mysql_query($query) or die("error: " . mysql_error());

	//this is where i need help compairing rows
	// update texter infomation
} elseif ($update == "texter"){
	$query = "select * from `user_texter` where match (`user`) against ('$username_1')";
	if (is_resource($result = mysql_query($query)) === TRUE){
		if (mysql_num_rows($query) > 0){
		$result = mysql_query("SELECT * FROM texter_providers WHERE `provider_name` = \"$provider_1\" ");
			while($myrow = mysql_fetch_assoc($result))
			{
				$provider_email= $myrow['send_email'];
			}
			
			$query = "
			UPDATE `users_texter` SET `provider` = '$provider_1', `number` = '$number', `provider_email` = '$provider_email', `text_on` = '$text_on_off' WHERE `loginname` = '$username_1' ;			
		";
			$result = mysql_query($query) or die("error: " . mysql_error());
		}else{
			$query = "insert into `texter_users` (`user`) values ('$username_1')";
			mysql_query($query)or die("error: " . mysql_error());
			if (mysql_affected_rows() > 0){
?> 
            <div>Successfully inserted!!!</div> 

<?php 
			}
		}
	}
} else {
	echo "if u get this message; take ur head out of your ass ";
};
?>

<html> 
<META 
http-equiv="refresh" content="1;URL=http://www.mydomain.com/contact/script.php">

See, i want it to check if there is a row with a person's username, if the username is there, simply update the info into the DB, if not, create the row with the persons user and the other information.
I didnt fully understand the query with "MATCH" in it. I've never used that so i dont know if i messed up that query and thats why its not working. -Thanks.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

check the syntax for REPLACE INTO TABLE
Post Reply