Only update not insert again if email is exit


#1

i want that only insert first time and next time when i changed some thing only update do not insert again

<?php
session_start();
include_once('../dbconnect.php');
if(!isset($_SESSION['user']))
{
header("Location: ../index.php");
}

$email=$_SESSION['user'];
$res=mysql_query("SELECT * FROM user_businessleads WHERE user_email='$email'");
$row=mysql_fetch_array($res);

if(isset($_POST['submit']))
{
$looking_for = $POST['lookingfor'];
$services = $_POST['services'];
$mybudget = $_POST['mybudget'];
$around = $_POST['around'];
$s_reguirement = $POST['sreguirement'];
$chk="";
$email=$_SESSION['user'];
foreach($services as $chk1)
{
$chk .= $chk1.",";

}

   $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')");

}
if(isset($_POST['submit']))
{
$looking_for = $POST['lookingfor'];
$services = $_POST['services'];
$mybudget = $_POST['mybudget'];
$around = $_POST['around'];
$s_reguirement = $POST['sreguirement'];
$chk="";
$email=$_SESSION['user'];
foreach($services as $chk1)
{
$chk .= $chk1.",";

}
$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;
}
?>
in this code i got problem when i want update email it will be insert again an other row updated
i want that only insert first time next time only updated


#2

@rubywhiz10534
You update something in your database like this:

$query->prepare('UPDATE tableName SET keyName = ?');

#3

not working this query
can u changed in my code


#4

@rubywhiz10534

Do not set value directly like this.

$pdo = new PDO('mysql:dbname=/*The name of your database*/;host=/*the host name*/, /*username between quotes*/, /*password between quotes*/);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ATTR_ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$query = $pdo->prepare('UPDATE user_businessleads SET services=?, looking_for=?, mybudget=?, around=?,s_requirment=? WHERE user_email');
$query->execute($chk, $looking_for, $mybudget, $around, $s_reguirement);

#5

in my code value is insert through session
in same session i want update read my whole code .
my problem is that ....
only insert once do not again
in my code row is updated but inserted again in new row