在数据库表中插入varchar优惠券代码

时间:2015-04-25 09:51:33

标签: php mysql sql-insert

我有一个基于奖励积分的网站。我想将数据库中的“优惠券代码”存储到一个记录中,该记录用户使用哪个代码为其帐户充值以购买任何东西。我在php中创建了一个插入表格,以便为用户插入优惠券。

我遇到一个问题,INSERT INTO sql语法只将int代码插入到数据库中,但当我们使用VARCHAR代码为帐户充值时,它不会存储任何数据...

示例:

  • int coupon code: 22334466
  • int coupon code: SU5C5E6S


<?php
if(isset($_POST['sub'])){
    $db_host="localhost";
    $db_username="root";
    $db_password="";
    $db_name="bs1";
    $con=mysql_connect("$db_host", "$db_username", "$db_password") or die("could not connect to mysql!!!");
    if($con=="")
    {
        echo "Database not connected!!!!";
    }
    else
    {
        $isdb=mysql_select_db("$db_name") or die("database not available!!!!");
        if($isdb=="")
        {
            echo "database not selected!!!!";
        }
        else
        {   
            $emp_ID=$_POST['emp_ID'];
            $code=$_POST['code'];


            $query = mysql_query("select * from oc_abhi_reward where `Code`='$code'") or die (mysql_error());
            $data=mysql_fetch_assoc($query);
            $code_db=$data['Code'];
            $points_db=$data['Point'];
            if($code==$code_db)
            {
            $query1 = mysql_query("select * from oc_customer where `emp_ID`='$emp_ID'") or die (mysql_error());
            $data1=mysql_fetch_assoc($query1);
            $customer_id=$data1['customer_id'];

            $query2=mysql_query("INSERT INTO `oc_customer_reward` (customer_id, emp_ID,  order_id, description, points, date_added) VALUES ($customer_id, $emp_ID,  0, 'rewarded', $points_db, NOW());");

            $query4=mysql_query("INSERT INTO `oc_customer_reward_history` (customer_id, emp_ID, description, Code, points) VALUES ($customer_id, $emp_ID, 'rewarded', $code, $points_db)");


            $query3=mysql_query("DELETE FROM oc_abhi_reward WHERE Code='$code'");
            header("location:http://localhost/bsl/index.php?route=account/account");
            exit();
            }
            else
            {

            }           
        }
    }
}
?>
<!--php code for reward entry END-->

1 个答案:

答案 0 :(得分:0)

$ code应该用单引号写,所以数据库中插入变量字符代码的代码应该是这样的,

$query4=mysql_query("INSERT INTO `oc_customer_reward_history` (customer_id, emp_ID, description, Code, points) VALUES ($customer_id, $emp_ID, 'rewarded', '$code', $points_db)");