why there is undefined index notice though variable is defined?










1















I am getting an undefined index notice in my php code though I defined a variable and submitting it with POST method. Here's the code:



<form action="" method="post">
<button class="subbutton button1" type="button" onclick="return
toggleMe('image')">Update Image</button><br>
<div id="image" style="display:none">
<center>
<table style="width:80%;">
<tr>
<td><input type="text" name="img_rn" placeholder="Enter Roll No."
class="textstyle" required="required"/></td>
<td><input type="file" name="image_src" id="fileToUpload"
required="required" style="color:white;"/></td>
<td><button class="button button1" type="submit" name="img_upld"
class="textstyle" required="required">Upload</button></td>
</tr>
</table>
</center>
</div>
</form>


php code:



<?php
if(isset($_POST["img_upld"]))

$img_rn=$_POST["img_rn"];
$cname = $_FILES['image_src']['name'];
if(!empty($cname))
$tname = $_FILES['image_src']['tmp_name'];
$size = $_FILES['image_src']['size'];
$fext = pathinfo($cname, PATHINFO_EXTENSION);

// Allow certain file formats
if($fext != "jpg" && $fext != "png" && $fext != "jpeg"
&& $fext != "gif" )
echo "<center>Sorry, only JPG, JPEG, PNG & GIF files are allowed.
</center>";


$savename = $img_rn.".".$fext;

$finalfile = "../uploads/$savename";
if(!empty($cname))
if($size < 500000)
$check = move_uploaded_file($tname,$finalfile);
if($check)
$upld=$conn->prepare("UPDATE users SET image=:savename WHERE
rollno=:img_rn");
//binding
$upld->bindparam(":savename",$savename);
$upld->bindparam(":img_rn",$img_rn);
$result=$upld->execute();

echo "<center style='color:white'>File uploaded successfully!</center>";
else
echo "<center style='color:white'>File upload failed.</center>";


else
echo "<center style='color:white'>File size too large.</center>";



?>


I have already defined a variable and also submitting it via POST methd, but still I am getting error Notice: Undefined index: image_src in C:wamp64wwwarsod_dynamicadminhome.php on line 368 not getting what is problem. Please help.










share|improve this question


























    1















    I am getting an undefined index notice in my php code though I defined a variable and submitting it with POST method. Here's the code:



    <form action="" method="post">
    <button class="subbutton button1" type="button" onclick="return
    toggleMe('image')">Update Image</button><br>
    <div id="image" style="display:none">
    <center>
    <table style="width:80%;">
    <tr>
    <td><input type="text" name="img_rn" placeholder="Enter Roll No."
    class="textstyle" required="required"/></td>
    <td><input type="file" name="image_src" id="fileToUpload"
    required="required" style="color:white;"/></td>
    <td><button class="button button1" type="submit" name="img_upld"
    class="textstyle" required="required">Upload</button></td>
    </tr>
    </table>
    </center>
    </div>
    </form>


    php code:



    <?php
    if(isset($_POST["img_upld"]))

    $img_rn=$_POST["img_rn"];
    $cname = $_FILES['image_src']['name'];
    if(!empty($cname))
    $tname = $_FILES['image_src']['tmp_name'];
    $size = $_FILES['image_src']['size'];
    $fext = pathinfo($cname, PATHINFO_EXTENSION);

    // Allow certain file formats
    if($fext != "jpg" && $fext != "png" && $fext != "jpeg"
    && $fext != "gif" )
    echo "<center>Sorry, only JPG, JPEG, PNG & GIF files are allowed.
    </center>";


    $savename = $img_rn.".".$fext;

    $finalfile = "../uploads/$savename";
    if(!empty($cname))
    if($size < 500000)
    $check = move_uploaded_file($tname,$finalfile);
    if($check)
    $upld=$conn->prepare("UPDATE users SET image=:savename WHERE
    rollno=:img_rn");
    //binding
    $upld->bindparam(":savename",$savename);
    $upld->bindparam(":img_rn",$img_rn);
    $result=$upld->execute();

    echo "<center style='color:white'>File uploaded successfully!</center>";
    else
    echo "<center style='color:white'>File upload failed.</center>";


    else
    echo "<center style='color:white'>File size too large.</center>";



    ?>


    I have already defined a variable and also submitting it via POST methd, but still I am getting error Notice: Undefined index: image_src in C:wamp64wwwarsod_dynamicadminhome.php on line 368 not getting what is problem. Please help.










    share|improve this question
























      1












      1








      1








      I am getting an undefined index notice in my php code though I defined a variable and submitting it with POST method. Here's the code:



      <form action="" method="post">
      <button class="subbutton button1" type="button" onclick="return
      toggleMe('image')">Update Image</button><br>
      <div id="image" style="display:none">
      <center>
      <table style="width:80%;">
      <tr>
      <td><input type="text" name="img_rn" placeholder="Enter Roll No."
      class="textstyle" required="required"/></td>
      <td><input type="file" name="image_src" id="fileToUpload"
      required="required" style="color:white;"/></td>
      <td><button class="button button1" type="submit" name="img_upld"
      class="textstyle" required="required">Upload</button></td>
      </tr>
      </table>
      </center>
      </div>
      </form>


      php code:



      <?php
      if(isset($_POST["img_upld"]))

      $img_rn=$_POST["img_rn"];
      $cname = $_FILES['image_src']['name'];
      if(!empty($cname))
      $tname = $_FILES['image_src']['tmp_name'];
      $size = $_FILES['image_src']['size'];
      $fext = pathinfo($cname, PATHINFO_EXTENSION);

      // Allow certain file formats
      if($fext != "jpg" && $fext != "png" && $fext != "jpeg"
      && $fext != "gif" )
      echo "<center>Sorry, only JPG, JPEG, PNG & GIF files are allowed.
      </center>";


      $savename = $img_rn.".".$fext;

      $finalfile = "../uploads/$savename";
      if(!empty($cname))
      if($size < 500000)
      $check = move_uploaded_file($tname,$finalfile);
      if($check)
      $upld=$conn->prepare("UPDATE users SET image=:savename WHERE
      rollno=:img_rn");
      //binding
      $upld->bindparam(":savename",$savename);
      $upld->bindparam(":img_rn",$img_rn);
      $result=$upld->execute();

      echo "<center style='color:white'>File uploaded successfully!</center>";
      else
      echo "<center style='color:white'>File upload failed.</center>";


      else
      echo "<center style='color:white'>File size too large.</center>";



      ?>


      I have already defined a variable and also submitting it via POST methd, but still I am getting error Notice: Undefined index: image_src in C:wamp64wwwarsod_dynamicadminhome.php on line 368 not getting what is problem. Please help.










      share|improve this question














      I am getting an undefined index notice in my php code though I defined a variable and submitting it with POST method. Here's the code:



      <form action="" method="post">
      <button class="subbutton button1" type="button" onclick="return
      toggleMe('image')">Update Image</button><br>
      <div id="image" style="display:none">
      <center>
      <table style="width:80%;">
      <tr>
      <td><input type="text" name="img_rn" placeholder="Enter Roll No."
      class="textstyle" required="required"/></td>
      <td><input type="file" name="image_src" id="fileToUpload"
      required="required" style="color:white;"/></td>
      <td><button class="button button1" type="submit" name="img_upld"
      class="textstyle" required="required">Upload</button></td>
      </tr>
      </table>
      </center>
      </div>
      </form>


      php code:



      <?php
      if(isset($_POST["img_upld"]))

      $img_rn=$_POST["img_rn"];
      $cname = $_FILES['image_src']['name'];
      if(!empty($cname))
      $tname = $_FILES['image_src']['tmp_name'];
      $size = $_FILES['image_src']['size'];
      $fext = pathinfo($cname, PATHINFO_EXTENSION);

      // Allow certain file formats
      if($fext != "jpg" && $fext != "png" && $fext != "jpeg"
      && $fext != "gif" )
      echo "<center>Sorry, only JPG, JPEG, PNG & GIF files are allowed.
      </center>";


      $savename = $img_rn.".".$fext;

      $finalfile = "../uploads/$savename";
      if(!empty($cname))
      if($size < 500000)
      $check = move_uploaded_file($tname,$finalfile);
      if($check)
      $upld=$conn->prepare("UPDATE users SET image=:savename WHERE
      rollno=:img_rn");
      //binding
      $upld->bindparam(":savename",$savename);
      $upld->bindparam(":img_rn",$img_rn);
      $result=$upld->execute();

      echo "<center style='color:white'>File uploaded successfully!</center>";
      else
      echo "<center style='color:white'>File upload failed.</center>";


      else
      echo "<center style='color:white'>File size too large.</center>";



      ?>


      I have already defined a variable and also submitting it via POST methd, but still I am getting error Notice: Undefined index: image_src in C:wamp64wwwarsod_dynamicadminhome.php on line 368 not getting what is problem. Please help.







      php image-uploading undefined-index






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 9:08









      StrooksStrooks

      537




      537






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You need to specify multipart/form-data when sending files.



          <form action="" method="post" enctype="multipart/form-data">


          See this helpful post for more information on enctype encoding.



          You should always check for isset and handle the result accordingly.






          share|improve this answer

























          • Thanks, it worked!

            – Strooks
            Nov 13 '18 at 9:15










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277392%2fwhy-there-is-undefined-index-notice-though-variable-is-defined%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You need to specify multipart/form-data when sending files.



          <form action="" method="post" enctype="multipart/form-data">


          See this helpful post for more information on enctype encoding.



          You should always check for isset and handle the result accordingly.






          share|improve this answer

























          • Thanks, it worked!

            – Strooks
            Nov 13 '18 at 9:15















          0














          You need to specify multipart/form-data when sending files.



          <form action="" method="post" enctype="multipart/form-data">


          See this helpful post for more information on enctype encoding.



          You should always check for isset and handle the result accordingly.






          share|improve this answer

























          • Thanks, it worked!

            – Strooks
            Nov 13 '18 at 9:15













          0












          0








          0







          You need to specify multipart/form-data when sending files.



          <form action="" method="post" enctype="multipart/form-data">


          See this helpful post for more information on enctype encoding.



          You should always check for isset and handle the result accordingly.






          share|improve this answer















          You need to specify multipart/form-data when sending files.



          <form action="" method="post" enctype="multipart/form-data">


          See this helpful post for more information on enctype encoding.



          You should always check for isset and handle the result accordingly.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 9:16

























          answered Nov 13 '18 at 9:10









          atomsatoms

          1,92911123




          1,92911123












          • Thanks, it worked!

            – Strooks
            Nov 13 '18 at 9:15

















          • Thanks, it worked!

            – Strooks
            Nov 13 '18 at 9:15
















          Thanks, it worked!

          – Strooks
          Nov 13 '18 at 9:15





          Thanks, it worked!

          – Strooks
          Nov 13 '18 at 9:15

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277392%2fwhy-there-is-undefined-index-notice-though-variable-is-defined%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto