插入查询在我的程序中不起作用,但在mysql选项卡上运行良好

时间:2012-02-22 14:11:46

标签: php mysql insert

我的sql insert查询无法在我的程序中运行。我打印了查询,然后将该代码复制粘贴到phpmyadmin的mysql选项卡中,然后它完美运行。任何人都请帮帮我。

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

这是打印出来的

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')

3 个答案:

答案 0 :(得分:1)

您正在引用数值,您应该瞄准。我也在字段名称周围添加了反引号(我不记得'状态'是否保留)

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'
)

因此,以下替换行指定值就足够了

$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."')";

作为补充,可能会有一些评论声明你应该使用mysqli_ functions或PDO而不是mysql_。目前,使用这种查询方法,您可能容易受到SQL注入攻击。

答案 1 :(得分:0)

可能是个原因......你检查过你连接到正确的数据库了吗?也许在“tbl_uploads”之前添加数据库名称,例如“mybase.tbl_uploads”

答案 2 :(得分:0)

总是让练习写这样的mysql查询。

 $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);

检查var_dump($ check);是否返回true或false ..