Page 1 of 1
Detecting Rows, if not there.. create it.
Posted: Thu Feb 02, 2006 3:25 pm
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.
Posted: Thu Feb 02, 2006 3:39 pm
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>
Posted: Thu Feb 02, 2006 4:53 pm
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.
Posted: Thu Feb 02, 2006 5:18 pm
by josh
check the syntax for REPLACE INTO TABLE