php - 将用户照片添加到数据库

标签 php mysql

我的问题是将图像的文件路径添加到数据库中的用户槽。这是一个示例,这是要选择照片的页面:

   require_once("script.php");   
   <form method="post" action='golf.php'>
<?php
 require_once("golf_phot.php");
?>
</div>
<input id="butt" type="submit" value="add"/>
</form>

以下是 Golf_photo.php 的内容:

$query="SELECT category_id FROM categories WHERE category_name='golf'";
$resultt=mysql_query($query);
$row=mysql_fetch_array($resultt);
$do=$row['category_id'];
$query2="SELECT photo FROM all_photos WHERE all_photos.category_id='$do'";
$result=mysql_query($query2); 
while ($row=mysql_fetch_array($result))
{
 $photo=$row['photo'];
 echo "<input type=\"checkbox\" name=\"addlist[]\" value=\"".$photo."\">"."<img    src=\"".$photo."\">";
echo "<br>";
echo "<br>";
echo "<br>";
}

我已经完成了 mysql 连接和数据库选择,我不会发布它,但它没有问题。以下是“script.php”的内容:

if(isset($_POST['addlist']))
{
$pics=$_POST['addlist'];
$n=count($pics);
//now, to open the database user_info, using the stored session

//make the session variable legit
if(isset($_SESSION['user']))
{
$user=$_SESSION['user'];
}
//get the user's id from the user_info table
$querya="SELECT * FROM user_info WHERE user_info.fb_id='$user'";
$result1=mysql_query($querya);
//now that we have the id, we can add the photos to 
//user_photos, using their id as a foreign key
if(mysql_num_rows($result1)>0){
$rowa=mysql_fetch_array($result1);}
$user_id=$rowa['USER_INFO_ID'];
//before we add them, we'll need to see whether they exist or not.
//adding time:
for($i=0;$i<$n;$i++){
$data=$pics[$i];
$queryb="SELECT * FROM user_photos";
$result3=mysql_query($queryb);
if(mysql_num_rows($result3)>0){
while($rowa=mysql_fetch_array($result3))
{
$data2=$rowa['USER_PHOTOS'];
if($data==$data2)
{ $var='exists';   }
else{
$queryb="INSERT INTO user_photos(USER_PHOTOS_ID,USER_INFO_ID,USER_PHOTOS) VALUES('NULL','".$user_id."','".$data."')";
$result2=mysql_query($queryb);
}
}
}
}

//选定的照片已添加到用户的图库中! } 用户数据变量都已设置,因为我在其他地方使用它们,并且它们已成功添加到数据库中,上面,我提供了一种查看照片是否已存在的方法,如果存在,我不会添加它(也许存在问题)那?)所以基本上,带有照片的页面被加载,用户检查他们想要的照片,然后将其发送到同一页面,其中“script.php”应该处理数据并将其添加到数据库中。没有照片添加到数据库中,我真的不知道这里出了什么问题。我恳请帮助,尽管这可能是一个简单的问题,如果您需要我澄清一些事情,请询问,同时,有人可以帮忙吗?提前致谢。

最佳答案

检查图片是否相同的一种解决方案是,对图片使用哈希算法来确定相同的图片。

即:(伪代码)

$hash=sha1_file('pic-filename');

$sql='select * from image_table where hash_col ='.$hash;

if(num_rows($sql)>0)
  //don't save pic.
else
  // save the pic and it's hash value.
  • hash_col 是图像表中的一列,用于获取特定图像的哈希值。

关于php - 将用户照片添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13326073/

相关文章:

php - 在 JavaScript 函数中传递 PHP 变量

Bindparam 中的 PHP MySQL PDO 语法错误

java - 没有为 LoginRequest 调用 responseListener (Android) (Java) (php)

php - Laravel 4 Restful Controller GET参数

php - 如何使用 JavaScript 填充 HTML 选择列表

php - 如何使用mysql基于月份选择数据

php - mysql查询通过检查另一表中的其他两个属性来选择一个表中的所有数据

mysql - 如何从 Redhat Linux 中完全删除 MySQL 8,以便安装 MySQL 5?

php - 如何获取本月记录?

php - JOIN 和 AND 后的订单查询