473,698 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

10 New Member
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>
  36. </div>
  37.             </div>
  39. </form>
  40. <?php
  41. if (isset($_POST['save'])) {
  43.   $title = mysql_real_escape_string(trim($_POST["post_title"]));
  44.   if ($title == '' || $title == 'Title'){
  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';
  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.  }
  147. $login = $_SESSION['SESS_LOG_IN'];
  149. $query = "INSERT INTO news SET postdate=NOW(),".
  151. "ttl='$title', newstxt='$description', ".
  153. "isprv='$isprv',authr='$login' ";
  155. if (isset($newsimg[0])) $query .=",newsimg1='$newsimg[0]'" ;
  157. if (isset($newsimg[1])) $query .=",newsimg2='$newsimg[1]'" ;
  159. if (isset($newsimg[2])) $query .=",newsimg3='$newsimg[2]'" ;
  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>";
  171. chdir('../Dir/Newsimg/'); 
  174. if (isset($newsimg[0])) unlink($newsimg[0]);
  176. if (isset($newsimg[1])) unlink($newsimg[1]);
  178. if (isset($newsimg[2])) unlink($newsimg[2]);
  180. }
  181. }
  182.             ?> 
  188. </div><!-- end #postedit -->
Feb 5 '11 #1
6 7531
code green
1,726 Recognized Expert Top Contributor
I would say newsimg[] is empty.
Output the contents to see if anything is in there
Feb 7 '11 #2
10 New Member
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 Recognized Expert Top Contributor
Does that mean you have solved it?
Feb 8 '11 #4
10 New Member
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:
[] => 1297148403tulip s 04.jpg
[1] => 1297148404tulip s 03.jpg
[2] => 1297148405tulip s 02.jpg

newsimg[0] doesn't store!
Feb 8 '11 #5
code green
1,726 Recognized Expert Top Contributor
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
10 New Member
Hi code green,Thanks for your attensions.
Feb 9 '11 #7

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

Similar topics

by: lawrence | last post by:
I just switched error_reporting to ALL so I could debug my site. I got a huge page full of errors. One of the most common was that in my arrays I'm using undefined offsets and indexes. These still work fine, but with error reporting at all they are marked as errors. Why? What am I doing wrong? www.monkeyclaus.org
by: Steven | last post by:
Hi All, I am moving some php code from a Linux machine to a Windows 2000 machine with the code belowe I get the following error : Notice: Undefined offset: 1 in c:\inetpub\wwwroot\test.php on line 5 1) reset($kolom1); 2) while(list($cat,$lnk) = each($kolom1)){ 3) kop($cat);
by: google | last post by:
Hello, I'm having major problems trying to get my head round this problem. I'm trying to generate an error free script, however, I still cannot sort out this loop. I get a "Notice: Undefined offset: 24 in c:\inetpub\wwwroot\classes\file.php on line 348"
by: Richard Lawrence | last post by:
Hi there, I'm having a problem with PHP which I'm not sure how to best solve. Here is the code: $fp = fopen("comments.txt", "r"); while(!feof($fp)) { $line = fgets($fp, 1024); list($key, $value) = explode(":", $line, 2);
by: nicy12 | last post by:
Hi! my name is Peter. iam working on the php platform. while trying to run and compile a program i get the undefined offset error iam nto much familiar with this error . Please help me . Thanks in advance. Error: Undefined offset: 0 in /es/ePrintsStats/includes/inc.html.cumulative_usage.es.php on line 16 The code: <?php $current_year = date("Y"); $last_year = date("Y")-1;
by: number1yan | last post by:
Can anyone help me, i am creating a website and am using a php script that recomends the website to other people. I keep getting the same error and can not work out why. The error is: Notice: Undefined index: FriendName in D:\Yan\Over_8\SendEmail.php on line 4, Notice: Undefined index: FriendEmail in D:\Yan\Over_8\SendEmail.php on line 5, Notice: Undefined index: Name in D:\Yan\Over_8\SendEmail.php on line 6, Notice: Undefined index: Email...
by: simple12 | last post by:
Hello I have a script which have the facility of entering any code to some part of a webpage. I have some problems with it. When i put some code in the script then their is no error shown. When i try to not use a code and leave it empty the following php errors happens. I dont understand why. Help me. Error shown are: - PHP Notice: in file /index.php on line 132: Undefined offset: 4 PHP Notice: in file /index.php on line 141:...
by: neridaj | last post by:
Hello, I'm trying to figure out how to get rid of these errors: Notice: Undefined offset: 1 in output_fns.php on line 315 Notice: getimagesize() : Read error! in output_fns.php on line 315 function get_imgdim()
by: raydiamond4u | last post by:
Hello. My name is Raymond. My code is generating the following error: Notice: Undefined offset: 2 in C:\wamp\www\imagegallery\supportfile\include\config.php on line 25 How do i correct the error? Here is the code: <?php // Include file for database connectivity $db_server = "localhost";
by: atang | last post by:
Hi, I found the code here, it's excately what i am looking for, ie8 give this error "Notice: Undefined offset: 49 in.." which is this line "if ($num_of_chars==$required)", i'm a newbie, i don't know how to overcome this error, so can someone please help me out? thanks, $string = "one two three"; $word_array = explode(" ",$string); for ($i=count($word_array); $i>=1; $i--) { $word = $i == 1 ? " word" : " words"; echo "Strings containing...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.