0

my sql insert query is not working in my program. I have print the query and then copy paste that code in mysql tab of the phpmyadmin, then it works perfectly. Any body please help me.

if ($_FILES["thumbnailimage"]["size"]>0 )
{
    $thumbnailkey = generateUniqueKey($tbl_uploads,"upload_key",12);
    $fkey = generateUniqueKey($tbl_uploads,"file_key",24);
    $folderkey = generateUniqueKey($tbl_uploads,"folderkey",28);
    $fname = substr($_FILES['thumbnailimage']['name'],0,strpos($_FILES['thumbnailimage']['name'],"."));
    $ext = getExtension($_FILES['thumbnailimage']['name']);
    $insertnewupload = "INSERT INTO ".$tbl_uploads." (upload_key,file_key,file_name,file_type,ext,folderkey,user_id,status,pkey) VALUES ";
    $insertnewupload.="('".$thumbnailkey."','".$fkey."','".$fname."','1','".$ext."','".$folderkey."','".$_SESSION['user_id']."','0','".$productkey."')";
    echo "<br>1=>".$insertnewupload;
//  $db->connect();
    $exec_insertnewitem = mysql_query($insertnewupload);

This is the printed out put

INSERT INTO tbl_uploads (upload_key,file_key,file_name,file_type,ext,folderkey,user_id,status,pkey) VALUES ('f958c38e5c31','9b6bd5118ec4a8456bcc46df','sunil','1','jpg','1c1a536fbdde4f24a219ada4c1c9','7','0','3b593aff92ce')
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Please write error here you are getting in insert operation.. – Milap Feb 22 '12 at 14:14
  • How did you find out it does not work in your program? – hakre Feb 22 '12 at 14:15
  • There is no error. Data is not inserted when I run the program. – Sunil Kumar Feb 22 '12 at 14:15
  • 1
    I sure hope productKey can't be specified by the user, otherwise you might be in store for a nasty shock someday. You really should be using parameterized queries or escaping the inputs: http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php – tvanfosson Feb 22 '12 at 14:16
  • no data inserted in the table , when I run the program – Sunil Kumar Feb 22 '12 at 14:16
  • no productkey is already taken . – Sunil Kumar Feb 22 '12 at 14:19
  • Please remember, I can run this query very well via the mysql tab of the phpmyadmin. But i can't run it fro my program. I thought it may be due to any connection existence problem .. – Sunil Kumar Feb 22 '12 at 14:20
  • Use http://php.net/manual/en/function.mysql-error.php to output errors that are happen. – Timur Feb 22 '12 at 14:22
  • If the problem is about the existence of a database connection, you need to post more information. There is nothing in the code above where a connection would be opened. – Feysal Feb 22 '12 at 14:23

3 Answers3

1

You are quoting numeric values, you should aim for. I've added backticks around the field names also (I can't recall if 'status' is reserved)

INSERT INTO `tbl_uploads` (
    `upload_key`,
    `file_key`,
    `file_name`,
    `file_type`,
    `ext`,
    `folderkey`,
    `user_id`,
    `status`,
    `pkey`
) 
VALUES (
    'f958c38e5c31',
    '9b6bd5118ec4a8456bcc46df',
    'sunil',
    '1',
    'jpg',
    '1c1a536fbdde4f24a219ada4c1c9',
    7,
    0,
    '3b593aff92ce'
)

So the following replacement for the line specifying values will suffice

$insertnewupload = "INSERT INTO `".$tbl_uploads."` (`upload_key`,`file_key`,`file_name`,`file_type`,`ext`,`folderkey`,`user_id`,`status`,`pkey`) VALUES ";
$insertnewupload.="('".$thumbnailkey."','".$fkey."','".$fname."','1','".$ext."','".$folderkey."',".$_SESSION['user_id'].",0,'".$productkey."')";

As an addition, there'll probably be a few comments stating you should be using mysqli_ functions or PDO instead of mysql_. At present you're potentially vulnerable to SQL injection with such a method of making a query.

0

Could be severy reasons... did you check that you connect to the correct database ? Maybe add the database name before "tbl_uploads", e.g. "mybase.tbl_uploads"

mbauer
  • 183
  • 10
0

Always make practice to write mysql query like this.

 $query = "INSERT INTO tablename (`upload_key`,`file_key`,`file_name`,`file_type`,`ext`,`folderkey`,`user_id`,`status,pkey`) VALUES ('f958c38e5c31','9b6bd5118ec4a8456bcc46df','sunil','1','jpg','1c1a536fbdde4f24a219ada4c1c9','7','0','3b593aff92ce')";
 $check = mysql_query($query);

check if var_dump($check);returns true or false..

Milap
  • 6,915
  • 8
  • 26
  • 46