MySQL, PHP Using LIKE Syntax? - php

I'm trying to make a user search with the following code:
<?php
session_start();
include("../BD/bd.php");
$searched_for = $_POST['searched_for'];
$query = #mysql_query("SELECT * FROM user_media WHERE nombre LIKE '%$searched_for%'") or die(mysql_error());
while($got_users = #mysql_fetch_array($query)){
echo '<div class="searched-content-info">'.
'<div class="searched-photo"><img src="'.$got_users['foto'].'"></div>
<div class="searched-names"><h3>'.$got_users['nombre'].'</h3></div>
<div class="searched-dates"><h3>'.'Miembro desde: '.$got_users['created_on'].'</h3></div>
</div>
<div class="divisor-search-user"></div>';
}
?>
But I'm getting all the rows, I just want to display the searched users info, seems like the $query is receiving a clean $searched_for
Any help here? Btw, I'm a little newbie here, please don't bully :)
EDIT: I tried changing $got_users['nombre']; with $searched_for to see if $searched_for is empty and yes it doesn't return any string that's why I am getting all the rows. $query is getting an empty variable but Why?
Here's my HTML:
<form target="u-n" id="search_input" action="search_user.php" method="post">
<input id="search-input" name="searched_for" type="search" placeholder="Search">
</form>

You used <input type="search" /> which is a HTML5 feature. Older browsers may not support this. Replace this input with type="text".
Then, your $_POST['searched_for'] should populate properly, that is:
<input name="searched_for" type="text" placeholder="Search" />
Also, you used the same id multiple times, which is an invalid HTML syntax.
Reference: HTML input tag at MDN

Related

Using php to set value of html input field

I want to set the value of an input text field with php, but it does not seem to work.
There are many similar Questions where the answer was something like this:
<?php $value = "Show This!" ?>
<form>
<input type="text" name="testname" value="<?php echo $value;?>" >
</form>
But this will print the whole <?php... stuff in the text field like this.
For clarification:. I am expecting to see this.
What am I doing wrong?
Note: I also tried several variants with/without string symbols, also the shorthand variant <input type="text" value="<?=$value?>"> and even something without a php variable:
<form>
<input type="text" name="testname" value="<?php echo 'Show this please';?>" >
</form>
your code looks correct, you just need to run it as PHP instead of HTML

why isn't my search working?

I'm trying to get the php code to search the database and return all the matching "park_name"s but it says that the search variable is undefined and also only returns one park from the database.
This is the code I have for the search:
<form method="post" action="Search_page.php" name="search" id="Search">
<label for="search">Search:</label><input type="text" name="Search" id="search" />
<input type="submit" name="submit" value="Search"/>
</form>
<?php
if(isset($_POST['search'])){
$search = $_POST['search'];
$search = preg_replace("#[^0-9a-z]i#","", $search); }
$sql="SELECT Park_name, street FROM park_list WHERE park_name LIKE '%$search%'";
//query db
$result = $db->query($sql);
?>
</div>
<?php while ($row = $result->fetch_assoc()) { ?>
<div class="results">
<h2><?php echo $row['Park_name']?></h2> </br>
<p><?php echo $row['street']?></p>
</div>
<?php } ?>
Because, Search != search.
Error reporting told you about it too.
Btw, != is the logical operator for "does not equal" ;-)
Those are case-sensitive.
By the way; do yourself a favor and use a prepared statement if you want to save/keep your database.
https://en.wikipedia.org/wiki/Prepared_statement
and check for errors on the query, should it fail using mysqli_error($db).
You're also using a name attribute here in conjunction with the POST array of the same name:
<form method="post" action="Search_page.php" name="search" id="Search">
^^^^^^^^^^^^^
Remove that ^
and rename name="Search" for the input to name="search".
where you thought would pan out, which it won't. Your search is relying on the input's name attribute (and the input itself). Forms generally do not use name attributes.
You need to remove it.
Side note: It's usually best to use a !empty() < (not empty) for a user input, instead of isset(). The latter is mostly used for radios/checkboxes/submit inputs.
Form field names are case sensitive.
Change your second line to
<label for="search">Search:</label><input type="text" name="search" id="search" />
I don't have rep to comment yet, but Park_name should be lowercase.
You have inconsistent case in the sql statement:
$sql="SELECT Park_name, street FROM park_list WHERE park_name LIKE '%$search%'";

Cant insert all html form code from DATABASE into textarea

I have large html code inside my DATABASE. I need to get that into textarea for editing purpose. I can do that all but i have problem when it is displaying code into textarea. Half code is inside of textarea but other half display form not code and it display it out of textarea. (See Picture ).
Passing data from DATABASE:
<?php
include_once("scripts/connect.php");
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM formsAndCategories WHERE id=$id");
while($res = mysqli_fetch_array($result))
{
$category = $res['category'];
$form = $res['form'];
}?>
Displaying code from DATABASE (issue is with textarea. its displaying wrong on page):
<form name="form1" method="post" action="edit.php">
<label class="fb-textarea-label">Form Code</label><textarea name="form" class="form-control" rows="25"><?php echo $form;?></textarea>
<label class="fb-text-label">Category</label> <td><input type="text" name="category" class="form-control" value="<?php echo $category;?>"></td>
<input type="hidden" name="id" value="<?php echo $_GET['id'];?>"></td>
<input type="submit" class="btn btn-success" name="update" value="Update"></td>
</form>
So far i tryed to do that on plain page. I was thinking maby those all (bootstraps, jquery and css etc) are ruining it all. But nothing.
Then i found out that there is no character limit in textarea but anyway i tryed to set character limit with maxlength="200000".
Then i tryed different code because i was thinking maby there is problem with html form code but nothing.
If it is not possible in this way then maby there is different way to do this ?
If somebody can help me out with this it would be great.
Thank You.
Following code will escape HTML characters. In the code above, HTML characters are not escaped and directly appending HTML to text box is causing the HTML breakup.
May be following solution will work.
<textarea name="form" class="form-control" rows="25"><?php echo htmlentities($form);?></textarea>
or
<textarea name="form" class="form-control" rows="25"><?php echo htmlspecialchars($form);?></textarea>

How to retain search query after clicking search and printing result on same page using PHP

I have created my PHP page where I have search query field. After submitting query I am printing result on same page. The query is working fine but I want query to be displayed even after result displayed. i.e. query is being disappeared after result comes. How can I retain the query word/s along with result in webpage. This might be very basic and sounds like stupid but since I am newbie and tried so many ways but in vain.
Below is my code:
<html>
<head>
<TITLE>PHP FORMS</TITLE>
</head>
<body algin=center>
<p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Enter Drug Name <input type="text" name="drugName"></p>
<p><input type="submit" value="search"></p>
</form>
</body>
</html>
Can anyone suggest valuable idea and cause for it?
try this
<input type="text" name="drugName"
value="<?php echo (isset($_POST['drugName']) ? $_POST['drugName'] : '') ?>">
Change your form method to GET and append as many strings as you want. Then via PHP, use $_GET['varstring'] value.
OR if you must use POST
foreach ($_POST as $set => $myval){
echo "{$set} = {$myval}\n";
}

PHP form post to MySQL error

I'm creating a php-post form, containing: Who, What, Where, Contact and date_created.
I've made a database with these rows.
Here's my HTML Form code:
<form id="contactform" action="post.php">
<p class="contact"><label for="who">Who</label></p>
<input id="who" name="who" placeholder="Who are you? (First & Second name)" required="" tabindex="1" type="text">
<p class="contact"><label for="email">What</label></p>
<input id="what" name="what" placeholder="What do you want?" required="" type="text">
<p class="contact"><label for="username">Where</label></p>
<input id="where" name="where" placeholder="Country, City, Street..." required="" tabindex="2" type="text">
<p class="contact"><label for="password">Contact</label></p>
<input type="text" id="contact" name="contact" placeholder="Phone number or email"required="">
<br><br>
<input class="buttom" name="submit" id="submit" tabindex="5" value="Submit" type="submit">
And here's the php post.php code:
<?php
// Grab our POSTed form values
// Note that whatever is enclosed by $_POST[""] matches the form input elements
$who = $_POST["who"];
$what = $_POST["what"];
$where = $_POST["where"];
$contact = $_POST["contact"];
// Connect to our DB with mysql_connect(<server>, <username>, <password>)
$sql_connection = mysql_connect("server_name", "admin", "password");
mysql_select_db("database_name", $sql_connection);
$sql = "INSERT INTO content (
who,
what,
where,
contact,
date_created
)
VALUES (
'$who',
'$what',
'$where',
'$contact',
NOW()
)";
mysql_query($sql, $sql_connection);
mysql_close($sql_connection);
?>
When I try to post something, nothing is happening. The screen is just white, the database is empty and the url is like this:
http://my-website.com/post.php?who=Firstname+Secondname&what=Some+sentences+here-and&where=America&contact=some#website.com&submit=Submit%21
Just as HamZa DzCyberDeV said, you didn't specify which method you're using in <form> tag.
For situations when you're POSTing something in your database, just as you are now - use method="post" and for forms when you're searching for something, use method="get".
In case of using post method, your URL will change to only my-website.com/post.php and in case of using get method, your URL will change to something like my-website.com/post.php?... (where your things which you're getting are going) - just how you got URL after submitting.
The screen is just white because post.php (where you're going after clicking on submit button) doesn't contain anything to send to output, which you can easily do with echo.
For instance, you can make a new html page which will be written down with echo:
echo '
<html
<body>
This is my website!
</body>
</html>
';
Also, what you could do is to use include() php script which has already formed HTML, or you can check out here for some other redirect methods:
http://php.about.com/od/learnphp/ht/phpredirection.htm
Just remember that PHP is language which server is processing and only HTML tags (with CSS and JS) are sent to other browser to be read.
For more about POST and GET method you can read here:
http://php.net/manual/en/reserved.variables.post.php
http://php.net/manual/en/reserved.variables.get.php
why don't you try this to get an error or a clue to what is going wrong, enclose your code in try and catch blocks:
try {
// your code
} catch ( Exception $e ) {
echo $e->getMessage();
}

Categories