0

Is it has method to save <img src="XXXXX"> image path to database using PHP. Why src path doesn't pick in PHP . My PHP code is

<?php
include('conn.php');
if(!empty($_POST["submit1"]) || isset($_POST["submit1"])){
 if (isset($_POST['img1'])) {
        $filename1= $_POST['img1'];
    }
  if (isset($_POST['img2'])) {
        $filename2= $_POST['img2'];
    }
}
?>

And my html code is

    <html>
    <head>
    <title>Test 
    </title>
</head>
    <body>
     <form  action="phpscript/test.php" method="post" enctype="multipart/form-data">
            <div>Images </div> 
              <input type="file" id="file" name="file">
              <div class="row align-text-center">                                     
                    <div class="col-sm2">
                        
 <input type="image"  id="img1" name="img1"  src="img/1.jpg" width="150" height="150"  style="border: "/> 
    
                    </div>
     <div class="col-sm2">
    <input type="image" src="img/2.jpg" width="150" height="150" id="img2" name="img2" style="border: " /> 
                                             
                    </div>
                  <div class="col-sm2">
    <button class="btn " type="submit" id="submit1" name="submit1">Submit</button>  
    </form>
    </body>
    
    </html>

Actually I need to pick image path in php code.But in php code $filename1 and $filename2 values are NULL.Is it another method to get image path in php code?

6
  • Did you already use var_dump($_FILES); to see, what variables it has? Commented Nov 23, 2020 at 11:42
  • 3
    Why should any $_FILES['img1'] exist? Your form field with that name is not a file upload field, it is just a type="image" field. Those do not send any file names. Commented Nov 23, 2020 at 11:43
  • 5
    I think you've misunderstood what the element type="image"does. The src-attribute is for how the element should look, it won't be submitted with the form Commented Nov 23, 2020 at 11:43
  • $_POST['img1'] also wouldn't be the file, take a look at php.net/manual/en/features.file-upload.post-method.php Commented Nov 23, 2020 at 11:52
  • I change code $_FILES to $_POST.But it also doesn't work.I think<input type="image" src="XXX"> doesn't work as input on POST method. Commented Nov 23, 2020 at 12:31

1 Answer 1

1

You have a misconception about <input type="image"> this. This just defines a image as a submit button. Check out this as a reference https://www.w3schools.com/tags/att_input_type_image.asp Now to save image path in a PHP variable, you have to select that image as a file.

<input type="image"  id="img1" name="img1"  src="img/1.jpg" width="150" height="150"  style="border:"/>
<input type="image" src="img/2.jpg" width="150" height="150" id="img2" name="img2" style="border:" />

This just makes an output of images which are img/1.jpg and img/2.jpg. To get images path in a PHP variable you have to change these line like bellow:

<input type="file" name="img1">
<input type="file" name="img2">

Then you can select an image file. In PHP part change your code with this code:

<?php
    include('conn.php');
    if(!empty($_POST["submit1"]) || isset($_POST["submit1"])){
     if (isset($_FILES["img1"])) {
            $filename1= $_FILES["img1"]["name"];
        }
      if (isset($_FILES['img2'])) {
            $filename2= $_FILES["img2"]["name"];
        }
    }
?>

To know details about PHP file/image uploading check out this https://www.php.net/manual/en/features.file-upload.post-method.php

Sign up to request clarification or add additional context in comments.

2 Comments

can we use hidden input to use for this?
Your question is not clear. What do you want to do actually? If you just want to get the image path, you don't need any hidden input tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.