473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 7500
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

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

Similar topics

1
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...
2
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...
1
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:...
4
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,...
6
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...
3
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:...
9
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...
2
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...
6
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?...
1
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.