nhay59
asked on
find a column value in mysql and update using php
Hi,
I have a simply table with three columns as follows,
id, tag, count
I know how to update the column 'count' using php, but I need to first check whether the value exists in the column. If it exists the value is updated, if not a new value is inserted. However, how would I check first that the value exists, and then use either the insert or update query based on whether the value exists?
Any help appreciated.
Thanks
I have a simply table with three columns as follows,
id, tag, count
I know how to update the column 'count' using php, but I need to first check whether the value exists in the column. If it exists the value is updated, if not a new value is inserted. However, how would I check first that the value exists, and then use either the insert or update query based on whether the value exists?
Any help appreciated.
Thanks
You could request the mysql_affected_rows() function just after your update. If 0 rows are updated, then you need to update the row. I think this is more efficient then calling to see if the record exist before updating.
$myvar = 1;
$sql = "select * from mytable where count = $myvar;"
$rs = mysql_query($sql);
if (mysql_num_rows($rs) >= 1{
$does_exist = true;
}
if ($does_exist == true) {
$sql = "update .....";
} else {
$sql = "insert into .....";
}
$sql = "select * from mytable where count = $myvar;"
$rs = mysql_query($sql);
if (mysql_num_rows($rs) >= 1{
$does_exist = true;
}
if ($does_exist == true) {
$sql = "update .....";
} else {
$sql = "insert into .....";
}
nplib,
The implementation you provided is functional, but can be improved.
When you wánt to do a select, then please add limit 0,1, or change the query into a count(somefield) query.
-r-
The implementation you provided is functional, but can be improved.
When you wánt to do a select, then please add limit 0,1, or change the query into a count(somefield) query.
-r-
thank you.
ASKER
Hi,
Thanks to both of you for the help. I'm using this with a baisc textfield and submit button, but I get an error with the included code. The error is as follows,
Parse error: syntax error, unexpected T_VARIABLE in.....
and this refers to the line,
$rs = mysql_query($sql);
Any ideas why this is happening?
Thanks for the help
Thanks to both of you for the help. I'm using this with a baisc textfield and submit button, but I get an error with the included code. The error is as follows,
Parse error: syntax error, unexpected T_VARIABLE in.....
and this refers to the line,
$rs = mysql_query($sql);
Any ideas why this is happening?
Thanks for the help
mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
mysql_select_db($db_name);
if(isset($_POST['add']))
{
$tag = $_POST['tag'];
$sql = "select * from tags where tag='$tag;'"
$rs = mysql_query($sql);
if (mysql_num_rows($rs) >= 1{
$does_exist = true;
}
if ($does_exist == true) {
$sql = "UPDATE tags SET `count`=`count`+1, tag ='$tag' WHERE tag='$tag'";
} else {
$sql = "INSERT INTO tags SET `count`=`count`+1, tag ='$tag';";
}
echo "Keyword tag and value updated";
}
$sql = "select * from tags where tag='$tag;'" <------ you forgot the ;
$rs = mysql_query($sql);
add ; to the end of you $sql statement
anytime you see that error, it is usually a syntax mistake from the line before.
$rs = mysql_query($sql);
add ; to the end of you $sql statement
anytime you see that error, it is usually a syntax mistake from the line before.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
Thanks for all the help.
Yes, it was much easier to just add the unique index to the tag column.
Thanks for all the help. It is much appreciated.
Thanks for all the help.
Yes, it was much easier to just add the unique index to the tag column.
Thanks for all the help. It is much appreciated.