0

Hello i am having a problem with trying to create a table on the server every time a try to enter data into the database i get the message table already exists. I have moved the code to create the table on to its own page and only recall that page when the data is being entered into the data base the first time. the code i have used for the create table looks like this

$sql="CREATE TABLE `images_broad` (
			
			`broad1` varchar(100),
			`broad2` varchar(100)
		)";	
		mysql_query($sql) or die (mysql_error());

and the code i have used to update the database looks like this

<?php
		include "table_create.php";
		

			
		$sql="UPDATE images_broad SET broad1='$thumb_name'";
		$query = mysql_query($sql);
		?>
2
Contributors
32
Replies
33
Views
9 Years
Discussion Span
Last Post by nav33n
Featured Replies
  • 1

    Yes. It is possible to store the path of the file. The datatype is varchar as you have mentioned. Store the path, then display the image by retrieving the path from the table and use it in the image tag. [code=php] $query = "select path from table"; $result = mysql_query($query); … Read More

0

Why do you want to create a table from php anyway ? Everytime you run your script, it will include table_create.php and tries to create the table all the time. Check this link . Add 'if not exists' in your create table query.

0

i will check that now i thought it had something to do with the code being executed more than once. That is why i tried putting it on a different page and only tried to recall it when i wanted the table to be populated.

0

Since you are including the script which creates the table, whenever you include that script, it tries to create the table causing the error.

0

thanks for that. the code i have used should be putting the data into the database now and this code should be getting the data out of the database.

<?php
						
	         $sql= "SELECT * FROM images_broad";
		$query = mysql_query($sql);
	        $result = mysql_fetch_array($query) or die (mysql_error());
		echo $result ['$thumb_name'];
						
	     ?>

is this code correct because when i run it i do not get anything showing up on the pages

0

i tried changing the echo line to how you said but it still does not place anything on the page. i have set $thumb_name a variable which stores the image at first and this is used to display the image that has been created on the upload page. I want to then store this variable in the table i have created and then recall the data from the tablew on a different page.

When i use the select statement i get nothing out of the database and the code for the rest of the page does not complete so i end up with half a page. i have tried putting the or die statement at the end but it does not give me any error messages so i do not no were it is going wrong.

0

Hmm.. Print out your query ( print $sql; )and execute it in mysql console or phpmyadmin. See if it returns any row. If it doesn't, then the problem is not with the query and there are no records. If it returns an error, post that error.

0

when i have put the print statement at the end of the update statement it has given me this line underneath the upload button.

UPDATE images_broad SET broad1='..'

the update code now looks like this

$sql="UPDATE images_broad SET broad1='.$thumb_name.'";
		$query = mysql_query($sql);
		print $sql;

is the print in the correct place?

0

once the upload section has been ran it then gives this line

UPDATE images_broad SET broad1='image/thumbs/thumb_1206960238.gif'

to me it looks like the image is being placed in the table but why would it not be displayed on the page i have asked it to?

the code i use to display the image is

$sql= "SELECT broad1 FROM images_broad";
	     $query = mysql_query($sql)or die (mysql_error());
	     $result = mysql_fetch_array($query) or die (mysql_error());
	     echo $result ['$thumb_name'];

could it have something to do with the data types set in the create table section as i have set broad1 to be a varchar

0

First of all, you are updating the table without any condition. So, it will update all the records and set broad1 = image/thumbs/thumb_1206960238.gif. Secondly, its echo $result and not echo $result ['$thumb_name'];

0

this is probably getting on your nerves now but it still does not display the image from the database i have now put an if statement on the update button so it will only try to put something in the database once the upload button is pressed.

the code for the display now looks like this

$sql= "SELECT * FROM images_broad";
	     $query = mysql_query($sql)or die (mysql_error());
	     $result = mysql_fetch_array($query) or die (mysql_error());
	     echo $result ['broad1'];

on the page where it is supposed to display the image it gets to this section of the code and stops, there should be more text after this but the page does not fully display. it is not printing out any errors.

0

Hmm.. Did you execute the query select * from images_broad in mysql console ? Did you get any output ?

0

i have just tried the print command on the section of code for displaying the image and this again came back with nothing. it loads the top part of the page but then dies when it gets to the php section.

0

does mysql console only work if you are not using a remote server as all the files which i am creating are all held on a remote server. this might be a stupid question but i have never heard of this i hae just looked it up and it seems it will only work if a remote server is not being used.

0

Well, I hope you have phpmyadmin atleast ? If you do, you can execute your query in phpmyadmin. You can use sqlyog to connect to remote server actually.

0

no there is no phpadmin. i did not set the original site up i have just been asked to fix the problems the last person caused before he left. he did not do anything correct and all work i have had to fix of his up to now has all been half done.

0

hmm.. In that case, try a simple script like this one.

$query = "select * from images_broad";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0){
 echo "There is something!"; //prints if there are more than 0 records
} else {
echo "There is nothing !"; // prints if there is nothing in the table
}

Tellme what it prints.

0

it has come back with there is nothing. when the image is uploaded it sets broad1 to the following

UPDATE images_broad SET broad1='image/thumbs/thumb_1206968603.gif'

is this not putting it into the database.
i have changed the update to insert to see if that changes anything and it giaves the same result.

could it have something to do with the files path?

0

UPDATE images_broad SET broad1='image/thumbs/thumb_1206968603.gif'

That wouldn't put anything to the table, since your table has no records. Try this.

$query = "insert into images_broad (broad1) values ('image/thumbs/thumb_1206968603.gif'); 
mysql_query($query);

Then execute the above script to check if a record exists or not. I am sure it will say, "There is something!" !

0

i have just checked what files are on the server and the images are being stored on the server before they are added to the table. would it be possible to then recall the file from the server without the use of mysql and having to create a table to recall the data from. it is possible then how they are saved would have to be changed as at the minute the are given a unique time stamp as the file name and this would be impossible to know unless you checked the file on the server. Or could this be left and it recalled in a different way than asking for the full file name.

0
<?PHP

$folder = "image/thumbs";
$handle = opendir($folder);

# Making an array containing the files in the current directory:
while ($file = readdir($handle))
{
    $files[] = $file;
}
closedir($handle);

#echo the files
foreach ($files as $file) {
    echo "<a href=$folder$file>$file</a>"."<br />";
}
?>

this was the code used to get the list of files but when you select one it says that the file is not on the server. do not know how it can say that when it is pulling the list from the server.

this method would not for this section but it will work for another area as it would be possible to delete the files from here therefore emptying any files which had benn previously uploaded. i will have to go back to trying to get it to store the file on the server using mysql and then recalling it from the server when it is needed.

0

Dude, try out a simple query to insert a record to the table. When you upload an image, add the path to the table. Do you upload an image through a script or manually ? Check this example. http://www.tizag.com/phpT/fileupload.php $target_path will be the path where the image is gonna be stored. Save it in the table. If that doesn't work, you have to post your latest complete code.

0

is it possible to store the path of the file on the server in a mysql table. if so would the data type for the table be set as a varchar or would this have to be change so it knows that it is storing the path of the file and not just a list of words?

1

Yes. It is possible to store the path of the file. The datatype is varchar as you have mentioned. Store the path, then display the image by retrieving the path from the table and use it in the image tag.

$query = "select path from table";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$path = $row['image_path'];
echo "<img src = '$path'>"; //list all the images 
}
Votes + Comments
is very helpful and gives good feedback
0

so i have set $target_path to $thumb_name and then stored the $target_path in the mysql table when i ran this it had the same result as before it stops the preview page from working. do you want me to upload all the code for the the upload page here?

i have used script to upload the images and on the page where the images are uploaded i have used an iframe for the upload so it does not reload the page when the image is uploaded.

0

here is the code for the upload page

<?php
 //define a maxim size for the uploaded images
 define ("MAX_SIZE","500"); 
 // define the width and height for the thumbnail
 // note that theese dimmensions are considered the maximum dimmension and are not fixed, 
 // because we have to keep the image ratio intact or it will be deformed
 define ("WIDTH","200"); 
 define ("HEIGHT","200"); 

  // this is the function that will create the thumbnail image from the uploaded image
 // the resize will be done considering the width and height defined, but without deforming the image
 function make_thumb($img_name,$filename,$new_w,$new_h)
 {
 	//get image extension.
 	$ext=getExtension($img_name);
 	//creates the new image using the appropriate function from gd library
 	if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
 		$src_img=imagecreatefromjpeg($img_name);

  	if(!strcmp("png",$ext))
 		$src_img=imagecreatefrompng($img_name);
	
	if(!strcmp("gif",$ext))
		$src_img=imagecreatefromgif($img_name);

 	 	//gets the dimmensions of the image
 	$old_x=imageSX($src_img);
 	$old_y=imageSY($src_img);

 	 // next we will calculate the new dimmensions for the thumbnail image
 	// the next steps will be taken: 
 	// 	1. calculate the ratio by dividing the old dimmensions with the new ones
 	//	2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable
 	//		and the height will be calculated so the image ratio will not change
 	//	3. otherwise we will use the height ratio for the image
 	// as a result, only one of the dimmensions will be from the fixed ones
 	$ratio1=$old_x/$new_w;
 	$ratio2=$old_y/$new_h;
 	if($ratio1>$ratio2)	{
 		$thumb_w=$new_w;
 		$thumb_h=$old_y/$ratio1;
 	}
 	else	{
 		$thumb_h=$new_h;
 		$thumb_w=$old_x/$ratio2;
 	}

  	// we create a new image with the new dimmensions
 	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);

 	// resize the big image to the new created one
 	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

 	// output the created image to the file. Now we will have the thumbnail into the file named by $filename
 	if(!strcmp("png",$ext))
 		imagepng($dst_img,$filename); 
 	else
 		imagejpeg($dst_img,$filename);
		
	if (!strcmp("gif",$ext))
		imagegif($dst_img,$filename); 

  	//destroys source and destination images. 
 	imagedestroy($dst_img); 
 	imagedestroy($src_img); 
 }

 // This function reads the extension of the file. 
 // It is used to determine if the file is an image by checking the extension. 
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }
  // This variable is used as a flag. The value is initialized with 0 (meaning no error found) 
 //and it will be changed to 1 if an error occures. If the error occures the file will not be uploaded.
 $errors=0;
 // checks if the form has been submitted
 if(isset($_POST['Submit']))
 {
 //reads the name of the file the user submitted for uploading
 	$image=$_FILES['image']['name'];
 	// if it is not empty
 	if ($image) 
 	{
 		// get the original name of the file from the clients machine
 		$filename = stripslashes($_FILES['image']['name']);
 		
 		// get the extension of the file in a lower case format
 	 	$extension = getExtension($filename);
 		$extension = strtolower($extension);
 		// if it is not a known extension, we will suppose it is an error, print an error message 
 		//and will not upload the file, otherwise we continue
 		if (($extension != "jpg")  && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))	
 		{
 			echo '<h1>Unknown extension!</h1>';
 			$errors=1;
 		}
 		else
 		{
 			// get the size of the image in bytes
 			// $_FILES[\'image\'][\'tmp_name\'] is the temporary filename of the file in which 
			//the uploaded file was stored on the server
 			$size=getimagesize($_FILES['image']['tmp_name']);
 			$sizekb=filesize($_FILES['image']['tmp_name']);

 			//compare the size with the maxim size we defined and print error if bigger
 			if ($sizekb > MAX_SIZE*1024)
 			{
 				echo '<h1>You have exceeded the size limit!</h1>';
 				$errors=1;
 			}

  			//we will give an unique name, for example the time in unix time format
 			$image_name=time().'.'.$extension;
 			//the new name will be containing the full path where will be stored (images folder)
 		 	$newname="image/".$image_name;
 			$copied = copy($_FILES['image']['tmp_name'], $newname);
 			//we verify if the image has been uploaded, and print error instead
 			if (!$copied) 
 			{
 				echo '<h1>Copy unsuccessfull!</h1>';
 				$errors=1;
 			}
 			else
 			{
 				// the new thumbnail image will be placed in images/thumbs/ folder
 				$thumb_name='image/thumbs/thumb_'.$image_name;
 				// call the function that will create the thumbnail. The function will get as parameters 
 				//the image name, the thumbnail name and the width and height desired for the thumbnail
 				$thumb=make_thumb($newname,$thumb_name,WIDTH,HEIGHT);
 			}}	}}
			

include "db.php";

$con = mysql_pconnect("$xxx","$xxxxx","$xxxxx") 
	or die ("QUERY ERROR: ".mysql_error());

$db = mysql_select_db($xxxxx, $xxxxx)
	or die("QUERY ERROR: ".mysql_error());
			



  //If no errors registred, print the success message and show the thumbnail image created
 if(isset($_POST['Submit']) && !$errors) 
 {
 	echo "<h5>Thumbnail created Successfully!</h5>";
 	echo '<img src="'.$thumb_name.'">';
 } 
 
 ?> 
</p>
 	<form name="newad" method="post" enctype="multipart/form-data"  action="">

		<input type="file" name="image"  ></td></tr>
		<input name="Submit" type="submit" id="image1" value="Upload image" ></td></tr>
	
 	</form>
	<?php

		include "table_create.php";
		$target_path=$thumb_name;
		if(isset($_POST['Submit']) && !$errors)
			{	
				$sql="INSERT INTO images_broad (broad1) VALUE ('$target_path')";
				$query = mysql_query($sql);
				print $sql;
			}
		?>
 

</body>
</html>
0

Okay ! some mistakes.
1. Insert into tablename (columnname) [b]values[/b] ('value'); Anyway, I dont know how your function make_thumb work, but this works.

<?php
 
include "db.php";
$con = mysql_pconnect("$xxx","$xxxxx","$xxxxx") or die ("QUERY ERROR: ".mysql_error());
$db = mysql_select_db($xxxxx, $xxxxx) or die("QUERY ERROR: ".mysql_error());
 //define a maxim size for the uploaded images
 define ("MAX_SIZE","500"); 
 // define the width and height for the thumbnail
 // note that theese dimmensions are considered the maximum dimmension and are not fixed, 
 // because we have to keep the image ratio intact or it will be deformed
 define ("WIDTH","200"); 
 define ("HEIGHT","200"); 

  // this is the function that will create the thumbnail image from the uploaded image
 // the resize will be done considering the width and height defined, but without deforming the image
 function make_thumb($img_name,$filename,$new_w,$new_h)
 {
 	//get image extension.
 	$ext=getExtension($img_name);
 	//creates the new image using the appropriate function from gd library
 	if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
 		$src_img=imagecreatefromjpeg($img_name);

  	if(!strcmp("png",$ext))
 		$src_img=imagecreatefrompng($img_name);
	
	if(!strcmp("gif",$ext))
		$src_img=imagecreatefromgif($img_name);

 	 	//gets the dimmensions of the image
 	$old_x=imageSX($src_img);
 	$old_y=imageSY($src_img);

 	 // next we will calculate the new dimmensions for the thumbnail image
 	// the next steps will be taken: 
 	// 	1. calculate the ratio by dividing the old dimmensions with the new ones
 	//	2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable
 	//		and the height will be calculated so the image ratio will not change
 	//	3. otherwise we will use the height ratio for the image
 	// as a result, only one of the dimmensions will be from the fixed ones
 	$ratio1=$old_x/$new_w;
 	$ratio2=$old_y/$new_h;
 	if($ratio1>$ratio2)	{
 		$thumb_w=$new_w;
 		$thumb_h=$old_y/$ratio1;
 	}
 	else	{
 		$thumb_h=$new_h;
 		$thumb_w=$old_x/$ratio2;
 	}

  	// we create a new image with the new dimmensions
 	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
	// resize the big image to the new created one
 	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

 	// output the created image to the file. Now we will have the thumbnail into the file named by $filename
 	if(!strcmp("png",$ext))
 		imagepng($dst_img,$filename); 
 	else
 		imagejpeg($dst_img,$filename);
		
	if (!strcmp("gif",$ext))
		imagegif($dst_img,$filename); 

  	//destroys source and destination images. 
 	imagedestroy($dst_img); 
 	imagedestroy($src_img); 
 }

 // This function reads the extension of the file. 
 // It is used to determine if the file is an image by checking the extension. 
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }
  // This variable is used as a flag. The value is initialized with 0 (meaning no error found) 
 //and it will be changed to 1 if an error occures. If the error occures the file will not be uploaded.
 $errors=0;
 // checks if the form has been submitted
 if(isset($_POST['Submit']))
 {
 //reads the name of the file the user submitted for uploading
 	$image=$_FILES['image']['name'];
 	// if it is not empty
 	if ($image) 
 	{
 		// get the original name of the file from the clients machine
 		$filename = stripslashes($_FILES['image']['name']);
 		
 		// get the extension of the file in a lower case format
 	 	$extension = getExtension($filename);
 		$extension = strtolower($extension);
 		// if it is not a known extension, we will suppose it is an error, print an error message 
 		//and will not upload the file, otherwise we continue
 		if (($extension != "jpg")  && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))	
 		{
 			echo '<h1>Unknown extension!</h1>';
 			$errors=1;
 		}
 		else
 		{
 			// get the size of the image in bytes
 			// $_FILES[\'image\'][\'tmp_name\'] is the temporary filename of the file in which 
			//the uploaded file was stored on the server
 			$size=getimagesize($_FILES['image']['tmp_name']);
 			$sizekb=filesize($_FILES['image']['tmp_name']);

 			//compare the size with the maxim size we defined and print error if bigger
 			if ($sizekb > MAX_SIZE*1024)
 			{
 				echo '<h1>You have exceeded the size limit!</h1>';
 				$errors=1;
 			}

  			//we will give an unique name, for example the time in unix time format
 			$image_name=time().'.'.$extension;
 			//the new name will be containing the full path where will be stored (images folder)
 		 	$newname="image/".$image_name;
 		 	$newname2="image/thumbs/thumb_".$image_name;
 			$copied = copy($_FILES['image']['tmp_name'], $newname);
 			$copied = copy($_FILES['image']['tmp_name'], $newname2);
 				$sql="INSERT INTO images_broad (broad1) VALUES ('$newname')";
				$query = mysql_query($sql);
 			//we verify if the image has been uploaded, and print error instead
 			if (!$copied) {
 				echo '<h1>Copy unsuccessfull!</h1>';
 				$errors=1;
 			}
 			else
 			{
 				// the new thumbnail image will be placed in images/thumbs/ folder
 				$thumb_name=$newname2;
 				// call the function that will create the thumbnail. The function will get as parameters 
 				//the image name, the thumbnail name and the width and height desired for the thumbnail
 				$thumb=make_thumb($newname,$thumb_name,WIDTH,HEIGHT);
 			}
 		}	
 	}
 }
			
  //If no errors registred, print the success message and show the thumbnail image created
 if(isset($_POST['Submit']) && !$errors) 
 {
 	echo "<h5>Thumbnail created Successfully!</h5>";
 	echo '<img src="'.$thumb_name.'">';
 } 
 
 ?> 
</p>
 	<form name="newad" method="post" enctype="multipart/form-data"  action="">

		<input type="file" name="image"  ></td></tr>
		<input name="Submit" type="submit" id="image1" value="Upload image" ></td></tr>
	
</form>
</body>
</html>

:) Cheers.

0

thanks for that it looks like it is working now it saves the data to the database and it is showing on the preview page there is something there.

on the preview page it displays the file name can this be changed so it shows the picture and not the name or does the code only limit it to showing the file extension.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.