Reputation: 1
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?
Upvotes: 0
Views: 614
Reputation: 309
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
Upvotes: 1