I use a Wordpress site, I do my search on a custom database called "operations". The search is performed on the website.
I need to get other results from the table related to this row on request, not just what I entered. And get other data related to this string. Here is the search form on the site:
<form method="post" action="https://site-name.com/wp-content/themes/theme/select_user.php">
<label for="sku">SKU:</label><br/>
<input type="text" name="sku" size="30"><br/>
<label for="barcode">Barcode:</label><br/>
<input type="text" name="barcode" size="30"><br/>
<input id="submit" type="submit" value="Search"><br/>
</form>
</fieldset>
The database has the following columns: id, date, title, size, sku, barcode, price
File Contents select_user.php:
require( __DIR__ . '/../../../wp-load.php' );
global $wpdb;
$sku = trim($_REQUEST['sku']);
$barcode = trim($_REQUEST['barcode']);
$sql_select = $wpdb->get_results(
$wpdb->prepare(
"
SELECT * FROM " . $wpdb->prefix . "operations
WHERE sku='$sku' || barcode='$barcode',
ARRAY_N
"
)
);
if ($sql_select)
{
foreach($sql_select as $row)
{
echo 'SKU: ' . $row['sku'] .'</br>';
echo 'Barcode: ' . $row['barcode'] .'</br>';
}
}
else {
echo 'No results';
}
With this code, I get the answer "no results".
I would be grateful for any help
use the post hook for wp forms not directly use .php file
add_action( 'admin_post_add_foobar', 'prefix_admin_add_foobar' );
function prefix_admin_add_foobar() {
// Handle request then generate response using echo or leaving PHP and using HTML
}
for your form use :
<form action="http://www.example.com/wp-admin/admin-post.php" method="post">
<input type="hidden" name="action" value="add_foobar">
<input type="hidden" name="data" value="foobarid">
<input type="submit" value="Submit">
</form>
I created my own shortcode, and placed everything that is necessary there. Everything works.
function custom_search_func( $atts ){
echo '<fieldset>
<form action="' . get_permalink() . '" method="POST">
<label for="sku">SKU:</label><br/>
<input type="text" name="sku" size="30"><br/>
<label for="barcode">Barcode:</label><br/>
<input type="text" name="barcode" size="30"><br/>
<label for="date">Date:</label><br/>
<input type="date" name="date" size="30"><br/>
<input type="submit" value="Search">
</form>
</fieldset>';
global $wpdb;
$sku = trim($_REQUEST['sku']);
$barcode = trim($_REQUEST['barcode']);
$date = trim($_REQUEST['date']);
$sql_select = $wpdb->get_results(
$wpdb->prepare(
"
SELECT id, date, title, size, barcode, sku, price FROM " . $wpdb->prefix . "operations
WHERE sku='$sku' || barcode='$barcode' || date='$date'
"
)
);
if ($sql_select)
{
echo '<table border="1" width="100%" cellpadding="5">';
echo '<tr><th>ID</th>';
echo '<th>Title</th>';
echo '<th>Size</th>';
echo '<th>SKU</th>';
echo '<th>Barcode</th>';
echo '<th>Price</th>';
echo '<th>Date</th></tr>';
foreach ( $sql_select as $id) {
echo '<tr>';
echo '<td>';
echo $id->id;
echo '</td>';
echo '<td>';
echo $id->title;
echo '</td>';
echo '<td>';
echo $id->size;
echo '</td>';
echo '<td>';
echo $id->sku;
echo '</td>';
echo '<td>';
echo $id->barcode;
echo '</td>';
echo '<td>';
echo $id->price;
echo '</td>';
echo '<td>';
echo $id->date;
echo '</td>';
echo '</tr>';
}
echo '</table>';
}
else {
echo 'No results';
}
}
add_shortcode( 'customsearch', 'custom_search_func' );
Use the [customsearch] shortcode to output this anywhere on the page
Related
I have an ideal search-box which searches item from a given listing. My searching is working fine, the problem is when I enter something in search box to search, it gives me result but my search box gets empty.
For eg If I search "Electronics" in search box, it gives me result of electronics but my search box gets empty. It should be written with "Electronic" when it gives me result.
Probably, I should be using GET method instead is it so?
Here is my code for searching:
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
if (!empty($_REQUEST['term']))
{
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM category WHERE cat_name LIKE '%" . $term . "%' or parent LIKE '%" . $term . "' or cat_status LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1)
{
$dynamicList="";
while ($row = mysql_fetch_array($r_query))
{
// $cat_id=;
/*$dynamicList .= '
<img style="border:#666 1px solid;" src="../storeadmin/category/thumbs/' . $row['cat_id'] . '.jpg" width="77" />';*/
echo "<tr bgcolor=''>";
echo "<td>" . $row['cat_id'] . "</td>";
echo "<td><img style='border:#666 1px solid;' width='70' src='http://localhost/jaymin/My%20Store/storeadmin/category/thumbs/". $row['cat_id'].".jpg' /></td>";
//echo "<td>".$dynamicList."</td>";
echo "<td>" . $row['cat_name'] . "</td>";
echo "<td>" . $row['parent'] . "</td>";
echo "<td>" . $row['cat_status'] . "</td>";
echo "<td><a href='categoryylisting_edit.php?id=" . $row['cat_id'] . "'>Edit</a></td>";
echo "<td><a name='delete' href='categoryylisting_edit.php?id=" . $row['cat_id'] . "'>Delete</a></td><tr>";
echo "</tr>";
}
}
else {
echo "Nothing should be displayed";
}
?>
</table>
change your code to
Search: <input type="text" name="term" value="<?php echo #$_REQUEST['term']; ?>" /><br />
instead of
Search: <input type="text" name="term" /><br />
Try this,
<input type="text" name="term" value="<?php if(isset($_POST['term'])){ echo $_POST['term']; } ?>"/>
If you want this in your url use GET instead of POST like,
<form action="" method="get">
Search: <input type="text" name="term" value="<?php if(isset($_GET['term'])){ echo $_GET['term']; } ?>" /><br />
<input type="submit" value="Submit" />
</form>
This is because you are submitting the form and after form submit textbox values disappears. To overcome this try:
<input type="text" name="term" value="<?php if(isset($_POST['term'])){ echo $_POST['term']; } ?>"/>
I am having a table with <input type="text" name="' . $r['0'] . '" value="' . $r['0'] . '"
populated from data that i fetch from database like this:
echo '<form id="actions" name="nonValidMainForm" method="post"><table border="2" width="100%">';
echo "<tr><td><b>Index</b></td><td><b>Email </b></td> <td><b>Date</b></td> <td><b>Name</b></td> <td><b>Surname</b></td> <td><b>Telephone Number</b></td> <td><b>Street Adress</b></td><br/>";
while($r = mysql_fetch_array($result)) {
$my[] = $r['0'];
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="' . $r['0'] . '" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo "<pre>";
print_r($my);
echo "</pre>";
if(isset($_POST['unsubscribe'])){
foreach($my as $key=>$value){
$email = $value;
}
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
echo '<button style="position:fixed;bottom:5;left:5;">Change</button>';
echo '</table></form>';
The table looks like this:
I have tried this:
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
But the value is empty
So each time i press unsubscribe button the corresponding email to be deleted. How is this possible?
Your form has many elements with the same name. How can the browser determine which element's value to send to the server when the form is posted? Generally the last one takes precedence, but I suspect that behavior may be undefined and browser-specific.
If each individual table row needs to be a separately post-able form, then each row needs its own form:
echo '<td>
<form method="POST" action="somePage.php">
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</form>
</td>';
That way when the browser posts the form to the server, it knows specifically which email and unsubscribe elements to use. Since there's only one of each for that form.
You have to wrap your inputs in a <form> tag.
echo '<form>';
while($r = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo '</form>';
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
Based on your code above it looks like it's a syntax error. Try the update below
if(isset($_POST['unsubscribe'])){
$email = $_POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' ); </script>";
}
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I have a error problem with my code in php. can anyone help me to correct it!
<?php $query_tbl = "SELECT transaksi.id_transaksi, pelanggan.nama FROM transaksi, pelanggan ".
"WHERE transaksi.id_pelanggan = pelanggan.id_pelanggan ORDER BY id_transaksi";
$list_tbl = mysqli_query($dbc, $query_tbl)
or die('Error select table');
while($row = mysqli_fetch_array($list_tbl))
{
echo '<tr>';
echo '<td>' . $row['id_transaksi'] . '</td>';
echo '<td>' . $row['nama'] . '</td>';
<form method="post" action="pengiriman-input.php">
<input type="hidden" name="id" value="<?php echo $row['id_transaksi']; ?>" />
<input type="submit" value="Kirim" />
</form>
echo '</tr>';
};?>
In Chrome i get this notif:
Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\delivery\pengiriman.php on line 33.
I don't know what is wrong because i am new with php.
And btw line 33 is
<form method="post" action="pengiriman-input.php">
there is some syntex error while putting HTML between PHP near your form tag
<?php $query_tbl = "SELECT `transaksi.id_transaksi`, `pelanggan.nama` FROM `transaksi`, `pelanggan` WHERE
`transaksi.id_pelanggan` = `pelanggan.id_pelanggan` ORDER BY `id_transaksi`";
$list_tbl = mysqli_query($dbc, $query_tbl)
or die('Error select table');
while($row = mysqli_fetch_array($list_tbl))
{
echo '<tr>';
echo '<td>' . $row['id_transaksi'] . '</td>';
echo '<td>' . $row['nama'] . '</td>';?>
<form method="post" action="pengiriman-input.php">
<input type="hidden" name="id" value="<?php echo $row['id_transaksi']; ?>" />
<input type="submit" value="Kirim" />
</form>
<?php echo '</tr>';
};?>
Try this more simple and neat code
<?php
$query_tbl = "SELECT transaksi.id_transaksi, pelanggan.nama FROM transaksi, pelanggan ". "WHERE transaksi.id_pelanggan = pelanggan.id_pelanggan ORDER BY id_transaksi";
$list_tbl = mysqli_query($dbc, $query_tbl) or die('Error select table');
?>
<?php while($row = mysqli_fetch_array($list_tbl)): ?>
<tr>
<td><?php echo $row['id_transaksi']; ?></td>
<td><?php echo $row['nama']; ?></td>
<td>
<form method="post" action="pengiriman-input.php">
<input type="hidden" name="id" value="<?php echo $row['id_transaksi']; ?>" />
<input type="submit" value="Kirim" />
</form>
</td>
</tr>
<?php endwhile; ?>
You can't put HTML directly into PHP
<?php $query_tbl = "SELECT transaksi.id_transaksi, pelanggan.nama FROM transaksi, pelanggan ".
"WHERE transaksi.id_pelanggan = pelanggan.id_pelanggan ORDER BY id_transaksi";
$list_tbl = mysqli_query($dbc, $query_tbl)
or die('Error select table');
while($row = mysqli_fetch_array($list_tbl))
{
echo '<tr>';
echo '<td>' . $row['id_transaksi'] . '</td>';
echo '<td>' . $row['nama'] . '</td>';
?> <!-- here ends the PHP -->
<form method="post" action="pengiriman-input.php">
<input type="hidden" name="id" value="<?php echo $row['id_transaksi']; ?>" />
<input type="submit" value="Kirim" />
</form>
<?php // here begin PHP again
echo '</tr>';
};?>
You need to end the php then start it again. Try this:
<?php $query_tbl = "SELECT transaksi.id_transaksi, pelanggan.nama FROM transaksi, pelanggan ".
"WHERE transaksi.id_pelanggan = pelanggan.id_pelanggan ORDER BY id_transaksi";
$list_tbl = mysqli_query($dbc, $query_tbl)
or die('Error select table');
while($row = mysqli_fetch_array($list_tbl))
{
echo '<tr>';
echo '<td>' . $row['id_transaksi'] . '</td>';
echo '<td>' . $row['nama'] . '</td>';
?>
<form method="post" action="pengiriman-input.php">
<input type="hidden" name="id" value="<?php echo $row['id_transaksi']; ?>" />
<input type="submit" value="Kirim" />
</form>
<?php
echo '</tr>';
};?>
In the above snippet you are opening a php tag in line 1 and have thought of closing it at last line. Between these two tag you cannot write the html script for creating form, as you are doing, in a php file.
It will be taken as a php script instead of html script and php compiler don't understands html tags.
Create the form tag as a php string variable and echo it as you are doing in the lines above.
I have a database containing books. On a page, I have loop that prints each record in the database which each book's title, author, publisher, date, and rating. I want to use a delete button at the bottom of each record in order to delete it.
When I click on the delete button, the page is updated, but the record is not deleted.
I have tried to find solutions to this problem, but have yet to. I tried to use the book_id category in my database table as my index, in order to identify each record and delete it but it does not work.
Here is the code for my button form, as it appears with html code:
while ($row = mysqli_fetch_array($result))
{
echo '<div id="forminput">';
$timestamp = strtotime($row['date']);
echo '<div id = "bookpicture">';
echo '<img src="images/Book Cover 8crop.jpg" alt="Book Cover">';
echo '</div>';
echo '<div id = "bookinfo">';
echo '<div id = "titleauthor">';
echo '<h3>' . htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8') . '</h3>';
echo '<p>' . htmlspecialchars($row['author'], ENT_QUOTES, 'UTF-8') . '</p>';
echo '</div>';
echo '</div>';
$id = htmlspecialchars($row['book_id'], ENT_QUOTES, 'UTF-8');
echo '</div>' ;
echo '<div id = "publisher">' ;
echo '<p>' . 'Published on' . " " . htmlspecialchars(date('F j, Y,', $timestamp),
ENT_QUOTES, 'UTF-8') .
" " . 'by' . " " . htmlspecialchars($row['publisher'], ENT_QUOTES, 'UTF-8') . '</p>';
echo '</div>';
echo '<div id = "formDelete">';
echo '<form name="deleteForm" method="post" id="deleteForm" action="index.php">';
echo '<input type="submit" value="Update" name="myAdd" id="myAdd" style = "width:
100px" required>';
echo '<input type="submit" value="Delete" name="myDelete" id="$id" style = "width:
100px" required>';
echo '</form>';
echo '</div>';
echo '</div>';
echo '<hr>' ;
echo '</div>';
}
?>
Here is the code from my index.php file.
else if (isset($_POST['myDelete']))
{
$ids = mysqli_real_escape_string($link, $_POST['$id']);
$sql="DELETE FROM readbooks WHERE book_id = '$ids'";
if (!mysqli_query($link, $sql))
{
$error = 'Error with submission: ' . mysqli_error($link);
include 'php/error.html.php';
exit();
}
}
Here is the updated code.
The Problem is you are not trying to send the row ID from the form.
In Form try sending row id from the form
echo '<input type="hidden" name="id" value="$id">'
try receiving that parameter
$id = $_POST['id'];
$con = mysql_connect("host address","mysql_user","mysql_pwd");
$query = "DELETE FROM readbooks WHERE id = $id";
mysql_query($query,$con);
Moving from comment, you're not actually getting the $id anywhere. Add a field to your form:
<input type='hidden' name='id' value='$id'>
and then refer to it in your php:
$ids = mysqli_real_escape_string($link, $_POST['id']);
Since your using mysqli now, use prepared statements. Do not directly use your user input to the query! Example:
$books = array();
// connection
$con = new mysqli('localhost', 'your_username', 'password_of_username', 'your_database');
if(isset($_POST['myDelete'])) {
$book_id = $_POST['myDelete']; // get the variable id
$stmt = $con->prepare('DELETE FROM readbooks WHERE book_id = ?');
$stmt->bind_param('i', $book_id);
$stmt->execute();
}
$result = mysqli_query($con, 'SELECT * FROM readbooks');
while($row = $result->fetch_assoc()) {
$books[] = $row;
}
?>
<form method="POST">
<table border="1" cellpadding="10">
<tr>
<th>Title</th>
<th>Author</th>
<th>Publisher</th>
<th></th>
</tr>
<?php foreach($books as $book): ?>
<tr>
<td><?php echo $book['title']; ?></td>
<td><?php echo $book['author']; ?></td>
<td><?php echo $book['publisher']; ?></td>
<td><button type="submit" name="myDelete" value="<?php echo $book['book_id']; ?>">Delete</button></td>
</tr>
<?php endforeach; ?>
</table>
</form>
I think you're having problem with the id that you are passing to your PHP code. Try to use var_dump($_POST) to see what is the content of your $_POST variable. As I can see, the index of $_POST is wrong.
<input type="submit" value="Delete" name="myDelete" id="$id" style = "width:100px" required>';
Try to change this to
<input type="submit" name="book_id" value="$id" style = "width:100px" required>';
In your php use this $_POST["book_id"] to get the value of book_id you posted.
Note : The attribute "name" of the input tags will be the index of your $_POST variable
I understand that you want to have a delete and update button at the same time. The problem is your logic behind the situation. You need to have a hidden field and put the id inside it. Try to use this.
echo '<form name="deleteForm" method="post" id="deleteForm" action="index.php">';
echo '<input type="hidden" value="$id" name="book_id" id="myAdd" required>';
echo '<input type="submit" value="Update" name="action" id="myAdd" required>';
echo '<input type="submit" value="Delete" name="action" id="$id" required>';
echo '</form>';
In your php code use try to have a condition like this :
$book_id = $_POST["book_id"];
if($_POST["action"] == "delete")
{
//do your deletion code here. use the variable $book_id
}
else{
//do your update code here.use the variable $book_id
}
I am trying to get the values of a dynamically created set of checkboxes in PHP but apparently I couldn't get it. The source codes are below.
The "managestaff.php" page would allow searching for staff via their names and throws out a list of names with checkboxes for the admin to check them and click on a "delete" button at the bottom to delete the staff whom are being checked.
The deletion would be done on "deletestaff.php" as the "delete" button on "managestaff.php" simply forwards these values to "deletestaff.php" to do deletion work of the staff.
"managestaff.php" page codes:
<b><h3>Manage Staff</h3></b><br/>
<form action="managestaff.php" method="POST">
<input name="form" type="hidden" id="form" value="true">
<table width=300>
<tr>
<td width=112>Staff Name: </td>
<td width=188><input type="text" class="textfield" name="sname" /><br/></td>
</tr>
</table><br/>
<input type="submit" value="submit" />
</form>
<?php
if (isset($_POST['form']) && (isset($_POST['sname'])) && $_POST['form'] == 'true') {
$space = ' ';
$staffname = mysql_real_escape_string($_POST['sname']);
$query = 'SELECT * from staff where staffname like \'%' . $staffname . '%\'';
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) != 0) {
echo '<br><br>';
echo '<table>';
echo '<tr><th>Staff ID' . $space . '</th><th>Staff Name' . $space . '</th></tr>';
echo '<form action="deletestaff.php" method="POST">';
echo '<input name="delstaffform" type="hidden">';
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>' . $row['staffid'] . '</td><td>' . $row['staffname'] . '</td>';
// :Begin - dynamic checkbox generation for deleting staff
echo '<td>';
echo '<input type="checkbox" name="delstaff" value="' . $row['staffid'] . '" />';
echo '</td>';
// :End
echo '</tr>';
}
echo '<tr align="right"><td colspan="3"><input type="submit" value="delete"/></td></tr>';
echo '</form>';
echo '</table>';
}
}
?>
"deletestaff.php" page codes:
<?php
print_r('POST: ' . $_POST);
echo '<br>';
if (isset($_POST['delstaffform']) && isset($HTTP_POST_VARS)) {
echo 'Submission of delstaffform FOUND !';
echo 'Staff to delete' . $HTTP_POST_VARS['delstaff'];
}
else{
echo 'Submission of delstaffform NOT FOUND !';
}
?>
The "deletestaff.php" doesn't do delete for now as it's a test page.
The current output I get is "Submission of delstaffform NOT FOUND !".
Thanks for the solutions.
Try this:
<input type="checkbox" name="delstaff[]" value="' . $row['staffid'] . '"/>';
print_r your $_POST and you'll see it sticks your submissions nicely into an array for you.
<?php
if (isset($_POST['delstaff']) && is_array($_POST['delstaff'])) {
echo 'Submission of delstaffform FOUND !';
$array = $_POST["delstaff"];
foreach($array as $value){
echo "<br>Value: ".$value."<br>";
}
} else {
echo 'Submission of delstaffform NOT FOUND !';
}
?>
Found the answer on my own but nevertheless you are helpful :D . Thanks a lot.