在MySQL插入时苦苦挣扎

时间:2012-04-09 14:35:59

标签: php mysql

您好我正在尝试使用php代码将记录插入表中。

<?php
$server = "localhost";
$user = "root";
$pwd = "";
$db_con = mysql_connect($server,$user,$pwd);
$select_db = mysql_select_db("test_database",$db_con);
$txnid = date ("ydmHis") . mt_rand(1000, 9999);
$custnumber = "4316010100000001";
$cust_mo_num = "7875432990";
$bc_id = "LTH001";
$timestamp = "12:12:12:12:12:12";
$amounta = "500";
$txnupdate = "INSERT INTO cust_txn (txnid,cust_num,cust_mo_num,bc_username,txn_time,txn_amount,txn_type,txn_status) VALUES ('$txnid','$custnumber','$cust_mo_num','$bc_id','$timestamp','$amounta','WTH','SUCC')";
$result = mysql_query($txnupdate);
mysql_close($db_con);
echo $result;
?>

当我运行此页面并刷新它时,记录只插入一次。 (txnid是主键,我每次都会更改它。)我无法知道我哪里出错了。

2 个答案:

答案 0 :(得分:1)

您的代码运行正常。我只是大写表名,因为我正在使用ubunthu框。 (您的create table语句随CUST_TXN表名提供)

<强>来源

<?php
$user = "root";
$pwd = "root123";
$db_con = mysql_connect($server,$user,$pwd);
$select_db = mysql_select_db("ci",$db_con);
$txnid = date ("ydmHis") . mt_rand(1000, 9999);
$custnumber = "4316010100000001";
$cust_mo_num = "7875432990";
$bc_id = "LTH001";
$timestamp = "12:12:12:12:12:12";
$amounta = "500";
$txnupdate = "INSERT INTO CUST_TXN (txnid,cust_num,cust_mo_num,bc_username,txn_time,txn_amount,txn_type,txn_status) VALUES ('$txnid','$custnumber','$cust_mo_num','$bc_id','$timestamp','$amounta','WTH','SUCC')";
echo $txnupdate;
$result = mysql_query($txnupdate);
mysql_close($db_con);
echo $result;
?>

<强>结果

> select * from CUST_TXN;
+------------------+------------------+-------------+-------------+-------------------+------------+----------+------------+
| txnid            | cust_num         | cust_mo_num | bc_username | txn_time          | txn_amount | txn_type | txn_status |
+------------------+------------------+-------------+-------------+-------------------+------------+----------+------------+
| 1209042028162311 | 4316010100000001 | 7875432990  | LTH001      | 12:12:12:12:12:12 | 500        | WTH      | SUCC       |
| 1209042028177407 | 4316010100000001 | 7875432990  | LTH001      | 12:12:12:12:12:12 | 500        | WTH      | SUCC       |
| 1209042028194204 | 4316010100000001 | 7875432990  | LTH001      | 12:12:12:12:12:12 | 500        | WTH      | SUCC       |
| 1209042028342444 | 4316010100000001 | 7875432990  | LTH001      | 12:12:12:12:12:12 | 500        | WTH      | SUCC       |
| 1209042028358383 | 4316010100000001 | 7875432990  | LTH001      | 12:12:12:12:12:12 | 500        | WTH      | SUCC       |
| 1209042028362068 | 4316010100000001 | 7875432990  | LTH001      | 12:12:12:12:12:12 | 500        | WTH      | SUCC       |
+------------------+------------------+-------------+-------------+-------------------+------------+----------+------------+

此外,您可以将current_timestamp用于txn_time列

MYSQL DDL

CREATE TABLE CUST_TXN_2 ( txnid char(16), PRIMARY KEY(txnid), cust_num char(16), cust_mo_num char(10), bc_username char(6), txn_time timestamp DEFAULT CURRENT_TIMESTAMP, txn_amount varchar(10), txn_type varchar(5), txn_status varchar(10) );

<强>来源

<?php
$user = "root";
$pwd = "root123";
$db_con = mysql_connect($server,$user,$pwd);
$select_db = mysql_select_db("ci",$db_con);
$txnid = date ("ydmHis") . mt_rand(1000, 9999);
$custnumber = "4316010100000001";
$cust_mo_num = "7875432990";
$bc_id = "LTH001";
$amounta = "500";
$txnupdate = "INSERT INTO CUST_TXN_2 (txnid,cust_num,cust_mo_num,bc_username,txn_amount,txn_type,txn_status) VALUES ('$txnid','$custnumber','$cust_mo_num','$bc_id','$amounta','WTH','SUCC')";
echo $txnupdate;
$result = mysql_query($txnupdate);
mysql_close($db_con);
echo $result;
?>

<强>结果

> select * from CUST_TXN_2;
+------------------+------------------+-------------+-------------+---------------------+------------+----------+------------+
| txnid            | cust_num         | cust_mo_num | bc_username | txn_time            | txn_amount | txn_type | txn_status |
+------------------+------------------+-------------+-------------+---------------------+------------+----------+------------+
| 1209042055524683 | 4316010100000001 | 7875432990  | LTH001      | 2012-04-09 20:55:52 | 500        | WTH      | SUCC       |
| 1209042055563581 | 4316010100000001 | 7875432990  | LTH001      | 2012-04-09 20:55:56 | 500        | WTH      | SUCC       |
| 1209042055564435 | 4316010100000001 | 7875432990  | LTH001      | 2012-04-09 20:55:56 | 500        | WTH      | SUCC       |
| 1209042055579931 | 4316010100000001 | 7875432990  | LTH001      | 2012-04-09 20:55:57 | 500        | WTH      | SUCC       |
+------------------+------------------+-------------+-------------+---------------------+------------+----------+------------+

答案 1 :(得分:1)

$ txnid听起来像时间戳。 Mysql可以自动插入它。将其设置为

ALTER TABLE CUST_TXN CHANGE txnid txnid TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP。

您不会再在代码中设置时间戳。