0

how to fix this error my code is working fine but i don't know why it is showing error my image is upload fine.

please help me to fix this issue thanks i have this error

Notice: Undefined index: photo in I:\xampp\htdocs\ccs\ad.php on line 121

Notice: Undefined index: photo in I:\xampp\htdocs\ccs\ad.php on line 125

Notice: Undefined index: photo in I:\xampp\htdocs\ccs\ad.php on line 127

and

line 121 is

$target = $target . basename( $_FILES['photo']['name']);

line 125 is

$photo=($_FILES['photo']['name']);

line 126 is

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))

and here complete code

<?php

/* 
 NEW.PHP
 Allows user to create a new entry in the database
*/

 // creates the new record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($sname, $fname, $error)
 {
 ?>
<?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 



    <form action="" method="post" enctype="multipart/form-data" class="registration_form">
  <fieldset>
    <legend>Admission  </legend>

    <p>Create a Admission Voucher <span class="style4" style="background:#EAEAEA none repeat scroll 0 0;line-height:1;margin-left:410px;;padding:9px 9px;">Please Fill the All Info </span> </p>
        <div class="elements">
      <label for="sname">Student Name   :</label>
      <input type="text" id="sname" name="sname" size="70" />

</div>
    <div class="elements">
      <label for="fname">Father Name   :</label>
      <input type="text" id="fname" name="fname" size="25" />
</div>

            <div class="elements">
      <label for="photo">Photo Attachment :</label>
 <input type="hidden" name="size" value="350000">
            <input type="file" name="photo" size="25"> 
    </div>



    <div class="submit">
 <button name="submit"  type="submit" class="pure-button pure-button-primary" value="Submit">Submit</button>
    </div>
  </fieldset>
</form>

    <?php 
     }

     //This is the directory where images will be saved
    $target = "images/";
    $target = $target . basename( $_FILES['photo']['name']);

    //This gets all the other information from the form

    $photo=($_FILES['photo']['name']);
    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
    {

    //Tells you if its all ok
    echo "<center>Photo ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory</center>";
    }



     // connect to the database
     include('connect-db.php');



     // check if the form has been submitted. If it has, start to process the form and save it to the database
     if (isset($_POST['submit']))
     { 
     // get form data, making sure it is valid
     $sname = mysql_real_escape_string(htmlspecialchars($_POST['sname']));
     $fname = mysql_real_escape_string(htmlspecialchars($_POST['fname']));
     $photo = mysql_real_escape_string(htmlspecialchars($_FILES['photo']['name']));

     // check to make sure both fields are entered
     if ($sname == '' )
     {
     // generate error message
     $error = 'ERROR: Please fill in all required fields!';

     // if either field is blank, display the form again
     renderForm($sname, $fname,$error);
     }
     else
     {
     // save the data to the database
      mysql_query("INSERT admission SET sname='$sname', fname='$fname',  photo='$photo'")
     or die(mysql_error()); 
     echo "<center>Admission</center>";
     echo "<center>Successful!</center>";
     // once saved, redirect back to the view page

     }
     }
     else
     // if the form hasn't been submitted, display the form
     {
     renderForm('','','','');
     }


    ?>
Kevin
  • 41,694
  • 12
  • 53
  • 70
user1833487
  • 145
  • 2
  • 13
  • 1
    no this is not dublicate – user1833487 Feb 24 '15 at 00:19
  • 1
    What is the difference ? Tell us! – Rizier123 Feb 24 '15 at 00:20
  • http://stackoverflow.com/questions/888/how-do-you-debug-php-scripts – Halcyon Feb 24 '15 at 00:20
  • my code is working fine but these error line is showing only how to fix it Notice: Undefined index: photo in I:\xampp\htdocs\ccs\ad.php on line 121 Notice: Undefined index: photo in I:\xampp\htdocs\ccs\ad.php on line 125 Notice: Undefined index: photo in I:\xampp\htdocs\ccs\ad.php on line 127 – user1833487 Feb 24 '15 at 00:22
  • 1
    If this ^ Is the only "difference" then it's a dupe that's exactly what's in the dupe question is – Rizier123 Feb 24 '15 at 00:26
  • @Rizier123 can you help me to fix this issue – user1833487 Feb 24 '15 at 00:28
  • *Hm...* wild guess. No valid enctype and maybe no POST method. *Wild, eh?* - Maybe even your file's form element's got a typo or isn't named. Won't know for sure till we see the HTML form for this. Plus, you're using `$_FILES['uploadedfile']` and `$_FILES['photo']` so that tells me something's not matching. So I'm betting that your element is named "uploadedfile" along with what I just said at the beginning of this comment. – Funk Forty Niner Feb 24 '15 at 00:35
  • So... did you read that? ^ Unless you're explicitly waiting on @Rizier123 's response. – Funk Forty Niner Feb 24 '15 at 00:44
  • 1
    Add an `isset()` conditional statement for your file's variable. This is because you have your form and PHP/SQL in one file. – Funk Forty Niner Feb 24 '15 at 01:12
  • where can i add isset() please tell me i am not expert in php – user1833487 Feb 24 '15 at 01:15
  • Move everything starting from `$target = "images/";` down to `echo "
    Photo ". basename...` under `if (isset($_POST['submit'])){` then modify that conditional statement to read as `if (isset($_POST['submit']) && !empty($_FILES['photo'])){` and you should be good to go. Make sure your bracing is paired also. If you have problems, let me know.
    – Funk Forty Niner Feb 24 '15 at 01:22
  • I've posted an answer below instead. I figured you may have a bit of difficulty with it. Let me know how it goes. – Funk Forty Niner Feb 24 '15 at 01:30
  • ok thanks @Fred-ii- i update my code and it is working fine and now when i click on submit but then Notice: Undefined index: uploadedfile in on line 54 Photo has been uploaded, and your information has been added to the directory Admission Successful! – user1833487 Feb 24 '15 at 01:33
  • I have changed that variable in my answer below which you can accept if it worked. – Funk Forty Niner Feb 24 '15 at 01:33
  • and line 54 is echo "
    Photo ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory
    ";
    – user1833487 Feb 24 '15 at 01:33

2 Answers2

3

This is too long for a comment as I tried explaining it in there, but felt it would be better if it were represented in an answer.

Nota: I also changed $_FILES['uploadedfile'] to $_FILES['photo'] since that would generate an error.

Move everything starting from $target = "images/"; down to echo "<center>Photo ". basename... then placed under if (isset($_POST['submit'])){ then modify that conditional statement to read as if (isset($_POST['submit']) && !empty($_FILES['photo'])){ and you should be good to go.

Here's a rewrite:

<?php

/* 
 NEW.PHP
 Allows user to create a new entry in the database
*/

 // creates the new record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($sname, $fname, $error)
 {
 ?>
<?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 

    <form action="" method="post" enctype="multipart/form-data" class="registration_form">
  <fieldset>
    <legend>Admission  </legend>

    <p>Create a Admission Voucher <span class="style4" style="background:#EAEAEA none repeat scroll 0 0;line-height:1;margin-left:410px;;padding:9px 9px;">Please Fill the All Info </span> </p>
        <div class="elements">
      <label for="sname">Student Name   :</label>
      <input type="text" id="sname" name="sname" size="70" />

</div>
    <div class="elements">
      <label for="fname">Father Name   :</label>
      <input type="text" id="fname" name="fname" size="25" />
</div>

            <div class="elements">
      <label for="photo">Photo Attachment :</label>
 <input type="hidden" name="size" value="350000">
            <input type="file" name="photo" size="25"> 
    </div>

    <div class="submit">
 <button name="submit"  type="submit" class="pure-button pure-button-primary" value="Submit">Submit</button>
    </div>
  </fieldset>
</form>

<?php 
     }

     // connect to the database
     include('connect-db.php');

     // check if the form has been submitted. If it has, start to process the form and save it to the database
     if (isset($_POST['submit']) && !empty($_FILES['photo']))
     { 

    //This is the directory where images will be saved
    $target = "images/";
    $target = $target . basename( $_FILES['photo']['name']);

    //This gets all the other information from the form

    $photo=($_FILES['photo']['name']);
    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
    {

    //Tells you if its all ok
    echo "<center>Photo ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory</center>";
    }

     // get form data, making sure it is valid
     $sname = mysql_real_escape_string(htmlspecialchars($_POST['sname']));
     $fname = mysql_real_escape_string(htmlspecialchars($_POST['fname']));
     $photo = mysql_real_escape_string(htmlspecialchars($_FILES['photo']['name']));

     // check to make sure both fields are entered
     if ($sname == '' )
     {
     // generate error message
     $error = 'ERROR: Please fill in all required fields!';

     // if either field is blank, display the form again
     renderForm($sname, $fname,$error);
     }
     else
     {
     // save the data to the database
      mysql_query("INSERT admission SET sname='$sname', fname='$fname',  photo='$photo'")
     or die(mysql_error()); 
     echo "<center>Admission</center>";
     echo "<center>Successful!</center>";
     // once saved, redirect back to the view page

     }
     }
     else
     // if the form hasn't been submitted, display the form
     {
     renderForm('','','','');
     }


?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

after your closing </form> change your code

<?php 
 }





 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
     //This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form

$photo=($_FILES['photo']['name']);
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "<center>Photo ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory</center>";
}



 // connect to the database
 include('connect-db.php');
 // get form data, making sure it is valid
 $sname = mysql_real_escape_string(htmlspecialchars($_POST['sname']));
 $fname = mysql_real_escape_string(htmlspecialchars($_POST['fname']));
 $photo = mysql_real_escape_string(htmlspecialchars($_FILES['photo']['name']));

 // check to make sure both fields are entered
 if ($sname == '' )
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
 renderForm($sname, $fname,$error);
 }
 else
 {
 // save the data to the database
  mysql_query("INSERT admission SET sname='$sname', fname='$fname',  photo='$photo'")
 or die(mysql_error()); 
 echo "<center>Admission</center>";
 echo "<center>Successful!</center>";
 // once saved, redirect back to the view page

 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','','');
 }


?>
Junius L
  • 15,881
  • 6
  • 52
  • 96