2

I'm making a cart database and I want to transfer carts that have been "alive" for 48 hours into an inactive_carts table.

now I have this code, but the problem is, it doesnt put it in the database, i checked and all of the $time items go in the if command

here's my code:

foreach ($times as $time) {
    $username = $time['username'];
    $user_id = $time['user_id'];
    $cart_id = $time['cart_id'];
    $cart_value = $time['cart_value'];
    if ($timeAlive >= 12000) {
        try {

            $queryInctive = $con->prepare('
                INSERT INTO inactive_carts(`cart_id` , `username` , `user_id` , `cart_value` , `date`) 
                 VALUES(:cart_id , ":username" , :user_id , ":cart_value" , CURRENT_TIMESTAMP)');
            $queryInctive->bindParam(":cart_id" , $cart_id , PDO::PARAM_INT);
            $queryInctive->bindParam(":username" , $username);
            $queryInctive->bindParam(":user_id" , $user_id , PDO::PARAM_INT);
            $queryInctive->bindParam(":cart_value" ,$cart_value );
            $queryInctive->execute();
        } catch (Exception $e) {
            echo "Error is: " . $e->getMessage();
        }
    }
}

I don't get errors at all. it seems to be all well

NOTE: cart_id is an int , username is a string , user_id is an int, cart_value is an array

roalz
  • 2,699
  • 3
  • 25
  • 42
Guy Sudai
  • 36
  • 5
  • How do you get your `$times` variable? Are you sure you enter the `if`-block? Is PDO set to throw exceptions at all? What about PHP native error-logging? `error_reporting(E_ALL); ini_set("display_errors", 1);` – Qirel Mar 18 '17 at 16:28

2 Answers2

0

You could probably simplify this by doing it all in queries. Do a INSERT..SELECT and then delete the rows after. This way you don't have to fetch the values first (I don't know how you got the $times variable to begin with).

$con->beginTransaction();     // Begin transaction
try {
    // Set a MySQL variable with the current time
    $con->query('SET @NOW := (NOW())');

    // Insert from old table into new table
    $con->query('INSERT INTO inactive_carts SELECT * FROM carts WHERE date < DATE_SUB(@NOW, INTERVAL 2 DAY)');

    // Then delete the old values
    $con->query('DELETE FROM carts WHERE date < DATE_SUB(@NOW, INTERVAL 2 DAY)');

    // Commit changes!
    $con->commit();
} catch (PDOException $e) {
    // Something went bad, and transfer wasn't made
}

Note: This assumes that carts and inactive_carts have the same columns

Also note that you might not get any errors because you might not have enabled PDO Exceptions. Adding

$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

after creating the PDO-object will enable that. You should also enable native PHP error reporting, by adding the following at the top of your file

<?php 
error_reporting(E_ALL);
ini_set("display_errors", 1);
Community
  • 1
  • 1
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • Fixed it, the problem was I had quotes around the parameters, thanks anyways. – Guy Sudai Mar 18 '17 at 18:15
  • Btw, do you happen to know a way i can insert an array to a database? – Guy Sudai Mar 18 '17 at 18:15
  • This is a more elegant and more dynamic solution though, you might as well adopt it ;-) And you shouldn't insert arrays or comma-separated values into to databases, you should normalize them instead. See http://stackoverflow.com/questions/246701/what-is-normalisation-or-normalization – Qirel Mar 18 '17 at 18:33
  • Yeah but the number of columns isn't set – Guy Sudai Mar 19 '17 at 11:19
  • Well, you can move the selected columns by doing `SELECT \`cart_id\`, \`username\`, \`user_id\`, \`cart_value\`` instead of `SELECT *`, and then have `date` column as `CURRENT_TIMESTAMP` default. – Qirel Mar 19 '17 at 12:39
  • well, I just used implode and put it as an "Array" – Guy Sudai Mar 19 '17 at 15:02
  • If it works for you, by all means - but using comma-separated values in a table like that isn't good practice. Like I said, look into normalization instead (see the link posted a few comments above). It becomes more maintainable in the long run ;-) – Qirel Mar 19 '17 at 15:59
-1

I had the problem that this problem was variable names in the database were uppercase and lowercase in my code.

https://www.w3schools.com/php/php_mysql_intro.asp

I here I learned everything I know about php and mysql. is a known good examples.

Lau
  • 1
  • 1