0

If the row for a specific user relevant to a specific product in the table called "user_star_rate" is null, I want to insert the data to the table and otherwise I want to update the table. But the update function is not working.

$jsqla7 = mysql_query("select * from user_star_rate where product_id='$product_id' and email='$visit_email'") or die(mysql_error());
$jfeta7 = mysql_fetch_assoc($jsqla7);

if($jfeta7 != null) {
    $rate = "UPDATE user_star_rate SET rate_value='$rate_value' WHERE product_id='$product_id' and email='$visit_email'" ;
} else {
    $rate = "INSERT INTO user_star_rate (email, product_id, rate_value) VALUES ('$visit_email','$product_id','$rate_value')" ;
}
3
  • mysql_* functions are deprecated. You should use mysqli_* or PDO. You also should use prepared statements to prevent sql injection. Commented Dec 12, 2014 at 6:57
  • 2
    Look into REPLACE INTO Commented Dec 12, 2014 at 6:58
  • 1
    try mysql_num_rows($result) != 0 instead of $jfeta7 != null Commented Dec 12, 2014 at 6:59

2 Answers 2

1

Change

$jfeta7 = mysql_fetch_assoc($jsqla7);
if($jfeta7 != null) {

to

$jfeta7 = mysql_num_rows($jsqla7);
if($jfeta7 > 0) {

Note: mysql_ functions are deprecated and do not use it. They will be removed in the upcoming versions of PHP.

Sign up to request clarification or add additional context in comments.

Comments

1
if(mysql_num_rows($jsqla7 )==0)
{

//INSERT
}
else
{
//UPDATE
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.