2 replies
I wrote this script to upload images to a folder in my server and add the image path and description to mysql table. I tried retriving the images and descriptions from the database, the images will be displayed, but the description will not.



<?php
$con=mysql_connect('localhost','****','*******') or die(mysql_error());
$vb=mysql_select_db('test');
$config='Real Php';
?>
<!doctype html><html>
<head>
<title>php oop</title>
<meta charset="utf-8">

</head>
<body><h1 style="color:#191970"><?php echo $config ?></h1>

<form action="get.php" method="POST" enctype="multipart/form-data">
<label for="file">File</label><br />


<input type="file" name="file"/><br />
<input type="text" name="cap" size="40" />
<input type="submit" value="upload" name="sade"/><br />
</form>

<?php
$folder="images/";
$upload = $_POST['sade'];
$file = $_POST['file'];
$cap=$_POST['cap'];
if(isset($upload))
{
move_uploaded_file($_FILES['file']['tmp_name'], "$folder".$_FILES['file']['name']);
echo $_FILES['file']['name']." is been added";
$path=$_FILES['file']['name'];
echo $path=$_FILES['file']['name'];

$insert=mysql_query("insert into people(path,caption) values('$path','$cap') ");

if($result) { echo "Image name saved into database"; }
else {

//Gives and error if its not
echo mysql_error();
}
}

else{echo "Error";}


?>

The above is upload images.







The code below is to fetch images and description but it fetches only the iamge, what do you think could be wrong?

<?php
$con=mysql_connect('localhost','*****','*******') or die(mysql_error());
$vb=mysql_select_db('test');
?>
<!doctype html><html>
<head> <link rel="stylesheet" type="text/css" href="main.css" />
<link rel="stylesheet" href="main.css" type="text/css" media="screen" />
<title>Real php | <?php echo $config; ?></title>
<meta charset="utf-8">
<style type="text/css">
a:hover,a:active,a:link{text-decoration:none;}
</style>
</head>
<body>
<h2>Finally</h2>
<?php

$data = mysql_query("SELECT `path` FROM people ") or
die(mysql_error());
//Puts it into an array
$file_path = 'http://localhost/green/images/';

while($row = mysql_fetch_assoc( $data ))
{//Outputs the image and other data

$src=$file_path.$row['path'];
echo "<img src=".$src."> <br>";
echo $row['caption'].'<br/>';
echo $row['caption'];
}
if(!$row){echo mysql_error();}


?>
</body>
</html>
#image #mysql #path
  • Profile picture of the author KirkMcD
    You're not selecting it from the table:

    $data = mysql_query("SELECT `path` FROM people ")
    {{ DiscussionBoard.errors[9168054].message }}
  • Profile picture of the author jigney
    It’s very simple, You are inserting both the thing image name and the description, But when you fetching the data at that time you are just fetching the image name as ‘path’but not the description which you stored in the caption.
    You have to write
    $data = mysql_query("SELECT * FROM people ") OR
    $data = mysql_query("SELECT `path`,`caption` FROM people ")
    And than you can fetch as array and display.
    {{ DiscussionBoard.errors[9205218].message }}

Trending Topics