Jump to content

a line in my code sends double to my db


Go to solution Solved by Love2c0de,

Recommended Posts

as you can see in my code, the line:

$con->query($sql);

 

for some reason gets the info posted on my database twise, and i have no reason why and how to fix this?

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

$sql = "INSERT INTO Persons (Fra, Til, Voksne, Born, Navn, Adresse, Byen, Telefon, Email, Kommentar)
VALUES
('$_POST[fra]','$_POST[til]','$_POST[voksne]','$_POST[born]','$_POST[navn]','$_POST[adresse]','$_POST[by]','$_POST[telefon]','$_POST[email]','$_POST[kommentar]')";

  {
  $email = $_POST['email'] ;
  $subject = $_POST['navn'] ;
  $navn = $_POST['navn'] ;
  $adresse = $_POST['adresse'] ;
  $by = $_POST['by'] ;
  $telefon = $_POST['telefon'] ;
  $email = $_POST['email'] ;
  $fra = $_POST['fra'] ;
  $til = $_POST['til'] ;
  $voksne = $_POST['voksne'] ;
  $born = $_POST['born'] ;
  $kommentar = $_POST['kommentar'] ;
  $time	 	= $_SERVER['REQUEST_TIME'];//Onix Time Format
  $tid = date('d-m-Y H:i:s', $time);
  $con->query($sql);
  
  mail("joachim@loxfear.com","bestilling fra: " . $subject,
  $navn . "\n" . $adresse . "\n" . $by . "\n" . $telefon . "\n" . $email . "\n\n" . "Fra dato: " . $fra . "\n" . "Til dato: " . $til . "\n" . "Bestilt: " . $tid . "\n\n" . "Antal voksne: " . $voksne . "\n" . "Antal børn: " . $born . "\n\n" . "Bemærkninger: " . $kommentar, "Du har fået en ny Leje Bestilling: ");
  
  mail($email,"bestillings bekreftigelse til: " . $subject,
  $navn . "\n" . $adresse . "\n" . $by . "\n" . $telefon . "\n" . $email . "\n\n" . "Fra dato: " . $fra . "\n" . "Til dato: " . $til . "\n" . "Bestilt: " . $tid . "\n\n" . "Antal voksne: " . $voksne . "\n" . "Antal børn: " . $born . "\n\n" . "Bemærkninger: " . $kommentar, "Denne bestilling blev sendt: " . $con->insert_id);
  
  }
if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "Din Bestilling er sendt!";

mysqli_close($con);
?>
Link to comment
https://forums.phpfreaks.com/topic/281681-a-line-in-my-code-sends-double-to-my-db/
Share on other sites

  • Solution

Good evening,

 

I believe you are executing the query twice.

 

Here: $con->query($sql);

 

and

 

Here: if (!mysqli_query($con,$sql))

 

I'd guess you were meant to be checking the return value of query instead of running the query again.

 

Kind regards,

 

L2c.

Edited by Love2c0de
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.