Jump to content

[SOLVED] Help with this please!


Trium918

Recommended Posts

I have two database tables. The first table is

called members_info and the second table is called

members_images. Members_info is used to log the user

in. After the user is logged in, there is a link on the user's

profile page called add photos. There are three columns inside

of the members_images table: images_id, members_id, and images_name;

 

From what I explain, could someone tell me why the code below

is not entering the members' id into the members_id column?

 

Note: Please ask questions or for more code!

 

<?php
// if ok, put in db
// if ok, put in db
$image_name = $path_big.$PhotoName;

// This isn't working also
$member_id = mysql_insert_id();

$result = mysql_query("INSERT INTO members_images(images_id,members_id,images_name) 
   VALUES(NULL,'$member_id','$image_name')") or
die("No file was entered in the database!");
?>

Link to comment
https://forums.phpfreaks.com/topic/53133-solved-help-with-this-please/
Share on other sites

// This isn't working also
$member_id = mysql_insert_id();

 

Where is the insert statement for that, more code is needed. Especially of members_id = 0, that means that the function is not work properly.

 

Assuming you are talking about this!

<?php
// if ok, put in db
   // if ok, put in db
   $image_name = $path_big.$PhotoFileName;
   $member_id = mysql_insert_id();
   $result = mysql_query("INSERT INTO members_images(images_id,members_id,images_name) 
   				VALUES(NULL,'$member_id','$image_name')") or
    die("No file was entered in the database!");
?>

Is this all happening in one script? mysql_insert_id() will only return the last value of an insert added with the current connection... if you've needed to open a new connection for the above query mysql_insert_id() will return 0 (false).

 

An easier way would be to store the members_id in the $_SESSION array when you log the member in, then use that in your query. eg;

 

<?php

  session_start();
  $image_name = $path_big.$PhotoName; // <-- no idea where your defining these variables?


  if ($result = mysql_query("INSERT INTO members_images (members_id,images_name) VALUES ('{$_SESSION['member_id']}','$image_name')")) {
    echo "Success";
  } else {
    echo "Query failed..." . mysql_error();
  }

?>

Is this all happening in one script? mysql_insert_id() will only return the last value of an insert added with the current connection... if you've needed to open a new connection for the above query mysql_insert_id() will return 0 (false).

 

An easier way would be to store the members_id in the $_SESSION array when you log the member in, then use that in your query. eg;

 

<?php

  session_start();
  $image_name = $path_big.$PhotoName; // <-- no idea where your defining these variables?


  if ($result = mysql_query("INSERT INTO members_images (members_id,images_name) VALUES ('{$_SESSION['member_id']}','$image_name')")) {
    echo "Success";
  } else {
    echo "Query failed..." . mysql_error();
  }

?>

 

Here is the entire script. Everything else is working.

<?php
session_start();
//include function files for this application
require_once("_container_fns.php");

if($_POST['submit']) {

$PhotoFileName = $_FILES["userfile"]["name"]; // get client side file name
$PhotoFileTemp = $_FILES["userfile"]["tmp_name"];
$PhotoFileType = $_FILES["userfile"]["type"];
$PhotoFileSize = $_FILES['userfile']['size'];

//check if you have selected a file.
if(!is_uploaded_file($PhotoFileTemp)){
echo "Error: Please select a file to upload!.";
    exit(); //exit the script and don't do anything else.
}

$TrustedFile = array("jpg", "JPEG", "JPG", "gif");

//get the file extension.
$FileNameParts = explode(".", $PhotoFileName);
$FileExtension = $FileNameParts [count($FileNameParts)-1]; // part behind last dot

if ((!in_array($FileExtension,$TrustedFile))) {
      echo "Wrong file extension.";
      exit();
}

if (in_array($FileExtension, $TrustedFile)) {

if ($FileExtension != "jpg" && $FileExtension != "JPEG" && 
$FileExtension != "JPG"
&& $FileExtension != "gif"){ die ("Choose a JPG or GIF for the photo"); }
//else { echo "Thats it!";}

//the new width of the resized image.
$img_thumb_width = 100; // in pixels

//create a random file name
$rand_name = md5(time());
$rand_name= rand(0,999999999);
//get the new width variable.
$ThumbWidth = $img_thumb_width;

$path_thumbs = "uploads/";
$path_big = "php_uploads/";

if ($PhotoFileSize == 0){	
die ("Sorry. The upload of $sPhotoFileName has failed.
	Search a photo smaller than 100K, using the button.");
}
if ($PhotoFileSize > 102400){	
die ("Sorry. The file $sPhotoFileName is larger than 100K.
	Advice: reduce the photo using a drawing tool.");
}

/*if($PhotoFileType == "image/pjpeg" || $PhotoFileType == "image/jpeg"){
       $new_img = imagecreatefromjpeg($PhotoFileTemp);
    }    
    elseif($PhotoFileType == "image/gif"){
       $new_img = imagecreatefromgif($PhotoFileTemp);
    }
    //list width and height and keep height ratio.
    list($width, $height) = getimagesize($PhotoFileTemp);
    $imgratio=$width/$height;
    if ($imgratio>1){
        $newwidth = $ThumbWidth;
        $newheight = $ThumbWidth/$imgratio;
    }else{
        $newheight = $ThumbWidth;
        $newwidth = $ThumbWidth*$imgratio;
     }
    //function for resize image.
    if (function_exists(imagecreatetruecolor)){
        $resized_img = imagecreatetruecolor($newwidth,$newheight);
    }else{
        die("Error: Please make sure you have GD library ver 2+");
    }

    imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    //save image
    imagejpeg ($resized_img,"$path_thumbs/$rand_name.$FileExtension");
    imagedestroy ($resized_img);
    imagedestroy ($new_img);*/



if (file_exists("$path_big" . $PhotoFileName)){
	 echo $PhotoFileName . " already exists. ";
}
else{
	 move_uploaded_file($PhotoFileTemp, "$path_big" . $PhotoFileName);
	 echo "Stored in: " . "$path_big" . $PhotoFileName;

   // if ok, put in db
   // if ok, put in db
   $image_name = $path_big.$PhotoFileName;
   $member_id = mysql_insert_id();
   $result = mysql_query("INSERT INTO members_images(images_id,members_id,images_name) 
   				VALUES(NULL,'$member_id','$image_name')") or
    die("No file was entered in the database!");
}	

}
else{ 
do_html_header("Problem:");
    echo "<p class=\"genmed\">That is not valid an image!  Please go back "
           ." and try again. Jpg or Gif format only!</p>";
    do_html_footer();
    exit;
}

}

?> 

We don't need the entire script. Read my post!

 

The simple fact of the matter is that mysql_insert_id() is returning false, because your last query is NOT in the same scope.

 

I posted a solution to this very problem.

 

Ok, I understand a little. I am logging in

with the user_name. Will it make a difference

if I was to change it to what you provided?

Throught out my entire site I mean.

 

<?php
// if they are in the database register the user id
   $valid_user = $user_name;
   $_SESSION['valid_user'] = $valid_user; 
?>

Will it make a different

if I was to change it to what you provided?

 

Just add the members_id to the $_SESSION array as well, you'll more then likely need both.

 

Like this you mean?

 

<?php
// if they are in the database register the user id
   $valid_user = $user_name;
   $_SESSION['valid_user']['members_id'] = $valid_user; 
?>

What? Like this...

 

<?php

   $_SESSION['valid_user'] = $valid_user;
   $_SESSION['members_id'] = $members_id; // you'll need to get $mebers_id from the database.

?>

 

Do you actually understand how your login works?

 

I am still learning. Only a couple of months under my belt!

Thanks.

Would it not just be easier to remove the mysql_insert_id() from the script? As it was already stated

mysql_insert_id() will only return the last value of an insert added with the current connection...
Is the mysql_insert_id() needed? Because unless you edit your code to include sessions then the result will always be false.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.