Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?



Help integrating php/mysql with existing html page.


37 replies to this topic - - - - -

#16 OP Brodel

    Neowinian UNSTOPPABLE

  • 5,877 posts
  • Joined: 25-July 04
  • Location: London UK, we rock on top like a toupee

Posted 17 April 2012 - 18:05

one last question...

I have the option to upload an image, however if nothing is uploaded I get a broken image icon in my post. Is there a way to echo the image if it's present but otherwise leave blank? This is what I have at the moment. I've looked at the !empty function but I'm not sure how to implement it or if it's the right way to do it in this case.

//Outputs the image and other data
echo "<h4></b> ".$info['title'] . "</h4>";
echo "<b><h6></b> ".$info['date']."</h6><br>";
echo "<p><img src=/blogimages/".$info['photo'] . "></p><br>";
echo "<b><p></b> ".$info['text'] . "<p><br>";
echo "<div class='blogspacer'></div>";



#17 Tjcool007

    Neowinian²

  • 103 posts
  • Joined: 27-April 11
  • Location: Belgium
  • OS: Windows 7 Pro x64, Ubuntu 13.04

Posted 18 April 2012 - 11:07

you could do something like

if(isset($info['photo']))
{
	echo "<p><img src='/blogimages/" . $info['photo'] . "' /></p><br />";
}
else
{
	echo "<p><img src='/blogimages/noimage.png' /></p><br />";
}

That would echo "noimage.png" instead if no image is there.
That's assuming the $info['photo'] var is empty when there's nothing uploaded.
Else you'll need a file_exists() call.

Also, some of your HTML in the post above is incorrect. You are closing the <b> before closing the tag inside, like here:
echo "<b><h6></b>" . $info['date'] . "</h6><br>";

Should probably be this:
echo "<b><h6>" . $info['date'] . "</h6></b><br>";


#18 OP Brodel

    Neowinian UNSTOPPABLE

  • 5,877 posts
  • Joined: 25-July 04
  • Location: London UK, we rock on top like a toupee

Posted 18 April 2012 - 12:05

Thanks, so I could just make a small noimage.png that is just the same colour as my background, right? Then there would technically be an image there, but it would look blank.

thanks for pointing out the mistake also :)

#19 The_Decryptor

    THE ALPHA CEPH!

  • 18,355 posts
  • Joined: 28-September 02
  • Location: Sol System
  • OS: WinLin X 10.9 Ill-tempered Badger

Posted 18 April 2012 - 12:18

Or you could just not write anything out, save loading a blank image from the server.

Edit: Also, the mysql_ API is deprecated (and for an old version of MySQL), if you're writing brand new code now either use the mysqli library, or PDO.

#20 threetonesun

    Neowinian ULTRAKILL

  • 11,301 posts
  • Joined: 26-February 02

Posted 18 April 2012 - 12:24

if you're trying to maintain formatting consistency, use an empty <div> instead of a blank image if there's no image.

#21 OP Brodel

    Neowinian UNSTOPPABLE

  • 5,877 posts
  • Joined: 25-July 04
  • Location: London UK, we rock on top like a toupee

Posted 18 April 2012 - 12:36

I have added a transparent noimage.png to the blogimages folder and tried the above code as well as a blank div, but each time I make a post with no image there is still a broken image link. Am I adding it incorrectly or something?

//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
//Outputs the image and other data
echo "<h4></b> ".$info['title'] . "</h4>";
echo "<b><h6>".$info['date']."</h6></b><br>";
if(isset($info['photo']))
{
		echo "<p><img src=/blogimages/" . $info['photo'] . " /></p><br />";
}
else
{
		echo "<p><img src='/blogimages/noimage.png' /></p><br />";
}
echo "<b><p></b> ".$info['text'] . "<p><br>";
echo "<div class='blogspacer'></div>";
}

If it makes any difference, in chrome and ie the broken image icon shows. In firefox it doesn't.


Is it a change I need to make to my addblog.php also?

  //This gets all the other information from the form
		$title=$_POST['title'];
		$date= date("d F Y H:i:s");
		$photo=($_FILES['photo']['name']);
		$text=$_POST['text'];
  
  
		//This keeps the formatting of text entered into the text area
		$text=nl2br($text);

		// Connects to your Database
		mysql_connect("localhost:8888","Alexweb","*****") or die(mysql_error()) ;
   mysql_select_db("Alexweb") or die(mysql_error()) ;
  
		//Writes the information to the database
		mysql_query("INSERT INTO `blogentries` VALUES ('$title', '$date', '$photo', '$text')") ;


		//Writes the photo to the server
		if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
		{
  

		//Tells you if its all ok
		echo "<h4>The photo ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your entry has been added to the blog" ;
		echo '<br><a href="http://www.mywebsite.com/blog2.php"> Click here to view</a>';
		}
		else {
	   //Gives an error if its not
		echo "<h4>thanks for your entry .";
	 echo '<br><a href="http://www.mywebsite.com/blog2.php"> Click here to view</a>';
		}


#22 threetonesun

    Neowinian ULTRAKILL

  • 11,301 posts
  • Joined: 26-February 02

Posted 18 April 2012 - 12:53

I assume it doesn't work if you post an image, either, as you've got quotes all over the place there. This line:

echo "<p><img src=/blogimages/" . $info['photo'] . " /></p><br />";

Should be
echo '<p><img src="/blogimages/' . $info['photo'] . '"/></p><br />';

To make things easier to read, you might want to use single quotes with PHP, double quotes with HTML.
Also, is the noimage.png in the correct place?

#23 OP Brodel

    Neowinian UNSTOPPABLE

  • 5,877 posts
  • Joined: 25-July 04
  • Location: London UK, we rock on top like a toupee

Posted 18 April 2012 - 13:01

if I include an image it does work and uploads it to blogimages. There is definitely a noimage in the correct place.

Yea, I really need to tidy things up. It's getting a bit of a mess, still getting he hang of integrating the html and php, as you can tell, lol.

#24 threetonesun

    Neowinian ULTRAKILL

  • 11,301 posts
  • Joined: 26-February 02

Posted 18 April 2012 - 13:18

I suspect that $photo=($_FILES['photo']['name']); does not return FALSE if there is no image. You should check if $_FILES['photo'] is set, and then assign it to $photo, which should by default be set to FALSE.

#25 winlonghorn

    Resident Elite

  • 1,037 posts
  • Joined: 17-March 05
  • Location: Erie, PA

Posted 18 April 2012 - 13:36

What about settting the image src to "./blogimages/"?

#26 OP Brodel

    Neowinian UNSTOPPABLE

  • 5,877 posts
  • Joined: 25-July 04
  • Location: London UK, we rock on top like a toupee

Posted 18 April 2012 - 13:37

View Postthreetonesun, on 18 April 2012 - 13:18, said:

I suspect that $photo=($_FILES['photo']['name']); does not return FALSE if there is no image. You should check if $_FILES['photo'] is set, and then assign it to $photo, which should by default be set to FALSE.

i'm not entirely sure what you mean. i don't want to keep bugging people with basic questions though so i'll do a bit of googling. thanks again for your help :)

#27 OP Brodel

    Neowinian UNSTOPPABLE

  • 5,877 posts
  • Joined: 25-July 04
  • Location: London UK, we rock on top like a toupee

Posted 18 April 2012 - 13:40

View Postwinlonghorn, on 18 April 2012 - 13:36, said:

What about settting the image src to "./blogimages/"?

Thanks, I tried that but still have the same issue.

#28 winlonghorn

    Resident Elite

  • 1,037 posts
  • Joined: 17-March 05
  • Location: Erie, PA

Posted 18 April 2012 - 13:42

View PostBrodel, on 18 April 2012 - 13:40, said:

Thanks, I tried that but still have the same issue.

Ok, sorry that it didn't work. I just figured it wasn't finding it because the path wasn't starting from the parent directory. I apologize that I couldn't be more helpful.

#29 OP Brodel

    Neowinian UNSTOPPABLE

  • 5,877 posts
  • Joined: 25-July 04
  • Location: London UK, we rock on top like a toupee

Posted 18 April 2012 - 13:44

View Postwinlonghorn, on 18 April 2012 - 13:42, said:

Ok, sorry that it didn't work. I just figured it wasn't finding it because the path wasn't starting from the parent directory. I apologize that I couldn't be more helpful.

No worries. It's all worth trying for me :)

#30 winlonghorn

    Resident Elite

  • 1,037 posts
  • Joined: 17-March 05
  • Location: Erie, PA

Posted 18 April 2012 - 13:44

View PostBrodel, on 18 April 2012 - 13:37, said:

i'm not entirely sure what you mean. i don't want to keep bugging people with basic questions though so i'll do a bit of googling. thanks again for your help :)

I think this is what he means:

if(isset($_FILES['photo']))
{
	$photo = $_FILES['photo'];
}

View PostBrodel, on 18 April 2012 - 13:44, said:

No worries. It's all worth trying for me :)

True. It is a learning experience. :)