472,131 Members | 1,634 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,131 software developers and data experts.

How to fix "Notice: Undefined offset" while inserting uploaded images into db?

10
Hi , i have a form and it has A problem:
i upload my 3 image and it uploaded in definded path but in insert in db filed there are empty:
Notice: Undefined offset: 0 in C:\wamp\www
Notice: Undefined offset: 1 in C:\wamp\www
Notice: Undefined offset: 2 in C:\wamp\www
i think scope of my Array variable : newsimg[] has a problem.
Expand|Select|Wrap|Line Numbers
  1. <div id="postedit" class="clearfix">
  2. <h2 class="ico_mug">ADD NEWS</h2>
  3. <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"
  4.              enctype="multipart/form-data">
  5. <div>
  6.             <input id="post_title" type="text" size="30" tabindex="1" name="post_title"
  7.              value="Title" />
  8.             </div>
  9. <div id="form_middle_cont" class="clearfix">
  10. <div class="left">
  11. <textarea id="markItUp" cols="80" rows="10" tabindex="2" name="markItUp"></textarea>
  12.             </div>
  13. <div class="left form_sidebar">
  14. <h3>Is A Member?</h3>
  15. <ul>
  16. <li>
  17. <label><input type="checkbox" class="noborder" name="chbox"  />Yes</label></li>
  18. </ul><BR/>
  19.      <ul>
  20.           <h4>News Pictures:</h4>
  21.           <li><input type=file name="file[]" size=20 
  22.                 accept="image/jpg,image/jpeg,image/png">
  23.           </li>
  24.           <li><input type=file name="file[]" size=20 
  25.                 accept="image/jpg,image/jpeg,image/png">
  26.           </li>
  27.           <li><input type=file name="file[]" size=20 
  28.                 accept="image/jpg,image/jpeg,image/png">
  29.           </li>
  30.           </ul>
  31. <p>
  32. <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
  33. <input type="submit" value="Store" id="save"  name="save"/>
  34. </p>
  35.  
  36. </div>
  37.             </div>
  38.  
  39. </form>
  40. <?php
  41. if (isset($_POST['save'])) {
  42.  
  43.   $title = mysql_real_escape_string(trim($_POST["post_title"]));
  44.   if ($title == '' || $title == 'Title'){
  45.  
  46. die("
  47. <div id='fail' class='info_div'  align='center' dir='rtl'>
  48. <span class='ico_cancel'><strong>
  49. Insert Title,Please</strong></span><br />
  50. <a href=\"javascript: history.go(-1)\">Retry</a>
  51. </div>"); }
  52. $description = ( mysql_real_escape_string(trim($_POST["markItUp"])));
  53.   if ($description == ''){
  54. die("
  55. <div id='fail' class='info_div'  align='center' dir='rtl'>
  56. <span class='ico_cancel'><strong>
  57. Insert News_body,Please</strong></span><br />
  58. <a href=\"javascript: history.go(-1)\">Retry</a>
  59. </div>"); }
  60.   if (isset($_POST['chbox'])) $isprv = 'true';
  61.  
  62.   else  $isprv = 'false';
  63.   //max fle size value
  64.   $max_file_size = 2097152;
  65.   //Global newsimg
  66.   global $newsimg;global $ctr;
  67.   //timestamp to make files unique names
  68.   $timestamp = time();
  69.   //destination folder path
  70.   $destpath = "../Dir/Newsimg/";
  71.   //looping each file or image from the form
  72.   while(list($key,$value) = @each($_FILES["file"]["name"])) {
  73.   //check if any empty or errors
  74.   if(!empty($value)){
  75.   if ($_FILES["file"]["error"][$key] > 0) {
  76.   $edir ='<div id="fail" class="info_div">';
  77.   $edir .='<span class="ico_cancel"><strong>';
  78.   $edir .="ERR : ".  $_FILES["file"]["error"][$key]  .'</strong></span></div>';
  79.   echo($edir);
  80.   } else {
  81.   //add the timestamp to filename
  82.   $file_name = $timestamp.$_FILES['file']['name'];
  83.   //temp name from upload form,  key of each is set
  84.   $source = $_FILES["file"]["tmp_name"][$key] ;
  85.   //getting the file type
  86.   $file_type = $_FILES["file"]["type"][$key];
  87.   //getting the file size
  88.   $file_size = $_FILES["file"]["size"][$key];
  89.   //getting the file width
  90.   $file_width = GetImageSize($source);
  91.   //placing each file name into a variable
  92.   $filename1 = $_FILES["file"]["name"][$key];
  93.   //lowering the file names
  94.   $filename = strtolower($filename1);
  95.   //adding timestamp to front of file name
  96.   $filename = "$timestamp$filename";
  97.   ++$timestamp;
  98.   if ($file_type != "image/jpeg" && $file_type != "image/png" && $file_type != "image/jpg") {
  99. die("
  100. <div id='fail' class='info_div'>
  101. <span class='ico_cancel'><strong>
  102. Invalid Format</strong></span><br />
  103. <a href=\"javascript: history.go(-1)\">Retry</a>
  104. </div>"); }
  105. if($file_width[0] > 650) {
  106. die("
  107. <div id='fail' class='info_div'>
  108. <span class='ico_cancel'><strong>
  109. Width less than 650 Pixel!</strong></span><br />
  110. <a href=\"javascript: history.go(-1)\">Retry</a>
  111. </div>"); }
  112. if($file_size > 51200) {
  113. die("
  114. <div id='fail' class='info_div'>
  115. <span class='ico_cancel'><strong>
  116. Size less than 50KByte!</strong></span><br />
  117. <a href=\"javascript: history.go(-1)\">Retry</a>
  118. </div>"); }
  119.  //moving the file to be saved from the temp location to the destination path
  120.   move_uploaded_file($source, $destpath . $filename);
  121.   //the actual final location with timestamped name
  122.   $final_file_location = "$destpath$filename";
  123.   //echo $final_file_location."<br />";
  124.   if (file_exists($final_file_location)) {
  125.   $newsimg[$ctr] = $filename;
  126.   $ctr++;
  127. ?>
  128. <div id="success" class="info_div"><span class="ico_success"><strong>
  129. <a href="<?php echo $final_file_location; ?>" target="_blank"><?php echo $filename; ?></a>
  130. </strong></span></div><br />
  131. <?php
  132.   }}}}
  133. $ctr=0;
  134. //Include database connection details
  135. require_once('../config.php');
  136. //Connect to mysql server
  137. $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
  138. if(!$link) {
  139.  die('Failed to connect to server: ' . mysql_error());
  140. }
  141. //Select database
  142. $db = mysql_select_db(DB_DATABASE);
  143. if(!$db) {
  144. die("Unable to select database");
  145.  }
  146.  
  147. $login = $_SESSION['SESS_LOG_IN'];
  148.  
  149. $query = "INSERT INTO news SET postdate=NOW(),".
  150.  
  151. "ttl='$title', newstxt='$description', ".
  152.  
  153. "isprv='$isprv',authr='$login' ";
  154.  
  155. if (isset($newsimg[0])) $query .=",newsimg1='$newsimg[0]'" ;
  156.  
  157. if (isset($newsimg[1])) $query .=",newsimg2='$newsimg[1]'" ;
  158.  
  159. if (isset($newsimg[2])) $query .=",newsimg3='$newsimg[2]'" ;
  160.  
  161. if (mysql_query($query)) {
  162. echo '<div id="success" class="info_div">';
  163. echo'<span class="ico_success"><strong>';
  164. echo 'SuccessFul! </strong></span></div>';
  165. } else {
  166. echo'<div id="warning" class="info_div">';
  167. echo'<span class="ico_error"><strong>';
  168. echo "ERR in Insert NEWS : ".mysql_error()."</strong></span></div>";
  169.  
  170.  
  171. chdir('../Dir/Newsimg/'); 
  172.  
  173.  
  174. if (isset($newsimg[0])) unlink($newsimg[0]);
  175.  
  176. if (isset($newsimg[1])) unlink($newsimg[1]);
  177.  
  178. if (isset($newsimg[2])) unlink($newsimg[2]);
  179.  
  180. }
  181. }
  182.             ?> 
  183.  
  184.  
  185.  
  186.  
  187.  
  188. </div><!-- end #postedit -->
Feb 5 '11 #1

✓ answered by code green

You have not initialised $ctr.
So the first time of
Expand|Select|Wrap|Line Numbers
  1. $newsimg[$ctr] = $filename;
The index will be null.
If you had error_reporting set (E_ALL | E_STRICT) then a 'Notice' level error would have been reported

6 7376
code green
1,726 Expert 1GB
I would say newsimg[] is empty.
Output the contents to see if anything is in there
Feb 7 '11 #2
tbebest
10
Hi, for my first value of array :if i use newsimg[] ,i have fatal error else i use newsimg[0] Notice: Undefined offset: 0; about other's i.e: newsimg[1] ,newsimg[2] ,the first sets good but the second doesn't set and if i don't set the first ,it places in newsimg[1];
i think my way to use and point to values of array has a bug. TNX.
Feb 8 '11 #3
code green
1,726 Expert 1GB
Does that mean you have solved it?
Feb 8 '11 #4
tbebest
10
Not completely, i have a problem with newsimg[0];
my first value in array newsimg[0] in insert, is empty.
Expand|Select|Wrap|Line Numbers
  1.  $query = "INSERT INTO news SET postdate=NOW(),".
  2.  "ttl='$title', newstxt='$description', ".
  3.  "isprv='$isprv',authr='$login' ";
  4.  if (isset($newsimg[0])) $query .=",newsimg1='$newsimg[0]'" ;
  5.  if (isset($newsimg[1])) $query .=",newsimg2='$newsimg[1]'" ;
  6.  if (isset($newsimg[2])) $query .=",newsimg3='$newsimg[2]'" ;
  7.  if (mysql_query($query)) {
  8. echo "<pre>";
  9. print_r($newsimg);
  10. echo "</pre>"; }
Sample => Result:
Array
(
[] => 1297148403tulips 04.jpg
[1] => 1297148404tulips 03.jpg
[2] => 1297148405tulips 02.jpg
)

newsimg[0] doesn't store!
Feb 8 '11 #5
code green
1,726 Expert 1GB
You have not initialised $ctr.
So the first time of
Expand|Select|Wrap|Line Numbers
  1. $newsimg[$ctr] = $filename;
The index will be null.
If you had error_reporting set (E_ALL | E_STRICT) then a 'Notice' level error would have been reported
Feb 8 '11 #6
tbebest
10
Hi code green,Thanks for your attensions.
Feb 9 '11 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by lawrence | last post: by
2 posts views Thread by Steven | last post: by
4 posts views Thread by Richard Lawrence | last post: by
3 posts views Thread by number1yan | last post: by
9 posts views Thread by simple12 | last post: by
2 posts views Thread by neridaj | last post: by
1 post views Thread by atang | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.