Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Uploading Image and update DB at Once 3

Status
Not open for further replies.

WilliamMute007

Programmer
Sep 30, 2007
116
GB
Hi all,

am really really baffled about this script which seems correct to me but for some reason doesnt do what its suppose to. Any help is MUCH appreciated

The code is suppose to take info from a form username, name, surname AND a thumbnail image of the user and store the details in the DB and upload the image to my designated path. see code below

Code:
<?php require_once('../connection.php');?>	

<?php
   



    $username = $_POST['school_type_nursery'];
    $pass = $_POST['school_type_primary'];
    $first_name = $_POST['school_type_secondary'];
    $surname = $_POST['school_type_nursery'];
    $part = $_POST['school_type_primary'];
    $member_since = $_POST['school_type_secondary'];
    $phone = $_POST['school_type_nursery'];
    $email = $_POST['school_type_primary'];
    $birthday = $_POST['school_type_secondary'];
    $local_area = $_POST['school_type_nursery'];
    $image = $_POST['image'];



  print_r($_POST); //debug the incoming vars
    
	 if(isset($_POST['Submit']))    {   
        mysql_pconnect($hostname_Connection, $username_Connection, $password_Connection) 
            or die("Unable to connect to db " . mysql_error());
        mysql_select_db($database_Connection)
            or die("unable to select database. ". mysql_error());
        
        $query = 
		
		 "INSERT INTO `mediamind1`.`choir_list` (
`id` ,
`username` ,
`pass` ,
`first_name` ,
`surname` ,
`part_singing` ,
`picture` ,
`date_joined` ,
`contact_number` ,
`email_address` ,
`birthday` ,
`local_area`
)
VALUES (
NULL , '$username', '$pass', '$first_name', '$surname', '$part', '$image', '$member_since', '$phone', '$email', '$birthday', '$local_area')";

        mysql_query ($query)
            or die("There was a failure putting the required destination into the database, please try again ".mysql_error());
			die("<META http-equiv=\"Refresh\" content=\"0;url=/../index.php\">");
    }
       else
       {
		die("<META http-equiv=\"Refresh\" content=\"0;url=fail.php\">");

       }

   

   
   
   
   
    $id = $keyword;

	$imagePath = "thumbs/";

//--- Check against the max file size

	$size = basename($_FILES['image']['size']);
	//die("siz".$size);
	$maxSize = round(2 * 1024 * 1024);	//2mb

	if($size > $maxSize) {
		echo "The file is to large to upload<br />";
		echo "<br />";
		echo "Your File Size: ".$size." (bytes)<br />";
		echo "Max File Size: ".$maxSize." (bytes)<br />";
		echo "<br />";
		echo '<a href="upload_school_badge.php" >Press Here</a> to return to the upload page<br />';
		die("");
	}
	


//---  This gets all the other information from the form

	$pic=($_FILES['image']['name']);

	
	
	

//---   Check the file Suffix / Length (.jpg OR .gif AND )


		if (strlen($pic) < 5){
			die ("File String Appears to be to short (Less than 5 characters)");
		}
		$FileSuffix = substr( $pic, strlen($pic) - 4 );
		if ($FileSuffix != ".jpg" and $FileSuffix != ".gif"){
			die("we do not support that type of file, We Only support JPG or GIF");
		}

//---  Ensure a something has been uploaded

	if(	$pic == ""){
		?>
		 	No file was selected to be uploaded<br />
			<a href="upload.php">Please Try Again</a>
		<?PHP
		
		 die("");
	}
//--- Connect to the database

	mysql_pconnect($hostname_Connection, $username_Connection, $password_Connection) 
            or die("Unable to connect to db " . mysql_error());
        mysql_select_db($database_Connection)
            or die("unable to select database. ". mysql_error());
	
	
	
	

//--- Upload the file

	//Create target Path
	$target = $imagePath.basename( $_FILES['image']['name']);

	// Ensure the file doesn't exist
	if(	file_exists($target)){

		echo "That file already exists";
		echo '<a href="upload_school_badge.php">Please Try Again</a>';
 		die("");
	}

	if(move_uploaded_file($_FILES['image']['tmp_name'], $target))
	{
		//Tells you if its all ok
		echo "<h2>The file has succesfully been uploaded</h2>";
		echo "<br />";
		echo "<a href='../index.php'>Go to the Homepage</a>";

		$filename = $target;
		$size = getimagesize($filename);
		$width	=  $size[0];
		$height	=  $size[1];

//---  Writes the information to the database
		//mysql_query("INSERT INTO tblPic (picname,gallery,width,height,userID) VALUES('$pic','$galleryName','$width','$height','$userID') ") or die(mysql_error());  


//---  Generate A Master Thumb of the image, (To Save Memory)
		if(true){
			$startX 		= 0;
			$startY 		= 0;
	
			// image to crop
			$image = $imagePath.$pic;		
			// dest image that is generated (either Thumbs Or Main)
			$dest_image = $imagePath.'thumbs/'.$pic;
	
			// get master image dimensions
			$ims = getimagesize($image);

			//calc new width / height to fit in the view port
			$viewWidth = 200;
			$viewHeight = 200;
			$masterWidth = $ims[0];
			$masterHeight = $ims[1];
		
			//Calc Master image size to fit in the View Port
			$ratioWidth = $masterWidth / $viewWidth;
			$ratioHeight = $masterHeight / $viewHeight;
		
			if( $ratioWidth < 1 && $ratioHeight < 1){
				$ratioWidth = 1;
				$ratioHeight = 1;
			}
		
			if($ratioWidth > $ratioHeight){
				$imgWidth = $masterWidth / $ratioWidth;
				$imgHeight = $masterHeight / $ratioWidth;
			} else {
				$imgWidth = $masterWidth / $ratioHeight;
				$imgHeight = $masterHeight / $ratioHeight;
			}
			// Create / resize etc
		
			$img = imagecreatetruecolor($imgWidth,$imgHeight);
			$org_img = imagecreatefromjpeg($image);
			imagecopyresampled($img,$org_img,0,0,0,0,$imgWidth,$imgHeight,$ims[0], $ims[1]);
			imagejpeg($img,$dest_image,100);

			?>
			<img src="<?php echo $dest_image; ?>" />
			<?php

		}
		
	} else {
		//Gives and error if its not
		echo "Sorry, there was a problem uploading your file.";
	}
		
?>

Thanks for all your possible help.
 
I cant acces my error log as I beleive only the Website hosting company have acccess to that. I only have access to the site basic control panel and PhyMyadmin. I am suspecting that It might have do with the hosting company dodgy server? I am going to try to upload the whole thing to another server and setup a new DB on that server and try. give me another 10 mins ish pls

Thanks for all your help so far
 
Hi,

I've tried moving it to an entirely different server but still the same problem. I get this

Code:
submit button not pressed post variables were

Array
(
    [name] => Justin
    [title] => is a Billionaire
    [comment] => and a Genius
    [button] => Submit
)

And DB was still not updated. ahh :(
 
well there you go, that's the problem solved. the button is called 'button' in your form but you are testing for 'submit' in your conditional.
 
Hurray! almost there! I not got the 'transaction successful' message but DB was still not updated.. :(
 
Sorry, Stupid me! I was using the wrong connection.php file. I have rectify that now and now I get

Code:
INSERT command denied to user 'rooncom'@'214.172.215.163' for table 'forum'

Would you happen to know the reason please?
 
Self explanatory really. the user rooncom@214.172.215.163 does not have permissions to perform insert commands.

Check your permissions in MYSQL and make sure the user from that location has the correct permissions.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I've checked it, I can insert via the Phpmyadmin fine, login in with the same username and password as I am using in my connection.php script.

This error is very strange, have never had that error before. Do you think it can be caused by anything else?
 
it is permissions. as vacunita said

i suspect PDO is providing a fully qualified IP address to the database.

go to phpmyadmin and click on privileges
make sure that your username is granted privileges from all hosts. if not edit the entry and set to all hosts (almost at the bottom of the page.
 
It is permission related. MYSQL is very picky of where it lets it users connect from. You need to specify the host from which the user is allowed to connect from.

Make sure the privileges are granted to all hosts as jpadie suggested, or at the very least from that particular IP address.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Now that the problem of the submit button has be resolve, I've moved the site back to the origianl server which doesnt have the Permission problem and hence works perfectly. However, the Original problem which I reported on this post (post one) still persist. Ability to upload the file in the form and sent the other information to a Database table. Basically, its a form for registration that also allows users to upload a picture of themselves. Upon submission, the content of the form is populated to a DB TOGETHER with their 'image' name and the image is then uploaded to a folder on the server.

Please help with this guys, I know you have been extremely helpful to which am very thankful.

Thanks again guys
 
william,

we really can't help you without some understanding of the errors you are getting. you need to get hold of the errors that are being generated. if you can't obtain these yourself please ask your host either to enable the relevant settings for you or to send you the logs.
 
Hi Justin,

Thanks for your effort. Played about with the script abit now am getting


Notice: Undefined index: birthdayy in /home/fhlinux153/m/vohc.co.uk/user/htdocs/vohc/new_member_form_process.php on line 32

Notice: Undefined index: image in /home/fhlinux153/m/vohc.co.uk/user/htdocs/vohc/new_member_form_process.php on line 34
Array ( [username] => Ui999zcz [pass] => 1234zxc [first_name] => Ui [surname] => William [part] => Sad [member_since] => 100000 [phone] => 999sdsd => help.com [birthday] => uuu [local_area] => Abby [submit] => Submit ) Column count doesn't match value count at row 1
 
Ok guys, seems to be working now after I played around with the script a little. One problem left which is prooving to be treaky. DB is now updating and the file is being uploaded to a directory specified. The only problem is that I want to input the name of the image being uploaded as part of my DB update. It seems because the field type is 'file', the PHP script is not capturing the 'file' field name. Hope am making it clear?

In simple term,

A form

Name: ----
Surname ----
your picture ------ [Browse Button]

Now as part of the updating the DB with the Name, Surname, I also want to Update the DB with the name of their picture.

Currently, I capture that Post like this

Code:
$image = mysql_real_escape_string($_POST['image']);

and I get the error

Undefined index: image in /home/fhlinux153/m/vohc.co.uk/user/htdocs/vohc/new_member_form_process.php on line 34

Any Help Please?
 
Guys, am really grateful for ALL your help! I've got it all working now finally with your supports. Am grateful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top