php 在 mysql table 中插入数据

php insert data in mysql table

我创建了一个 php 函数来创建缩略图,它按预期工作,但现在我想将这个缩略图图像和一些数据插入我的 mysqli 数据 baze 中。我试图这样做,但它不起作用。我对 mysql 不好,需要帮助。

require 'config.php';

if(preg_match('/[.](jpg)$/', $filename)) {
    $im = imagecreatefromjpeg($path_to_image_directory . $filename);
} else if (preg_match('/[.](gif)$/', $filename)) {
    $im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png)$/', $filename)) {
    $im = imagecreatefrompng($path_to_image_directory . $filename);
}

$ox = imagesx($im);
$oy = imagesy($im);

$nx = $final_width_of_image;
$ny = floor($oy * ($final_width_of_image / $ox));

$nm = imagecreatetruecolor($nx, $ny);

imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);

if(!file_exists($path_to_thumbs_directory)) {
  if(!mkdir($path_to_thumbs_directory)) {
       die("There was a problem. Please try again!");
  } 
   }
imagejpeg($nm, $path_to_thumbs_directory . $filename);
$host = 'localhost';
$user = 'timoleon_pandess';
$pass = 'pass';
mysql_connect($host, $user, $pass);
mysql_select_db('timoleon_pandessia');

$insert_path="INSERT INTO `ng17p_jshopping_products`(`product_id`,      `parent_id`, `product_ean`, `product_availability`, `product_template`,  `product_price`, `min_price`, `different_prices`, `product_weight`, `image`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7],[value-8],[value-9],[value-10])";

$var=mysql_query($inser_path);
?>

输入图片的数据库路径,例如:'\img\image2.png'

将 table 中的存储图像更改为类型 "longb"(这将触发 LONGBLOB)。 读这个 http://forums.mysql.com/read.php?20,17671,27914

为什么要在数据库中插入图片? 这样做

   Insert into table (`path`) Values ('image.png');

\image.png - 服务器上图像的路径。从数据库读取时,您会添加完整路径 \img\avatar\image.png

我不明白,你使用插入查询就像php中写的那样?该值需要封装在''中,例如:不是,[value-4]而是,'[value-4]'。我认为值将是一个变量$value4

  INSERT INTO `ng17p_jshopping_products` (`product_id`,    `parent_id`, `product_ean`, `product_availability`, `product_template`,  `product_price`, `min_price`, `different_prices`, `product_weight`, `image`) VALUES ('[value-1]','[value-2]','[value-3]','[value-4]','[value-5]','[value-6]','[value-7]','[value-8]','[value-9]','[value-10]')

您需要添加变量 将包含图像的文件名。

$filename=$path_to_thumbs_directory.$filename;

插入时

$query="INSERT INTO `ng17p_jshopping_products` (`product_id`,`parent_id`,`product_ean`, `product_availability`,`product_template`, `product_price`, `min_price`, `different_prices`, `product_weight`, `image`) VALUES ('[value-1]','[value-2]','[value-3]','[value-4]','[value-5]','[value-6]','[value-7]','[value-8]','[value-9]','$filename')";
$res=mysql_query($query);
if($res==1) { $id=mysql_insert_id;
$query="Select image from ng17p_jshopping_products where id='$id'";
$res=mysql_query($query);
$row=mysql_fetch_array($res);
echo 'It is OK <img src="'.$row['image'].'">'; 
} else echo 'Error';

如果您完成所有操作,并且尝试更改代码以使用 mysqli 时它会起作用,请阅读此处 http://php.net/manual/ru/book.mysqli.php

你必须使用 ob_start(); ob_get_clean();获取图像内容并将其放入您的数据库中。

首先在您的数据库中创建一个字段来存储图像内容:

ALTER TABLE `ng17p_jshopping_products` ADD `image_content` LONGBLOB NOT NULL ;

并像这样使用您的代码:

<?php
require 'config.php';

if(preg_match('/[.](jpg)$/', $filename)) {
    $im = imagecreatefromjpeg($path_to_image_directory . $filename);
} else if (preg_match('/[.](gif)$/', $filename)) {
    $im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png)$/', $filename)) {
    $im = imagecreatefrompng($path_to_image_directory . $filename);
}

$ox = imagesx($im);
$oy = imagesy($im);

$nx = $final_width_of_image;
$ny = floor($oy * ($final_width_of_image / $ox));

$nm = imagecreatetruecolor($nx, $ny);

imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);

if(!file_exists($path_to_thumbs_directory)) {
  if(!mkdir($path_to_thumbs_directory)) {
       die("There was a problem. Please try again!");
  } 
}

$host = 'localhost';
$user = 'timoleon_pandess';
$pass = 'pass';
mysql_connect($host, $user, $pass);
mysql_select_db('timoleon_pandessia');

ob_start();
imagejpeg($nm);
$image_content = mysql_real_escape_string(base64_encode(ob_get_clean()));
imagedestroy($nm);

$insert_path="INSERT INTO `ng17p_jshopping_products`(`product_id`, `parent_id`, `product_ean`, `product_availability`, `product_template`,  `product_price`, `min_price`, `different_prices`, `product_weight`, `image`,`image_content`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7],[value-8],[value-9],[value-10],'".$image_content."')";

$var=mysql_query($inser_path);
mysql_close();
?>

要显示您的图像,您必须使用像这样的函数:

<?php
function show_image($id) {
    $host = 'localhost';
    $user = 'timoleon_pandess';
    $pass = 'pass';
    mysql_connect($host, $user, $pass);
    mysql_select_db('timoleon_pandessia');

    // code to retrieve the image from database with $id id to $image_to_show variable
    // ....

    header('Content-Type: image/jpeg');
    echo stripslashes(base64_decode($image_to_show["image_content"]));
}
?>

这足以在项目与其图像之间建立联系。设置为 中的单元格,您将图像路径存储到 varchar(255) 而不是 blob 或其他类似情况(存储文件不是在硬盘上而是在数据库中)

$bd_host="localhost"; 
$bd_user="timoleon_pandess";
$bd_password="pass";
$bd_base="_base";

$con=mysql_connect($bd_host, $bd_user, $bd_password); 

if(!con) {
echo 'Ошибка, невозможно подключиться к серверу базы данных.'; 
exit; 
}

mysql_select_db($bd_base, $con); 
mysql_query('SET NAMES utf8');

$filename='/img/img.png'; // you need to catch it when you make and save thumbnail

$query="insert into ng17p_jshopping_products (`image`) Values ('$filename')";
mysql_query($query);

如果你的脚本没有制作图片或保存在需要的地方,这是另一个问题!