0

I have this code into my php script that i test:

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

but when purchase something, data about buyer are not inserted into te_sales table into database. What is wrong with that?

This is entire code:

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

enter image description here

DrMTR
  • 499
  • 1
  • 14
  • 35
  • 1
    Possible duplicate of [How to include a PHP variable inside a MySQL insert statement](https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-insert-statement) – Alexander Jul 01 '17 at 16:08

2 Answers2

2

Have you look if there was an error?

<?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());
}
?>
  • This is error that i get using your code: `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` – DrMTR Jul 01 '17 at 16:09
  • have you escape content of your vars before inserting ? you could use "mysql_real_escape_string" function – Sébastian Guesdon Jul 01 '17 at 16:14
  • replaced mysql_query with mysql_real_escape_string but didnt change nothing. Dont get any error, but dont insert nothing in database. – DrMTR Jul 01 '17 at 16:21
  • Again got: `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` – DrMTR Jul 01 '17 at 16:27
  • replace time() for now() – Sébastian Guesdon Jul 01 '17 at 16:31
  • For information mysql extension is depreciated, use mysqli or pdo_mysql extension for your next developments, bye – Sébastian Guesdon Jul 01 '17 at 16:37
  • Thanks once again. Will follow your instructions. – DrMTR Jul 01 '17 at 16:37
0

You insert syntax is wrong, try like this

$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());
}
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • @DrMTR did you add the quotes with string escaping for example 'mysql_real_escape_string' ? – A l w a y s S u n n y Jul 01 '17 at 16:34
  • No. Didnt add. Only changing the variable from time() to now in SQL query, makes to work it. `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 Jul 01 '17 at 16:37
  • @DrMTR but this is weird '$user_info[id]','$cPack[t_credit]','$cPack[t_price]',now()‌​)" because how it works ? associative array without quotes $user_info[id] should not work because you missed the quote here $user_info['id'] :( :P :P – A l w a y s S u n n y Jul 01 '17 at 16:52
  • @DrMTR but this is weird '$user_info[id]','$cPack[t_credit]','$cPack[t_price]',now()‌​)" because how it works ? associative array without quotes $user_info[id] should not work because you missed the quote here $user_info['id'] :( :P :P – A l w a y s S u n n y Jul 01 '17 at 16:52