-1

I am creating a newsletter and I want to display my database image which I have inserted by name into my database. when I try to display image, but image is not displayed.

$date = $_POST['date'];

$qry_banner = "INSERT INTO `banner_tbl`(`banner_img`, `date`) VALUES ('$img_banner','$date')";
$res_banner = mysql_query($qry_banner);
$banner_tbl_id = mysql_insert_id();


$sec_id = $_POST['sectionID'];
$sec_active_image = $_POST['activeimage'];
$sec_title = $_POST['section_title'];


$total_sec = count($sec_id);

for($i=0;$i<$total_sec;$i++) 
{
$qry_section1 = "INSERT INTO `section_lt_tbl`(`banner_id`, `sectionID`, `activeimage`, `sectiontitle`) VALUES 
('$banner_tbl_id','$sec_id[$i]','$sec_active_image[$i]','$sec_title[$i]')";

 $nav_1_img = mysql_query("SELECT `activeimage` FROM `section_lt_tbl` WHERE sectionID = '1' limit 1");
 $nav_2_img = mysql_query("SELECT `activeimage` FROM `section_lt_tbl` WHERE sectionID = '2' limit 1");
 $nav_3_img = mysql_query("SELECT `activeimage` FROM `section_lt_tbl` WHERE sectionID = '3' limit 1");
 $nav_4_img = mysql_query("SELECT `activeimage` FROM `section_lt_tbl` WHERE sectionID = '4' limit 1");

$res_section1 = mysql_query($qry_section1);
$sec_tbl_id1 = mysql_insert_id();

$section_generated_id[] = $sec_tbl_id1;

}
 $nav_1_img_val = mysql_fetch_assoc ($nav_1_img);
 $nav_2_img_val = mysql_fetch_assoc ($nav_2_img);
 $nav_3_img_val = mysql_fetch_assoc ($nav_3_img);
 $nav_4_img_val = mysql_fetch_assoc ($nav_4_img);

Below is my html code where I'm trying to display image:

<td bgcolor="#dbdbdb" align="center"><table border="0" cellpadding="0" cellspacing="0" width="550" align="center">
       <tr>

       <td width="25%"><a href="#featuredstartup"><img src="uploads/<?php echo $nav_1_img_val ?>_blue.jpg" width="125px" height="134px" />
</a></td>
       <td width="25%"><a href="#healthcaredeals"><img src="uploads/<?php echo $nav_2_img_val ?>_grey.jpg" width="125px" height="134px" /></a></td>
       <td width="25%"><a href="#healthcarenews"><img src="uploads/<?php echo $nav_3_img_val ?>_grey.jpg" width="125px" height="134px" /></a></td>
       <td width="25%"><a href="#healthcaremarkets"><img src="uploads/<?php echo $nav_4_img_val ?>_grey.jpg" width="125px" height="134px" /></a></td>

       </tr>
       </table>
       </td>
Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65
chintan suthar
  • 51
  • 1
  • 10

3 Answers3

2

Modify your img src tag to following.

<img src="uploads/<?php echo $nav_1_img_val['activeimage'] ?>_blue.jpg" width="125px" height="134px" />

mysql_fetch_assoc() will return you an associative array and you have to fetch the corresponding key value.

Gautam
  • 1,046
  • 5
  • 20
1

You have to use $nav_1_img_val['activeimage'] instead of $nav_1_img_val because mysql_fetch_assoc is returning an array.

You should also think about moving to mysqli or PDO because mysql is deprecated.

mario.van.zadel
  • 2,919
  • 14
  • 23
1

mysql_fetch_assoc returns an associative array.

You need to access the value of the array by using the name of the column as key.

 $nav_1_img_result = mysql_fetch_assoc ($nav_1_img);
 $nav_1_img_val = $nav_1_img_result['activeimage'];
Muhammad Usman
  • 1,366
  • 5
  • 18
  • 33