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 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":