0

I have a little problem.

I'm making form that submits simple article to database and then displays it on admin.php page.

Everything works fine, except image. First i upload it to my server and then I try to add string(path of this image) to database.

However I can't use variable $filepath in second if statement. I can echo it after image upload but can't use in other if.

Could u help me? Thanks in advance.

Here's code:

<?php session_start();
  if (!isset($_SESSION['logged-in']))
  {
    header('Location: index.php');
    exit();
  }
?>
<?php
include('db_connect.php');
if(isset($_POST['btn_upload'])) {
  $filetmp = $_FILES["file_img"]["tmp_name"];
  $filename = $_FILES["file_img"]["name"];
  $filetype = $_FILES["file_img"]["type"];
  $filepath = "photo/".$filename;

  move_uploaded_file($filetmp,$filepath);

  $result = mysqli_query($mysqli, "INSERT INTO upload_img (img_name,img_path,img_type) VALUES ('$filename','$filepath','$filetype')");

  echo '<img src="' . $filepath . '" alt="">';
  echo $filepath;
}

if ( isset($_POST['add']) ) {
  $title = strip_tags($_POST['title']);
  $content = strip_tags($_POST['content']);
  $image = strip_tags($filepath);
  $statement = $mysqli->prepare("INSERT bikes (title,image,content) VALUES (?,?,?)");
  $statement->bind_param("sss",$title,$image,$content);
  $statement->execute();
  $statement->close();
  header('Location: admin.php');
}
?>


<form method="post" action="" class="ui form">
  <div class="required field">
    <label>Title</label>
    <input type="text" name="title" id="title">
  </div>
  <div class="required field">
    <label>Content</label>
    <textarea name="content" id="content" cols="30" rows="10"></textarea>
  </div>
  <div class="required field">
    <label>Image</label>
    <input type="text" name="image" id="image">
  </div>
    <input type="submit" class="ui primary button" id="add" name="add" value="Add article"></input>
</form> 

<form action="addbike.php" method="post" enctype="multipart/form-data">
  <input type="file" name="file_img" />
  <input type="submit" name="btn_upload" value="Upload">  
</form>

2 Answers 2

1

Since both of your forms work independently(with co-relation with each other whatsoever), $filepath variable won't be accessible in the second if block. However, what you can do is, merge both of your forms together so that all of your form's textual data and image would be accessible in a single submit button.

HTML:

<form method="post" action="" class="ui form" enctype="multipart/form-data">
    <div class="required field">
        <label>Title</label>
        <input type="text" name="title" id="title">
    </div>
    <div class="required field">
        <label>Content</label>
        <textarea name="content" id="content" cols="30" rows="10"></textarea>
    </div>
    <div class="required field">
        <label>Image</label>
        <input type="text" name="image" id="image">
    </div>
    <input type="file" name="file_img" />
    <input type="submit" class="ui primary button" id="add" name="add" value="Add article"></input>
</form> 

PHP:

if (isset($_POST['add']) && is_uploaded_file($_FILES["file_img"]["tmp_name"])) {
    $filetmp = $_FILES["file_img"]["tmp_name"];
    $filename = $_FILES["file_img"]["name"];
    $filetype = $_FILES["file_img"]["type"];
    $filepath = "photo/".$filename;

    if(move_uploaded_file($filetmp, $filepath)){
        $result = mysqli_query($mysqli, "INSERT INTO upload_img (img_name,img_path,img_type) VALUES ('$filename','$filepath','$filetype')");
        if($result){
            $title = strip_tags($_POST['title']);
            $content = strip_tags($_POST['content']);
            $image = strip_tags($filepath);
            $statement = $mysqli->prepare("INSERT bikes (title,image,content) VALUES (?,?,?)");
            $statement->bind_param("sss",$title,$image,$content);
            $statement->execute();
            $statement->close();

            header('Location: admin.php');
            exit();
        }else{
            // error handling
        }
    }else{
        // error handling
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

btn_upload is mutually exclusive to add, so the if statement which executes for btn_upload doesn't also run for add.

If you want it to be a two-phase process (upload image, then add) then you have to set a hidden input whose value is the filepath.

However, ideally the user would fill in name and provide an image and then submit them together. In order to do that, place all inputs inside the same form element and remote the "Add" button.

1 Comment

Thank you man. I tried it and it works. I would never have thought that it will be so simple.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.