This is my first contribution here:
So I am using PHP and MySQLi. I have a search bar and it works great,
but I want to entries to disappear when I clear the search.
Using the search bar is changing the URL:
without search: http://localhost:52694/index.php
with search: http://localhost:52694/index.php?search=test
If I clear my search bar, it is still the same URL (http://localhost:52694/index.php?search=test) and I cant click go because it tells me that the box can't be empty to start search.
I hope everything is clear and you understand my issue.
This is my search in php code:
<?php
if(isset($_GET['search'])){
$filtervalues = $_GET['search'];
$query = "SELECT * FROM table WHERE CONCAT( value, name, zla ) LIKE '%$filtervalues%' ";
$query_run = mysqli_query($con, $query);
if(mysqli_num_rows($query_run) > 0) {
foreach($query_run as $items) {
?>
<tr>
<td><?= $items['value']; ?></td>
<td><?= $items['name']; ?></td>
<td><?= $items['zla']; ?></td>
<td>
<a class='btn btn-danger btn-sm' href='/delete.php?id=<?php echo $items['id']; ?>'>Del</a>
</td>
</tr>
<?php
}
} else {
?>
<tr>
<td colspan="4">No Record Found</td>
</tr>
<?php
}
}
?>
From
I cant click go because it tells me that the box can't be empty to
start search.
I assume that in your HTML you have the required attribute present in the input field.
Here's an example using required :
<form action="index.php" method="GET">
Search for : <input name="search" required/>
<input type="submit" value="send"/>
</form>
<?php
if(isset($_GET["search"]))
echo $_GET["search"];
else
echo "nothing to search for";
using example like this: if your search field is empty, a little box will pop telling you to fill the field.
So please give brief view of your html search bar code , or check if required is present, if so .. delete it, and use it only for the fields that must be filled like in login or ...
Related
Hye, i'm working on a small project of Inventory system. Everything is okay until this last part. I have included a delete button at the end of each row for user to delete any item of choice, but when user press the delete button, it deleted the last row of the table, instead of the row/item of choices, where is my mistake in my coding?
Thank you!
<?php
$result= mysql_query("SELECT * from Staff ORDER BY status");
$count=1;
while($row=mysql_fetch_array($result)){
?>
<tr>
<td align="left"><?php echo $count; ?></td>
<td align="left"><?php echo $row['staffId']; ?></td>
<td align="left"><?php echo $row['name']; ?></td>
<td align="left"><?php echo $row['address'];?></td>
<td align="left"><?php echo $row['pNum'];?></td>
<td align="left"><?php echo $row['status'];?></td>
<td align="left"><?php echo $row['type'];?></td>
<td><input type="submit" name="deleteStaff" value="Delete" onClick="displayMessage()">
<input type="hidden" name="staffId1" value="<?PHP echo $row['staffId']; ?>">
</td>
</tr>
</tbody>
<?php
$count++; }
if(isset($_POST['deleteStaff'])){
$id=$_POST['staffId1'];
$result=mysql_query("DELETE from Staff WHERE staffId='$id' ");
if($result){
echo '<script> location.replace("viewStaff.php"); </script>';
}
else{
?>
<script>
alert ("Fail to delete data")
window.location.href='viewStaff.php'
</script>
<?PHP
}
}
?>
I've encountered this error before as well. I think the error is happening since you're calling the same "onclick" function for each array result.
Instead, create a link to a page where you can run the php deletion script and pass the staffID into the URI and Get that in the php deletion script that you created.
example
<form action="deletion_script.php?staffid=<?php echo $staffid; ?>">
Put that inside of the td tag
Then in your deletion_script.php file, put something like this
<?php
//get staffid from URI
$staffid = $_GET['staffid'];
$sql="DELETE FROM $table_name WHERE staffid = '$staffid'"
$result = mysqli_query($connect, $sql);
if($result) {
//send back to the page you want
header("Location: ../inventory.php");
}
?>
This worked for me, and how I handle all situations like this from now on. You'll need to adjust the code a little to fit your set up.
since the name for the hidden field is the same for all rows being generated the post request is pulling the value of the last row( also "staffId1" ).
I recommend either using a individual form for each row in which case your code doesn't change as much or use js to get the required staffID using selectors
If you include the same hidden field (StaffId1) for every record it will contain more then one value.
You are now listening to the pressed SUBMIT so you don't need the hidden field. You can just get the value if the pressed SUBMIT.
Button
type="submit" name="staffId" value="<?php echo $id ?>"
Php
$id=$_POST['staffId'];
I created a search bar in html that searches my mySQL database and displays the results for my search.
My problem is that if I haven't typed anything on the bar it will display the whole table when I do not want that.
My code is this
<form action="Artists.php" method="post">
<input type="text" name="searchdatabase"<br><br>
<input type="submit" name="searching" value="Search"><br><br>
and I am trying to do this
<?php
if( ! mysqli_num_rows($resultsearch) ) {
echo "Mysql error: " .mysqli_error($mysql); }
while($row = mysqli_fetch_array($resultsearch)):?>
<tr>
<td><?php echo $row['firstname'];?></td>
<td><?php echo $row['lastname'];?></td>
</tr>
<?php endwhile;?>
But this is not working,
I am trying to create an if statement on html (I cant use Javascript) so that my code will not display my entire table and will only display the search I wanted once I press the search button.
Why do you have endwhile? Just use {} for your loop, match your searchdatabase to one of your names in the mysql. If it doesn't appear in the database throw a error. Other wise have it display. Right now you are calling your whole table. $blah = $con, "Select * From somedata where searchdatabase = firstName"; if you need to order by a certain way order by asce or something like that hope that helps.
just make the input as 'required'
<input type="text" name="searchdatabase" required>
this way you cant leave the search bar unfilled.
this codes below came from my friend. i want to make a search under this codes . from this codes , it will display all the users to delete. what i want is i want to put search box and search button . so when i insert user id and press search only then it display which user that i want . where should i put this search text , button and php ? i want to put it under this else if codes . help me. im new :(
else if($_SESSION['jawatan'] == 'ADMIN') { ?>
<li> VIEW PROFILE </li>
<li> VIEW REQUEST </li>
<li>LOG OUT</li>
<li><img src="msc.jpg" width = "240" height ="80"></li>
<form method="get">
<input type="text" name="userid" placeholder="search" />
<inpu type="submit" value="Search" />
</form>
<h3>View User</h3>
<table border='1'>
<tr>
<td><b>#</b></td>
<td><b>Nama</b></td>
<td><b>Email</b></td>
<td><b>Division</b></td>
<td><b>Department</b></td>
<td><b>Delete</b></td>
</tr>
<?php
$i = 1;
$whr="";
if(isset($_GET['userid'])){
$whr.= " and mem_id='".$_GET['userid']."'"; // which field you want. If you want to do search name use LIKE instead =(equal to)
}
$result = mysql_query("SELECT * FROM members WHERE mem_role='USER' ".$whr);
if(mysql_num_rows($result)>0) {
while($row = mysql_fetch_array($result)) {
?>
<tr>
<td>
<?php echo $i; ?>
</td>
<td>
<?php echo $row['mem_name']; ?>
</td>
<td>
<?php echo $row['mem_email']; ?>
</td>
<td>
<?php echo $row['mem_division']; ?>
</td>
<td>
<?php echo $row['mem_department']; ?>
</td>
<td>Delete</td>
</tr>
<?php
$i++;
}
} else {
echo '<tr><td>No results found</td></tr>';
}
?>
</table>
<?php
}
HTML
<form method="get">
<input type="text" name="userid" placeholder="search" />
<inpu type="submit" value="Search" />
</form>
PHP
$whr="";
if(isset($_GET['userid'])){
$whr.= " and mem_id='".$_GET['userid']."'"; // which field you want. If you want to do search name use LIKE instead =(equal to)
}
MYSQL
$result = $result=mysql_query("SELECT * FROM members WHERE mem_role='USER' ".$whr);// add whr variable here
I can tell you the steps:
Create search text field and search button ( wrap in form if you want to do with php only)
on click of button ( that would be submit ), fetch that value from search text box and run query based on that
when results appear , fill the table data with that results using foreach.
hope it helps. Ask me for any issue.
I'm attempting to make a master-detail search page for my intranet that will allow me to search both staff and hardware from the same page. I've gotten part of the way there but I've gotten myself stuck and need some help.
I have the inventory page (index.php) setup and I've included a form with two button - "Staff" and "Hardware" - to include the master-details .php pages I've designed.
The code for this is as follows:
<div class="content">
<h1>Inventory</h1>
<p></p>
<form action="" method="post">
<input type="submit" name="Staff" value="Staff" />
<input type="submit" name="Hardware" value="Hardware" />
</form>
<p></p>
<p></p>
<?php
if (isset($_POST['Staff']))
{
include 'details.php';
include 'lib/staffsearch.php';
};
if (isset($_POST['Hardware']))
{
include 'detailhw.php';
include 'lib/hwsearch';
};
?>
</div>
Again, this works fine - I click "staff" and it loads both my "staffsearch.php" and "details.php" pages. This is where I run into trouble however. My staffsearch.php includes a form that queries my staff database and returns the matching results in a table. I should be able to click on one of the results and the detailed information is displayed in the details box above. What happens though is that I click on the "Staff" button, my includes are displayed, I enter the search criteria, click "search" and get a blank page as a result.
If I click on the "staff" button again though my includes are reloaded along with the correct search result. Accessing the details however results in the same problem - once my results are displayed and I click on one instead of the details I receive a blank page until I click the "staff" button again - then my includes are reloaded and my results are displayed.
Is there anyway that when I click the search button on my staffsearch form that I can force the index page to reload the includes without having to click the "staff" button again?
Thanks, ahead of time for any advice as I'll throw in the mandatory. "I'm new to php" and my Googling hasn't really netted me any good results. If you need anymore information just ask!
EDIT 1
The code for my searchstaff.php page is as follows:
<table width="598px" border="1" align="center">
<tr valign="middle">
<td alight="left" colspan="3"> <form id="form1" name="form1" method="get" action="">
<label for="name">Search:</label>
<input type="text" name="name" id="name" /><input type="submit" name="search" id="search" value="Submit" style="margin-left:10px";/>
</form>
</td>
<tr>
<td align="center" width="40px">No.</td>
<td width="300px">Name</td>
<td width="158px">IP Address</td>
</tr>
<?php do { ?>
<tr>
<td align="center"><?php echo $row_inventorystaff['staff_id']; ?><?php echo $row_DetailRS1['staff_id']; ?></td>
<td> <?php echo $row_inventorystaff['f_name']; ?> <?php echo $row_inventorystaff['l_name']; ?><?php echo $row_DetailRS1['f_name']; ?> <?php echo $row_DetailRS1['l_name']; ?></td>
<td><?php echo $row_inventorystaff['ip_address']; ?><?php echo $row_DetailRS1['ip_address']; ?></td>
</tr>
<?php } while ($row_inventorystaff = mysql_fetch_assoc($inventorystaff)); ?>
</table>
<br />
<table border="0" style="margin-left:15px;" >
<tr>
<td><?php if ($pageNum_inventorystaff > 0) { // Show if not first page ?>
First
<?php } // Show if not first page ?></td>
<td><?php if ($pageNum_inventorystaff > 0) { // Show if not first page ?>
Previous
<?php } // Show if not first page ?></td>
<td><?php if ($pageNum_inventorystaff < $totalPages_inventorystaff) { // Show if not last page ?>
Next
<?php } // Show if not last page ?></td>
<td><?php if ($pageNum_inventorystaff < $totalPages_inventorystaff) { // Show if not last page ?>
Last
<?php } // Show if not last page ?></td>
</tr>
</table>
<p style="margin_left:15px">Records <?php echo ($startRow_inventorystaff + 1) ?> to <?php echo min($startRow_inventorystaff + $maxRows_inventorystaff, $totalRows_inventorystaff) ?> of <?php echo $totalRows_inventorystaff ?>
</p>
I have screen shots of the process of clicking through the index and performing a search that I can upload if it helps the explanation.
I would use a radio button to make a decision which will be easier to parse. Double submit buttons are a funny thing and some browsers dont like that.
<input name="dept" value="staff" type="radio"> Staff
<input name="dept" value="hardware" type="radio"> Hardware
<?php
if( isset($_POST['dept'] && $_POST['dept'] == 'staff' ) {
include 'staffForm.php';
} else {
include 'hardwareForm.php';
}
?>
I have gotten the id numbers of users from my database, and I want to make a button for each user. My code makes a table that shows all the IDs and creates a button for each one. I'm having trouble figuring out how to get the name of those buttons for use in other code. The error I am getting is "undefined variable" (in the 3rd line), which I am most likely getting because I am going at getting the button names wrong.
Basically, the $_POST in the third line is wrong (among perhaps other things). My question is how would one get the name (or id?) of the buttons I have made: how should I fix the $_POST or should I use something else entirely?
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST[$n])) header("location:" . $n . ".php");
}
?>
<div id="mod_user">
<table id='mod_table'>
<th class='ttop'>#</th>
<th class='ttop'>Page</th>
<?php
$result = $db->prepare("SELECT * FROM User");
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
$n=$row["UserID"];
?>
<form action="" method="post">
<tr>
<td class='tben'><?php echo $n; ?></td>
<td class='tben'><button type='submit' name=<?php echo $n; ?> >Go here</button></td>
<br />
</tr>
</form>
<?php
} ?>
</table>
</div>
You can try like this:
<td class='tben'><button type='submit' name="usernames[<?php echo $n ?>]" >Go here</button></td>
So you can get button name from $_POST["usernames"] array as below
foreach($_POST["usernames"] as $username => $btn_value)
echo "$username => $btn_name";