Jump to content

Image from mysql becomes text


PTW

Recommended Posts

hello world! First post here!

 

I'm a newbie trying to make my own cms, everything is working except the import of image into the post, this becomes text.

 

Here is the code:

 

function connect() {

	$con = mysql_connect($this->host, $this->username, $this->password) or die (mysql_error());
	mysql_select_db ($this->db, $con) or die (mysql_error());

	}

	function get_content($id = '') {

		if($id != ""): 
			$id = mysql_real_escape_string($id);
			$sql = "SELECT * FROM cms_content WHERE id = '$id'";
		else:
			$sql = "SELECT * FROM cms_content ORDER BY id DESC";					endif;

			$return = '<a href="index.php">go back</a>';



		$res = mysql_query($sql) or die(mysql_error());

		if(mysql_num_rows($res) != 0):
			while($row = mysql_fetch_assoc($res)) {
				echo '<div class="title"><h1><a href="index.php?id=' . $row['id'] . '">' . $row['title'] . '</a></h1></div>'; 
				echo '<div class="date"><p>' . $row['date'] . '</p></div>';
				echo '<div class="photo"><p>' . $row['photo'] . '</p></div>';
				echo '<div class="txt"><p>' . $row['body'] . '</p></div>';






			}

		else:

			echo '<p class="results_">uH oh!</p>';

		endif;	

		echo $return;
	}




	function add_content($p) {

	$title = mysql_real_escape_string($p['title']);
	$date = mysql_real_escape_string($p['date']);
	$photo = mysql_real_escape_string($p['photo']);
	$body = mysql_real_escape_string($p['body']);

	// if they write wrong

	if(!$title | | !$body):

		if(!$title):
			echo "<p>The Title is Required</p>";
		endif;

		if(!$photo):
			echo "<p>No photo</p>";
		endif;

		if(!$body):
			echo "<p>The body is Required</p>";
		endif;

		echo '<p><a href="add-content.php">Try again</a></p>';

	else:

		// add to db
		$sql = "INSERT INTO cms_content VALUES (null, '$title', '$date', '$photo', '$body')";
		$res = mysql_query($sql) or die(mysql_error());
		echo "<p class='results_'>Added with success!</p>";
		// [END] add to db

	endif;

	// [END] if they write wrong

	}

 

 

 

and in the html i got a form

 

<form enctype="multipart/form-data" method="post" action="index.php">

<input type="hidden" name="add" value="true">

<div>

	<label for="title"><p>Title:</p></label>
	<input type="text" name="title" id="title" />

</div>




<div>



<input type="file" name="photo"><br> 


</div>




<div>

	<label for="body"><p>Body:</p></label>
	<textarea name="body" id="body" rows="8" cols="40"></textarea>

</div>



<input type="submit" name="submit" value="Add Content">

</form>

Link to comment
Share on other sites

you need to use $_FILES array to get the uploaded file

 

http://php.net/manual/en/features.file-upload.post-method.php

 

ok, don't really get this, but here is what get echo:d

 

if(mysql_num_rows($res) != 0):
			while($row = mysql_fetch_assoc($res)) {
				echo '<div class="title"><h1><a href="index.php?id=' . $row['id'] . '">' . $row['title'] . '</a></h1></div>'; 
				echo '<div class="date"><p>' . $row['date'] . '</p></div>';
				echo '<div class="photo"><p>' . $row['photo'] . '</p></div>';
				echo '<div class="txt"><p>' . $row['body'] . '</p></div>';


			}

 

I got to get the $_FILES array in -> echo '<div class="photo"><p>' . $row['photo'] . '</p></div>'; somehow?

Link to comment
Share on other sites

You are simply writing the image path to the screen, and not placing it within <img> tags:

 

<?php
...

while($row = mysql_fetch_assoc($res)) {
echo '<div class="title"><h1><a href="index.php?id=' . $row['id'] . '">' . $row['title'] . '</a></h1></div>'; 
echo '<div class="date"><p>' . $row['date'] . '</p></div>';
echo '<div class="photo"><p><img src="' . $row['photo'] . '" width="<WIDTH>" height="<HEIGHT>"/></p></div>';
echo '<div class="txt"><p>' . $row['body'] . '</p></div>';
}

 

Replace <WIDTH> and <HEIGHT> accordingly, obviously.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.