PHP没有进入数据库

时间:2017-03-18 16:20:09

标签: php pdo

我正在制作一个购物车数据库,我想要转移那些已经过活动的购物车"进入inactive_carts表48小时。

现在我有了这段代码,但问题是,它没有把它放在数据库中,我检查了所有$time项目都在if命令中

这是我的代码:

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();
        }
    }
}

我根本没有犯错误。它似乎很好

注意: cart_id是一个int,username是一个字符串,user_id是一个int,cart_value是一个数组

2 个答案:

答案 0 :(得分:0)

您可以通过在查询中完成所有操作来简化此操作。执行public class Pay { private string workerName; private double hoursWorked; private double rateOfPay; private double grossPay; private double netPay; private double FICA_TAX = 0.0765; private double FED_TAX = 0.20; private double STATE_TAX = 0.10; private double HEALTH_INS = 0.07; private double ficaTax; private double fedTax; private double stateTax; private double healthIns; public Pay() { /* Pay aPay = new Pay(); aPay.grossPay = grossPay; aPay.netPay = netPay; aPay.ficaTax = ficaTax; aPay.fedTax = fedTax; aPay.stateTax = stateTax; aPay.healthIns = healthIns; */ } public string WorkerName { set { workerName = value; } } public double HoursWorked { set { hoursWorked = value; } } public double RateOfPay { set { rateOfPay = value; } } public double GrossPay { get { grossPay = (hoursWorked * rateOfPay); return grossPay; } } public double NetPay { get { netPay = grossPay - FICA_TAX - FED_TAX - STATE_TAX - HEALTH_INS; return netPay; } } public double FicaTax { get { ficaTax = FICA_TAX * grossPay; return ficaTax; } } public double FedTax { get { fedTax = FED_TAX * grossPay; return fedTax; } } public double StateTax { get { stateTax = STATE_TAX * grossPay; return stateTax; } } public double HealthIns { get { healthIns = HEALTH_INS * grossPay; return healthIns; } } public override string ToString() { string stats; stats = string.Format("Name\t\t {0} \n", workerName); stats += string.Format("Gross Pay\t {0} \n", grossPay); stats += string.Format("FICA tax\t {0} \n", ficaTax); stats += string.Format("Federal tax\t {0} \n", fedTax); stats += string.Format("State tax\t {0} \n", stateTax); stats += string.Format("Health Insurance {0} \n", healthIns); stats += string.Format("Net pay\t\t {0} \n", netPay); return stats; } } 然后删除行。这样您就不必首先获取值(我不知道您是如何得到INSERT..SELECT变量的。)

$times
  

注意:这假定$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 } carts具有相同的列

另请注意,您可能没有收到任何错误,因为您可能没有启用PDO例外。添加

inactive_carts
创建PDO对象后

将启用它。您还应通过在文件顶部添加以下内容来启用本机PHP错误报告

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

答案 1 :(得分:-1)

我遇到的问题是这个问题是数据库中的变量名在我的代码中都是大写和小写。

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

我在这里学到了所有关于php和mysql的知识。是一个众所周知的好例子。