【问题标题】:Data is not inserting into database, no error showed数据未插入数据库,未显示错误
【发布时间】:2023-04-11 07:10:01
【问题描述】:

我将这段代码放入我测试的 php 脚本中:

mysql_query("INSERT INTO te_sales(uid,s_credit,s_price,s_date) VALUES ('$user_info[id]','$cPack[t_credit]','$cPack[t_price]',time())");

但是当购买东西时,关于买家的数据不会插入到数据库中的te_sales 表中。这有什么问题?

这是完整的代码:

if(isset($_POST['te_package'])){
// Lets get the data....
 $sP = intval($_POST['te_package']);
// Lets check in DB either it is there or not.
$chk = mysql_query("SELECT * FROM te_pack WHERE id='$sP' LIMIT 1");
 if(mysql_num_rows($chk)== 1){ // Founded go ahead
// Fetch the sP pack
$cPack = mysql_fetch_array($chk);
// Check user balance...
if($user_info['purchase_balance'] >= $cPack['t_price']){ // Proceed as user 
have enough balance to make purchase.
// Lets Cut out the user balance... And give the TE Credits
mysql_query("UPDATE members SET purchase_balance = purchase_balance - 
'$cPack[t_price]' , te_credit = te_credit + '$cPack[t_credit]' 
WHERE id='$user_info[id]'");
// Insert the logs of sales...
//mysql_query("INSERT INTO te_sales(uid,s_credit,s_price,s_date) VALUES 
('$user_info[id]','$cPack[t_credit]','$cPack[t_price]',time())");
 $result = mysql_real_escape_string("
INSERT INTO te_sales(uid,s_credit,s_price,s_date) 
VALUES ('$user_info[id]','$cPack[t_credit]','$cPack[t_price]',time())
");

if (!$result) {
die('Invalid request : ' . mysql_error());
}

【问题讨论】:

标签: php mysql database mysqli


【解决方案1】:

你看看有没有错误?

<?php
$result_data = mysql_query('
    INSERT INTO te_sales(uid,s_credit,s_price,s_date) 
    VALUES (
        "'.mysql_real_escape_string($user_info['id']).'",
        "'.mysql_real_escape_string($cPack['t_credit']).'",
        "'.mysql_real_escape_string($cPack['t_price']).'",
        now()
    )
');

if (!$result_data) {
  die('Invalid query request: ' . mysql_error());
}
?>

【讨论】:

  • 这是我使用您的代码时遇到的错误:Invalid request : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 2
  • 在插入之前您是否已经转义了 vars 的内容?你可以使用“mysql_real_escape_string”函数
  • 用 mysql_real_escape_string 替换了 mysql_query 但没有任何改变。不要出现任何错误,但不要在数据库中插入任何内容。
  • 又得到:Invalid query request: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') )' at line 1
  • 将 time() 替换为 now()
【解决方案2】:

你插入语法错误,试试这样

$result_data = mysql_query("
  INSERT INTO te_sales(uid,s_credit,s_price,s_date) VALUES ('".$user_info['id']."','".$cPack['t_credit']."','".$cPack['t_price']."',time()     )
");

if (!$result_data) {
  die('Invalid query request: ' . mysql_error());
}

【讨论】:

  • @DrMTR 你是否添加了带有字符串转义的引号,例如'mysql_real_escape_string'?
  • 没有。没加。仅在 SQL 查询中将变量从 time() 更改为 now 即可。 mysql_query("INSERT INTO te_sales(uid,s_credit,s_price,s_date) VALUES ('$user_info[id]','$cPack[t_credit]','$cPack[t_price]',now())");
  • @DrMTR 但这很奇怪 '$user_info[id]','$cPack[t_credit]','$cPack[t_price]',now()‌​)" 因为它是如何工作的?没有引号的关联数组 $user_info[id] 不应该工作,因为你错过了这里的引号 $user_info['id'] :( :P :P
  • @DrMTR 但这很奇怪 '$user_info[id]','$cPack[t_credit]','$cPack[t_price]',now()‌​)" 因为它是如何工作的?没有引号的关联数组 $user_info[id] 不应该工作,因为你错过了这里的引号 $user_info['id'] :( :P :P
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-18
  • 1970-01-01
  • 2019-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多