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.
 
Might be easier if you told us what problem you were seeing - other than the obvious mis-assigment at the beginning
Code:
    $username = $_POST['school_type_nursery'];
    $pass = [COLOR=red]$_POST['school_type_primary'];[/color]
    $first_name = [COLOR=green]$_POST['school_type_secondary'];[/color]
    $surname = [COLOR=blue]$_POST['school_type_nursery'];[/color]
    $part = [COLOR=red]$_POST['school_type_primary'];[/color]

    $member_since = [COLOR=green]$_POST['school_type_secondary'];[/color]

    $phone = [COLOR=blue]$_POST['school_type_nursery'];[/color]

    $email = [COLOR=red]$_POST['school_type_primary'];[/color]

    $birthday = [COLOR=green]$_POST['school_type_secondary'];[/color]

    $local_area = [COLOR=blue]$_POST['school_type_nursery'];[/color]
    $image = $_POST['image'];


___________________________________________________________
If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
Steam Engine Prints
 
i note that you are not escaping the data before using the variables in the insert query.

other than that, I agree with johnwm
 
Thanks for pointing that out John... I havent even gotten as far as correcting those variables. My head is spinning trying to figure out whats wrong! I've even tried this little update/post a comment to the DB via an html form

Code:
<link rel="stylesheet" type="text/css" href="css/main.css">

<?php require_once('connection.php');?>	



				<?php
	   	if(isset($_POST['submit'])){			
 $name = $_POST['name'];
    $comment = $_POST['comment'];
    $title = $_POST['title'];





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`.`forum` (
`id` ,
`name` ,
`comment` ,
`date` ,
`title`
)
'$name', '$comment', NOW( ), '$title' 
)"; 
$result=mysql_query($query);
}

   

    ?>

For the life of me its just not updating the DB. It just returns a blank screen and when I log into PHPMyAdmin, the DB is not updated. Any help would be really appreciated please geniuses.. :(
 
Hi Guys,

Thank you for responding firstly. I have corrected the mis-assignment at the biggining of the first script above (main question). Still, no luck. I simply get taken to the (fail.php) page indicating the DB hasnt been updated. Also the image has not been uploaded to the server. :( :(
 
change this to
Code:
$result=mysql_query($query) [red] or die (mysql_error())[/red];

i'm also confused by your query. which i think is malformed. try this instead

Code:
$query="INSERT INTO `mediamind1`.`forum` (`id`,`name`,`comment`,`date`,`title`) [red] VALUES ( [/red] '$name', '$comment', NOW(), '$title')";
 
Hi Jpadie,

I've just done what you said but still no luck.

It simply shows me a blank page the current php page. Also it dosn't update the DB neither. :(
 
Seems to me you have another error somewhere, but you PHP setup is not set to display errors. This would account for the blank screen.

If you have access to php.ini make sure display_errors is on and error_reporting is set to E_ALL.

Additionally, you are including a connection.php script which I can only assume is set to connect to the DB yet you ignore whatever it does, and proceed to make a connection yourself.

What is in that file?





----------------------------------
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.
 
to enable errors without php.ini editing

Code:
ini_set('display_errors', true);
error_reporting(E_ALL);

'course this won't cause parse errors to be shown at runtime...
 
Jpadie,

Still the same problem...

Here's how the code looks now by the way

Code:
<link rel="stylesheet" type="text/css" href="css/main.css">

<?php require_once('connection.php');


ini_set('display_errors', true);
error_reporting(E_ALL);


?>	



				<?php
	   	if(isset($_POST['submit'])){			
 $name = $_POST['name'];
    $comment = $_POST['comment'];
    $title = $_POST['title'];





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`.`forum` (`id`,`name`,`comment`,`date`,`title`)  VALUES (  '$name', '$comment', NOW(), '$title')";
$result=mysql_query($query)  or die (mysql_error());



}

   

    ?>
 
let's simplify a bit
Code:
<?php
//turn on error tracking
ini_set('display_errors', true);
error_reporting(E_ALL);

//get connection variables
require_once('connection.php');

//connect to database server
mysql_connect($hostname_Connection, $username_Connection, $password_Connection) or die("Unable to connect to db " . mysql_error());

//connect to database
mysql_select_db($database_Connection)
            or die("unable to select database. ". mysql_error());

//check if form submitted
if(isset($_POST['submit'])){
 //create escaped variables
 $name = mysql_real_escape_string($_POST['name']);
 $comment = mysql_real_escape_string($_POST['comment']);
 $title = mysql_real_escape_string($_POST['title']);
 
 //construct query
 $query="INSERT INTO `mediamind1`.`forum` (`id`,`name`,`comment`,`date`,`title`)  VALUES (  '$name', '$comment', NOW(), '$title')";

 //run query
 $result=mysql_query($query);
 if (!$result) { die (mysql_error()); }
 
 //success
 echo "transaction successful";
} else {
 echo 'submit button not pressed';
 echo "<hr/> post variables were <br/><pre>" . print_r($_POST, true);
}
 
Hi Jpadie

I had this:

submit button not pressed post variables were

Array
(
[name] => Uvi
[title] => Testing
[comment] => Geniuous
[button] => Submit
)
 
You may need to look at your SQL statement which has 5 field names and only 4 variables.

___________________________________________________________
If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
Steam Engine Prints
 
The ID field is an auto increment. I've even tried the NULL insertion for it but still no luck. No updating DB
 
good point john!

Code:
$query="INSERT INTO `mediamind1`.`forum` (`id`,`name`,`comment`,`date`,`title`)  VALUES (  NULL, '$name', '$comment', NOW(), '$title')";

the one thing you can't get here is 'nothing'. an error or other feedback will be given by this code or by the server. if you can't see the feedback then check in your php error logs.
 
Hi Justin,

Still nothing, I had already tried the NULL. Do you know how I cant check my Php error logs? very weird problem.
 
can you post the contents of connection.php

look in your php.ini for the location of your error log. and whilst you're at it, turn on the error_display and error_reporting. don't forget to restart your webserver after making the changes.
 
altered slightly (password and username) here is the content of my connection.php

Code:
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_Connection = "88.217.344.134";
$database_Connection = "med1";
$username_Connection = "med1";
$password_Connection = "med007";
$Connection = mysql_pconnect($hostname_Connection, $username_Connection, $password_Connection) or die(mysql_error());
?>

Thanks
 
why do you connect to the db server in this script and then connect again in the global script?

not that it is likely to make any difference.

what is the contents of your error log?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top