当在mysql数据库中存储图像时,我有错误


when storing image in mysql database i have error

警告:mysql_connect():第37行C:''examplep''htdocs''ramya''store.php中的用户'root'@'localhost'(使用密码:YES

<?php
    error_reporting(E_ALL);
    $server = "localhost";
    $login = "root";
    $s_password = " ";
    $hotel_name=$_POST['hotel_name'];
    $street_name=$_POST['street_name'];
    $city=$_POST['city'];
    $state=$_POST['state'];
    $country=$_POST['country'];
    $zipcode=$_POST['zipcode'];
    $phone_number=$_POST['phone_number'];
    $fax=$_POST['fax'];
    $email_id=$_POST['email_id'];
    $pass=$_POST['password'];
    foreach ($_FILES["pictures"]["error"] as $key => $error) { //used for multiple uploads
        if ($error == UPLOAD_ERR_OK) {
            $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
            }
    }
    $size = getimagesize($tmp_name);
    $width = $size[0]; // get width of the image
    $height = $size[1]; //get height of the image
    $type = $size[2]; //get type of the image
    $mime_type = $size['mime']; //get MIME of the image
    if(!$data = addslashes(@fread(@fopen($tmp_name, "r"), @filesize($tmp_name)))){
        die("'n<BR>Cannot read temp file: $tmp_file"); 
    } 
    $link = mysql_connect($server, $login, $s_password);
    if (!$link) {
        die("'n<BR>Could not connect $server:" .  mysql_error());
    }
    $db_selected = mysql_select_db("test");
    if (!$db_selected) {
        die ("'n<BR>Can''t use Table : $db_selected" . mysql_error());
    }
    $query   = "INSERT INTO image_data ";
    $query  .= " (hotel_name,street_name,city,state,country,zipcode,phone_number,fax,email_id,password,image_type, image_width, image_height, image_data) ";
    $query  .= " values ";
    $query  .= " ('$hotel_name','$street_name','$city','$state','$country','$zipcode','$phone_number','$fax','$email_id','$pass','$mime_type', '$width', '$height', '$data') ";
    $result = mysql_query($query);
    if (!$result) {
        $message  = '<BR>Invalid query: ' . mysql_error() . "'n";
        die($message);
    }

    $image_id = mysql_insert_id() ;
    echo "'n<IMG SRC='"getimage.php?id=$image_id'" />";
    mysql_close($link);
    exit();
?>

此特定错误与您的代码无关。您不会首先登录MySQL。请确保您的帐户具有访问权限。

此外,整个脚本非常不安全。使用准备好的/参数化的查询来避免SQL注入攻击。您现在处理数据的方式可能根本不起作用。addslashes()是不够的。

请检查密码中是否有空格,尝试删除该

$s_password = "";

这个错误的含义正是它所说的。这不是你的PHP文件的问题,而是你实际上没有权限连接到你想要连接的MySQL服务器

  1. 您的MySQL服务器真的托管在localhost上吗
  2. 您想以root用户的身份连接到它吗
  3. 你真的想用一个空格作为密码吗

话虽如此,PHP文件也存在许多问题。最明显的是您正在使用mysql_*函数。这些不应在新代码中使用,因为它们不再得到维护,并且已被正式弃用。(请参见红框?)

您应该使用PDO或MySQLi。

第二个问题是,您极易受到SQL注入的攻击。这个可以通过对查询中包含的每个变量调用mysql_escape_string()来修复,但更好的解决方案是切换到PDO或MySQLi并调查准备好的语句。