0

My PHP codes don't work because I'm using a localhost instead of a host server??Or may be because I put tag not in the head/body tag of the HTML file??

Hi!

I store data in my SQLite database and I tried using PHP to let the users search for those data and see them through HTML. I followed this Youtube video as the tutorial: https://youtu.be/PBLuP2JZcEg but I couldn't make it work. When I input something(for example: new) in the search bar and submit it, nothing happens, no search results are shown. (Aside from the website link change to "http://127.0.0.1:5000/?search_problem=new&search_problem=")

I'm struggling very hard with this. I would greatly appreciate if you could help me. Thank you.

Below are my codes and some screenshot of the website that may help you to understand how to help me better:

-My PHP codes ( I put these using the PHP tags inside my HTML files):

<?php
sqlite_connect("blog_id", "problem_name","text") or die ("couldn't find relating problem");
sqlite_select_db("data.sqlite") or die ("couldn't find relating problem");
$output = "";

//collect
if(isset($_POST['search_problem'])){
   $search_problem_query = $_POST['search_problem'];
   $query = sqlite_query("SELECT * FROM blog_post WHERE problem_name LIKE '%$search_problem_query%' OR text LIKE '%$search_problem_query%' LIMIT 30") or die("couldn't find relating problem");
   $count = sqlite_num_rows($query);
   if ($count == 0){
      $output = "There was no matched problems";
   } else {
      while $row = sqlite_fetch_array($query){
          $problem_name = $row['problem_name'];
          $text= $row['text'];
          $id= $row['blog_id'];
          $output .= " '.$problem_name.' '.$text.' ";
      }
   }


}

?>

-Then I print out the output or result of the search that was found later on in the HTML file:

<form class="form-inline my-2 my-lg-0 text-right">
      <input class="form-control mr-sm-2" type="search" placeholder="Search problem" aria-label="Search" name="search_problem">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
    <?php print("$output"); ?>


My database name is data.sqlite

My database name is data.sqlite

-My codes for the data table "blog_post":

class BlogPost(db.Model):
    __tablename__ = 'blog_post'
    users = db.relationship(User)

    blog_id = db.Column(db.Integer, primary_key=True)
    # blog_next = db.Column(blog_id()+1)
    user_id = db.Column(db.Integer,db.ForeignKey('users.id'), nullable=False) #users.id  is taken from the tablename(users) and id in its table
    date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)  #Automatically post the time of the post
    problem_name = db.Column(db.String(140), nullable=False)
    text = db.Column(db.Text, nullable=False)
    blog_image = db.Column(db.String(140), nullable=False, server_default='default_blog.jpg')


    def __init__(self, text, problem_name, user_id, blog_image):
        self.text = text
        self.problem_name = problem_name
        self.user_id = user_id
        self.blog_image = blog_image

Also, to clarify, my data table "blog_post" has a column "problem_name" named "New life 3":

enter image description here

Upchanges
  • 310
  • 2
  • 14
  • This: `LIKE '%search_problem_query%'` will literally search for the string `search_problem_query` (you're missing `$` in front of the variable). Instead of using `sqlite_*`, I would recommend using PDO and prepared statements. Using user data directly in a query opens you up for SQL injection attacks. – M. Eriksson May 11 '20 at 14:24
  • Thank you so much for telling me about SQL injection attacks! I literally don't know anything about that. I will research PDO and use it! But after I put $ in '%search_problem_query%', my website is still showing nothing (after I click the search button (with an input "new") ). But, thank you for helping me! – Upchanges May 11 '20 at 14:33
  • Showing nothing or showing any of your error messages? If it's nothing (white page), then I would guess that you have some other errors as well. You either need to check your web servers error log or [set PHP to show all errors and warnings](https://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings) to find out exactly where it goes wrong. – M. Eriksson May 11 '20 at 14:37
  • Oh. I just noticed that both the input field and the submit button have the same name. Change the name of the button to be different from the input. Or you can even remove the name from the button completely. – M. Eriksson May 11 '20 at 14:39
  • My website still shows everything else, the blogs, images, texts... but it doesn't show any search result after I click the search button(with an input). – Upchanges May 11 '20 at 14:40
  • Ok! I'm on it now! – Upchanges May 11 '20 at 14:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213624/discussion-between-upchanges-and-magnus-eriksson). – Upchanges May 11 '20 at 14:42

0 Answers0