---
title: "update the email value if exit if email is not exit then insert new email"  
description: "update the email value if exit if email is not exit then insert new email"  
author: "Himanshu Rai"  
published: 2016-03-06  
updated: 2016-03-08  
canonical: https://www.mindstick.com/forum/34036/update-the-email-value-if-exit-if-email-is-not-exit-then-insert-new-email  
category: "php"  
tags: ["php", "mysql", "sql"]  
reading_time: 1 minute  

---

# update the email value if exit if email is not exit then insert new email

if(!isset($_SESSION['user'])){ [header](https://www.mindstick.com/articles/336036/explain-about-html-header-body-and-footer-tags)("[Location](https://www.mindstick.com/blog/11636/relocating-business-or-office-to-another-location): ../index.php");}\
$email=$_SESSION['user'];$res=mysql_query("[SELECT](https://www.mindstick.com/forum/160534/orderby-then-select-vs-select-then-orderby-performance) * FROM user_businessleads WHERE user_email='$email'");$row=mysql_fetch_array($res);\
if(isset($_POST['submit'])) { $looking_for = $_POST['looking_for']; $[services](https://www.mindstick.com/articles/13111/distinctions-of-iiot-services-how-its-helps-brands) = $_POST['services']; $mybudget = $_POST['mybudget']; $around = $_POST['around']; $s_reguirement = $_POST['s_reguirement']; $chk=""; $email=$_SESSION['user']; [foreach](https://www.mindstick.com/forum/33870/how-parallel-foreach-works-internally)($services as $chk1) { $chk .= $chk1.","; } if(!empty($email)) { $query=mysql_query("[INSERT](https://www.mindstick.com/blog/173/executing-insert-delete-or-update-query-in-sqlserver-using-ado-dot-net) INTO user_businessleads(services,user_email,looking_for,mybudget,around,s_requirment)[values](https://www.mindstick.com/forum/327/sum-textbox-values)('$chk','$email','$looking_for','$mybudget','$around','$s_reguirement')"); }else{ $updated=mysql_query("[UPDATE](https://www.mindstick.com/forum/156353/what-is-hummingbird-update) user_businessleads SET services='$chk',looking_for='$looking_for',mybudget='$mybudget',around='$around',s_requirment='$s_reguirement'WHERE user_email='$email'")or die;}}in this query in my [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) value is inserting but did not update .i want that is email id not exit in table which is given first insert after that if it is exit the only updatedplz suggest me what the changes [required](https://www.mindstick.com/forum/160269/what-is-required-data-annotation-how-does-it-affect-model-validation) in above code....\
\

## Replies

### Reply by Sachin Singh

I think you need to Change Condition of **If** case as look like below, Because if any email are exists in '$res' query then it will be updated, if not then created.

\

```
if(mysqli_num_rows($res) >= 0))
```

```
 { $updated=mysql_query("UPDATE user_businessleads SET services='$chk',looking_for='$looking_for', mybudget='$mybudget',around='$around',s_requirment='$s_reguirement'WHERE user_email='$email'")or die; }else{    $query=mysql_query("INSERT INTO user_businessleads(services,user_email,looking_for,mybudget,  around,s_requirment)values('$chk','$email','$looking_for','$mybudget','$around','$s_reguirement')"); }}
```

\
\


---

Original Source: https://www.mindstick.com/forum/34036/update-the-email-value-if-exit-if-email-is-not-exit-then-insert-new-email

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
