-2

Hi I have this code shown below. This works great but every so often the data does not get added to the database. What is the best way to ensure that the data gets added to the database. What I think should happen is that on an error it tries again maybe every 15 seconds for say 3 times. Let me know if you don't agree.

Can anybody show me the php code I need to add and where I should add it. I'm a little out of my depth but I'm thinking that the php knows when it's failed as there is "if ($conn->connect_error)" in the code.

I'm also concerned that the data does not get added multiple times.

Please let me know if this is more complicated than I'm thinking. Any help, comments are welcome.

    <?php
///////////////////////////////////////////////
//////////////////// Login ////////////////////
///////////////////////////////////////////////
    $servername = "*******";
    $username = "*******";
    $password = "*******";
    $dbname = "*******";


///////////////////////////////////////////////
/////////////// Get vars //////////////////////
///////////////////////////////////////////////
    $identifier = $_REQUEST['identifier'];
    $paid = "Not Paid";
    $cartAmount = $_REQUEST['cartAmount'];
    $cartMore = $_REQUEST['cartMore'];
    $cartLink = $_REQUEST['cartLink'];
    $cartImage = $_REQUEST['cartImage'];
    $cartSelected = $_REQUEST['cartSelected'];
    $cartName = $_REQUEST['cartName'];
    $cartQuantity = $_REQUEST['cartQuantity'];
    $shippedDate = $_REQUEST['shippedDate'];
    $myNote = $_REQUEST['myNote'];


///////////////////////////////////////////////
///////////// Create connection ///////////////
///////////////////////////////////////////////
    $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }


///////////////////////////////////////////////
///////////// add vars to data base /////////////
///////////////////////////////////////////////
    $sql = "INSERT INTO customers (cartAmount, cartMore, cartLink, cartImage, cartSelected, cartName, cartQuantity, identifier, paid, shipped_date, myNote)
    VALUES ('$cartAmount', '$cartMore', '$cartLink', '$cartImage', '$cartSelected', '$cartName', '$cartQuantity', '$identifier', '$paid', '$shippedDate', '$myNote')";
    
    if ($conn->query($sql) === TRUE) {
        echo $cartAmount . "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
    
    $conn->close();
    echo "Connected successfully";
    ?>
user2238083
  • 583
  • 2
  • 9
  • 21
  • 1
    how about you start logging errors and debug it properly –  Jun 27 '16 at 04:02
  • Retrying a failed query is a horrible strategy. There is a reason of a query fails. Find and solve it. – arkascha Jun 27 '16 at 04:03
  • Run query generated by this `echo $sql;` in phpmyadmin and check it works or not ? – Niklesh Raut Jun 27 '16 at 04:05
  • 1
    probably being open to SQL injection has a lot to do with it, any semicolon in any sent field will break this –  Jun 27 '16 at 04:09
  • Use parameterized queries, http://php.net/manual/en/mysqli.quickstart.prepared-statements.php. – chris85 Jun 27 '16 at 04:12
  • `echo $cartAmount . "New 2nd level sql injection created successfully";` – Drew Jun 27 '16 at 04:12
  • Thanks! What is the best way to go about logging errors. Could I place in my PHP code above something like if ($conn->connect_error) { write error to folder } – user2238083 Jun 27 '16 at 04:13
  • Write to a file in append mode is not a bad idea. http://stackoverflow.com/a/24972441/1816093. Turn on [error reporting](http://stackoverflow.com/a/21429652) – Drew Jun 27 '16 at 04:14

1 Answers1

-1

I am more concerned as to why it doesn't get added sometimes. That is where you need to look at. Is there a problem with the input, does it throw an exception when you try adding it? There must be a reason. Find it and format your input such that it will always work.

The other problem could be your database is inaccessible and it will throw an exception so use that to try again or find a more reliable host.

Good luck

Realist
  • 34
  • 1
  • 3