0

I made a simple document generator by the form, this form saves everything to mysql database, It works great, but when someone type a the same 'nrumowy' it creates a new row in mysql, 'nrumowy' is unique, so when someone adds a form with the same 'nrumowy' I want to just update existing data in mysql, I have that code:

$con=mysqli_connect("localhost","login","pass","database");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$numerklienta = mysqli_real_escape_string($con, $_POST['numerklienta']);
$name = mysqli_real_escape_string($con, $_POST['name']);
$hours = mysqli_real_escape_string($con, $_POST['hours']);
$date = mysqli_real_escape_string($con, $_POST['date']);
$beginDate = mysqli_real_escape_string($con, $_POST['beginDate']);
$nrdomu = mysqli_real_escape_string($con, $_POST['nrdomu']);
$telefon = mysqli_real_escape_string($con, $_POST['telefon']);
$fax = mysqli_real_escape_string($con, $_POST['fax']);
$nip = mysqli_real_escape_string($con, $_POST['nip']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$stronawww = mysqli_real_escape_string($con, $_POST['stronawww']);
$branza = mysqli_real_escape_string($con, $_POST['branza']);
$vatkodpocztowy = mysqli_real_escape_string($con, $_POST['vatkodpocztowy']);
$vatmiejscowosc = mysqli_real_escape_string($con, $_POST['vatmiejscowosc']);
$vatulica = mysqli_real_escape_string($con, $_POST['vatulica']);
$vatnrdomu = mysqli_real_escape_string($con, $_POST['vatnrdomu']);
$vatemail = mysqli_real_escape_string($con, $_POST['vatemail']);
$vatosoba = mysqli_real_escape_string($con, $_POST['vatosoba']);
$datapublikacji = mysqli_real_escape_string($con, $_POST['datapublikacji']);
$rabat = mysqli_real_escape_string($con, $_POST['rabat']);
$wartoscnetto = mysqli_real_escape_string($con, $_POST['wartoscnetto']);
$typreklamy = mysqli_real_escape_string($con, $_POST['typreklamy']);
$inne = mysqli_real_escape_string($con, $_POST['inne']);
$inne2 = mysqli_real_escape_string($con, $_POST['inne2']);
$inne3 = mysqli_real_escape_string($con, $_POST['inne3']);
$zaliczka = mysqli_real_escape_string($con, $_POST['zaliczka']);
$liczbarat1 = mysqli_real_escape_string($con, $_POST['liczbarat1']);
$zaakceptowaneprzez = mysqli_real_escape_string($con, $_POST['zaakceptowaneprzez']);
$telzam = mysqli_real_escape_string($con, $_POST['telzam']);
$datapodpis = mysqli_real_escape_string($con, $_POST['datapodpis']);
$nrumowy = mysqli_real_escape_string($con, $_POST['nrumowy']);

$sql="IF
NOT EXISTS ( SELECT * FROM zam WHERE nrumowy = '$nrumowy' )

THEN

INSERT INTO zam (numerklienta, name, hours, date, beginDate, nrdomu, telefon, fax, nip, email, stronawww, branza, vatkodpocztowy, vatmiejscowosc, vatulica, vatnrdomu, vatemail, vatosoba, datapublikacji, rabat, wartoscnetto, typreklamy, inne, inne2, inne3, zaliczka, liczbarat1, zaakceptowaneprzez, telzam, datapodpis, nrumowy)
VALUES ('$numerklienta', '$name', '$hours', '$date', '$beginDate', '$nrdomu', '$telefon', '$fax', '$nip', '$email', '$stronawww', '$branza', '$vatkodpocztowy', '$vatmiejscowosc', '$vatulica', '$vatnrdomu', '$vatemail', '$vatosoba', '$datapublikacji', '$rabat', '$wartoscnetto', '$typreklamy', '$inne', '$inne2', '$inne3', '$zaliczka', '$liczbarat1', '$zaakceptowaneprzez', '$telzam', '$datapodpis', '$nrumowy' )

ELSE

UPDATE zam SET name = '$name', numerklienta = '$numerklienta', hours = '$hours', date = '$date', beginDate = '$beginDate', nrdomu = '$nrdomu', telefon = '$telefon', fax = '$fax', nip = '$nip', email = '$email', stronawww = '$stronawww', branza = '$branza', vatkodpocztowy = '$vatkodpocztowy', vatmiejscowosc = '$vatmiejscowosc', vatulica = '$vatulica', vatnrdomu = '$vatnrdomu', vatemail = '$vatemail', vatosoba = '$vatosoba', datapublikacji = '$datapublikacji', rabat = '$rabat', wartoscnetto = '$wartoscnetto', typreklamy = '$typreklamy', inne = '$inne', inne2 = '$inne2', inne3 = '$inne3', zaliczka = '$zaliczka', liczbarat1 = '$liczbarat1', zaakceptowaneprzez = '$zaakceptowaneprzez', telzam = '$telzam', datapodpis = '$datapodpis' WHERE nrumowy ='$nrumowy'

END IF";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}


mysqli_close($con);

This query without " select..... " and "else update" just a 'insert into' works great, also when I change this 'insert into' to 'update' but I don't know how to make this variable if not exists - insert into - else update

1
  • What made you think you can just embed to SQL statements in an if..else construct? I think you can do that only in a stored procedure. Commented Aug 19, 2014 at 9:38

4 Answers 4

2

There are two options you can use on MySQL.

  1. Use 'REPLACE' . The issue here is that if you record has foreign key then you're in trouble as described here
  2. Use INSERT ... ON DUPLICATE KEY UPDATE. its usage is well described in this article

I'd prefer 2) so in your case you need to do the following:

  1. Make 'nrumowy' primary key or unique index
  2. Update the SQL to the something like following:

(shortened version)

INSERT INTO zam
            (numerklienta,
             name,
/* put rest of the fields here */
             nrumowy)
VALUES ('$numerklienta',
             '$name',
/* put rest of the fields here */
             '$nrumowy' )
ON DUPLICATE KEY UPDATE
  numerklienta = values(numerklienta),
  name = value(name)
/* put rest of the fields here */
  ;     

PS: In general I'd suggest you to stop using raw SQL in your code. Use PDO functions such as 'PDO::prepare' and 'PDOStatement::bindValue'. You can see good examples in PHP documentation.

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

1 Comment

I used 'ON DUPLICATE KEY UPDATE' and setting 'nrumowy' as primary and unique, that's what I wanted, thank you very much.
0

You should definitely read about REPLACE INTO.

Comments

0

you can check nrumowy from table

$check = "SELECT nrumowy FROM zam WHERE nrumowy = '$nrumowy'";
$check = mysql_query($check);
$check = mysql_fetch_array($check);
$check = $check['nrumowy'];

if (empty($check))
        {
            insert
        }
else
        {
            update
        }

Comments

0

iatboy, is this should look like this? Because this don't work for me (blank page), I'm totally newbie at mysql and php, this is only form for easiest work, this won't be used at public page.

<?php
$con=mysqli_connect("localhost","login","pass","db");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$numerklienta = mysqli_real_escape_string($con, $_POST['numerklienta']);
$name = mysqli_real_escape_string($con, $_POST['name']);
$hours = mysqli_real_escape_string($con, $_POST['hours']);
$date = mysqli_real_escape_string($con, $_POST['date']);
$beginDate = mysqli_real_escape_string($con, $_POST['beginDate']);
$nrdomu = mysqli_real_escape_string($con, $_POST['nrdomu']);
$telefon = mysqli_real_escape_string($con, $_POST['telefon']);
$fax = mysqli_real_escape_string($con, $_POST['fax']);
$nip = mysqli_real_escape_string($con, $_POST['nip']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$stronawww = mysqli_real_escape_string($con, $_POST['stronawww']);
$branza = mysqli_real_escape_string($con, $_POST['branza']);
$vatkodpocztowy = mysqli_real_escape_string($con, $_POST['vatkodpocztowy']);
$vatmiejscowosc = mysqli_real_escape_string($con, $_POST['vatmiejscowosc']);
$vatulica = mysqli_real_escape_string($con, $_POST['vatulica']);
$vatnrdomu = mysqli_real_escape_string($con, $_POST['vatnrdomu']);
$vatemail = mysqli_real_escape_string($con, $_POST['vatemail']);
$vatosoba = mysqli_real_escape_string($con, $_POST['vatosoba']);
$datapublikacji = mysqli_real_escape_string($con, $_POST['datapublikacji']);
$rabat = mysqli_real_escape_string($con, $_POST['rabat']);
$wartoscnetto = mysqli_real_escape_string($con, $_POST['wartoscnetto']);
$typreklamy = mysqli_real_escape_string($con, $_POST['typreklamy']);
$inne = mysqli_real_escape_string($con, $_POST['inne']);
$inne2 = mysqli_real_escape_string($con, $_POST['inne2']);
$inne3 = mysqli_real_escape_string($con, $_POST['inne3']);
$zaliczka = mysqli_real_escape_string($con, $_POST['zaliczka']);
$liczbarat1 = mysqli_real_escape_string($con, $_POST['liczbarat1']);
$zaakceptowaneprzez = mysqli_real_escape_string($con, $_POST['zaakceptowaneprzez']);
$telzam = mysqli_real_escape_string($con, $_POST['telzam']);
$datapodpis = mysqli_real_escape_string($con, $_POST['datapodpis']);
$nrumowy = mysqli_real_escape_string($con, $_POST['nrumowy']);



$result = mysql_query(SELECT nrumowy FROM zam WHERE nrumowy = '$nrumowy' limit 1);
if (empty(mysql_fetch_array($result)))
{
    mysql_query(INSERT INTO zam (numerklienta, name, hours, date, beginDate, nrdomu, telefon, fax, nip, email, stronawww, branza, vatkodpocztowy, vatmiejscowosc, vatulica, vatnrdomu, vatemail, vatosoba, datapublikacji, rabat, wartoscnetto, typreklamy, inne, inne2, inne3, zaliczka, liczbarat1, zaakceptowaneprzez, telzam, datapodpis, nrumowy)
VALUES ('$numerklienta', '$name', '$hours', '$date', '$beginDate', '$nrdomu', '$telefon', '$fax', '$nip', '$email', '$stronawww', '$branza', '$vatkodpocztowy', '$vatmiejscowosc', '$vatulica', '$vatnrdomu', '$vatemail', '$vatosoba', '$datapublikacji', '$rabat', '$wartoscnetto', '$typreklamy', '$inne', '$inne2', '$inne3', '$zaliczka', '$liczbarat1', '$zaakceptowaneprzez', '$telzam', '$datapodpis', '$nrumowy' ));
}
else
{
    mysql_query(UPDATE zam SET name = '$name', numerklienta = '$numerklienta', hours = '$hours', date = '$date', beginDate = '$beginDate', nrdomu = '$nrdomu', telefon = '$telefon', fax = '$fax', nip = '$nip', email = '$email', stronawww = '$stronawww', branza = '$branza', vatkodpocztowy = '$vatkodpocztowy', vatmiejscowosc = '$vatmiejscowosc', vatulica = '$vatulica', vatnrdomu = '$vatnrdomu', vatemail = '$vatemail', vatosoba = '$vatosoba', datapublikacji = '$datapublikacji', rabat = '$rabat', wartoscnetto = '$wartoscnetto', typreklamy = '$typreklamy', inne = '$inne', inne2 = '$inne2', inne3 = '$inne3', zaliczka = '$zaliczka', liczbarat1 = '$liczbarat1', zaakceptowaneprzez = '$zaakceptowaneprzez', telzam = '$telzam', datapodpis = '$datapodpis' WHERE nrumowy ='$nrumowy');
}

if (!mysqli_query($con,$result)) {
  die('Error: ' . mysqli_error($con));
}


mysqli_close($con);
?>

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.