user1702837
user1702837

Reputation:

adding user photos to database

my question is on adding the file paths to images to a user's slot in a database. here is a sample, this is the page where the photos are to be chosen:

   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>

here are the contents of golf_phot.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>";
}

i already did the mysql connection and database selection, i won't post that but it has no issues. here are the contents of "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);
}
}
}
}

// selected photos added to user's gallery! } the user data variables are all set as i use them elsewhere and they are successfuly added to the database, above, i include a way to see if the photo already exists, i don't add it if it does(maybe there's an issue with that?) so basically, the page with photos is loaded, the user checks on the photos they want and then they send it to the same page, where "script.php" is supposed to process the data and add it to the database. no photos are added to the database, i really can't tell what's the issue here. i kindly ask for help, even though this is probably an easy question, if you need me to clarify something, kindly ask so, meanwhile, can anybody help? thanks in advance.

Upvotes: 0

Views: 107

Answers (2)

Good man
Good man

Reputation: 496

One solution for check the picture is same or not is, using a hash algorithm on the pictures to determined the same pics.

i.e: (pseudo code)

$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 is a column in your image table that get the hash value of the particular image.

Upvotes: 1

Vikas Umrao
Vikas Umrao

Reputation: 2615

Please Try this code in your script.php. hoping this may work for you.
I Have removed the condition if(mysql_num_rows($result3)>0){

Code:

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

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

    }
    }

Upvotes: 0

Related Questions