I've been trying to develop a code where I have a form to search SQL tables according to date ranges and a checkbox status.
I already have worked out the search query for the dates, but I haven't been able to make it work with the checkbox. I would like to combine both features in one search but I can't figure it out.
For ex: Search between "01/01/2012" and "12/31/2012" where status is "DONE! (Checkbox=Checked)".
Here is the code I'm using to do the search:
The Form
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>">
<table>
<tr>
<td style="text-align:center; padding-top:15px;">
<span>From :</span>
<input type = "date" name = "OLD">
To:
<input type = "date" name = "NEW">
Status:
<input type='checkbox' name='Status' value='DONE'/>
</td>
</tr>
<tr>
<td style="text-align:center; padding-top:15px;">
<button type = "submit" name = "search" value = "Search" class="button orange">Search</button>
<button type = "reset" value = "Clear" class="button orange">Reset</button>
</td>
</tr>
</table>
</form>
The PHP
<?php
if(!isset($_POST['search']))
{
?>
<?php
}
else
{
$OLD = trim($_POST['from']);
$NEW = trim($_POST['to']);
$connection = mysql_pconnect("HOST", "USER", "PASS") or die("Connection failed. ".myslq_error());
mysql_select_db("DATABASENAME") or die("Unable to select db. ".mysql_error());
$query = "SELECT * FROM table WHERE Date >= '$OLD' AND Date <= '$NEW' ORDER BY date ASC";
$result = mysql_query($query) or die(mysql_error());
echo "<table class='table' id='SearchResult' cellspacing='0' cellpadding='0'>";
echo "<tr class='rowa'><b>";
echo "<td class='col1 cell'>Name</td>";
echo "<td class='col2 cell'>Last Name</td>";
echo "</tr>";
echo "</table>";
while($record = mysql_fetch_object($result))
{
echo "<table class='table' id='SearchResult' cellspacing='0' cellpadding='0'>";
echo "<tr class='rowb'>";
echo "<td class='col1 cell'>".$record->Name."</td>";
echo "<td class='col2 cell'>".$record->LastName."</td>";
echo "</tr>";
echo "</table>";
}
}
?>
Who can lead me in the right way to accomplish this?! Thanks a lot!
You can do it like this:
else
{
$OLD = trim($_POST['from']);
$NEW = trim($_POST['to']);
$status = isset($_POST['Status']) ? "AND status = 'DONE' " :"AND status = 'NOT DONE' ";
$connection = mysql_pconnect("HOST", "USER", "PASS") or die("Connection failed. ".myslq_error());
mysql_select_db("DATABASENAME") or die("Unable to select db. ".mysql_error());
$query = "SELECT * FROM table WHERE Date >= '$OLD' AND Date <= '$NEW' ".$status."ORDER BY date ASC";
$result = mysql_query($query) or die(mysql_error());
I think the query composition must be something like that:
$query = "SELECT * FROM table WHERE 1 = 1 "
if ($_POST['MyCheckbox']=="checked")
$query = $query . " and Date >= '$OLD' AND Date <= '$NEW' ORDER BY date ASC";
Related
I am using the following code to display certain rows from my database table:
<?php
$searchtype=$_POST['searchtype'];
$searchterm=$_POST['searchterm'];
$searchterm= trim($searchterm);
if (!$searchtype || !$searchterm)
{
echo 'Error';
exit;
}
if (!get_magic_quotes_gpc())
{
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}
$db = include "connect2db.php";
$query = "select * from notes where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
echo '<p>Number of rows found: '.$num_results.'</p>';
for ($i=0; $i <$num_results; $i++)
{
$row = $result->fetch_assoc();
echo '<i>';
echo stripslashes($row['date']);
echo '</i><br /> ';
echo '<b>';
echo stripslashes($row['notetitle']);
echo '</b><br /> ';
echo stripslashes($row['note']);
echo '<br /><br /> ';
echo '</p>';
}
$result->free();
$db->close();
?>
Now I would like to display an edit-link for each row displayed, that can open a new page in which it is possible to edit a specific row. I already have the code that lets you edit the row:
<?php
if ($_REQUEST['save']=="Save") { // is data submitted?
// create variables
$noteid = $_REQUEST['noteid'];
$coursename = $_REQUEST['coursename'];
$notetitle = $_REQUEST['notetitle'];
$note = $_REQUEST['note'];
$query = "UPDATE notes SET ";
$query .= "coursename='$coursename', ";
$query .= "notetitle='$notetitle', ";
$query .= "note='$note' ";
$query .= "WHERE noteid='$noteid'";
$result = $db->query($query);
} elseif ($_REQUEST['delete']=="Delete") { // is data to be removed?
$noteid = $_REQUEST['noteid'];
$query="DELETE FROM notes WHERE noteid='$noteid'";
$result = $db->query($query);
}
?>
<div class="formular">
<div class="row1">
<p>Id</p>
<p>Notetitle</p>
<p>Note</p>
</div>
<?php
$query = "SELECT * FROM notes ORDER BY noteid DESC";
$result = $db->query($query);
while ($row = mysqli_fetch_array($result)) {
echo "<form ".$_SERVER['PHP_SELF']." name='edit-form' method='post' class='row1'>\n";
echo "<p class='align_top padding_top'>".$row['noteid']."<input type='hidden' name='noteid' value='".$row['noteid']."' /></p>\n";
echo "<p class='align_top'><input type='text' name='notetitle' value='".$row['notetitle']."' /></p>\n";
echo "<p><textarea name='note' rows='10' cols='50'>".$row['note']."</textarea></p>\n";
echo "<p><input type='submit' name='save' value='Save' /></p>";
echo "<p><input type='submit' name='delete' value='Delete' /></p>";
echo "</form>\n";
}
echo '</div>';
$result->free();
$db->close();
?>
What I am struggling with is how to display an edit-link for each row that lets you open a page where you can edit/delete the content of only that row.
I hope someone can help, I am very new at this.
Thank you!
Add a button next to each row that opens an edit page (or modal) with the id inside, example: <button onclick="edit('randomId')">Edit RandomId </button>
You could implement something different that accepts the unique id of that specific row and open a new page or modal with it.
There is registration form in which country field is there.if user's country is not in drop down list. user can select other at that time display one textbox and user enter their country in textbox.after submit country by user .how to approve the requested country and publish in country drop down list in php.
config.php
<?php
$con=mysql_connect("localhost","root","");
if(!$con)
{
die("Could not connect".mysql_error());
}
mysql_select_db("RateMyProfessor",$con);
?>
Demo.php
<?php
include ("config.php");
$query = "select * from user_details where is_approved='0'";
$result=mysql_query($query);
$i = 1; //counter for the checkboxes so that each has a unique name
echo "<form action='process.php' method='post'>"; //form started here
echo "<table border='1'>
<tr>
<th>UserId</th>
<th>Email</th>
<th>Country </th>
<th>Update</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['UserId'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . $row['Country'] . "</td>";
echo "<td><input type='checkbox' name='check[$i]' value='".$row['UserId']."'/>";
echo "</tr>";
$i++;
}
echo "</table>";
echo "<input type='submit' name='approve' value='approve'/>";
echo "</form>";
mysql_close($con);
?>
process.php
<?php
include_once("config.php");
if(isset($_POST['approve']))
{
if(isset($_POST['check']))
{
foreach ($_POST['check'] as $value){
echo $value;
$sql = "update user_details set is_approved ='1' where UserId = '$value'";
mysql_query($sql) or die (mysql_error());
}
}
}
?>
when admin is approve country from admin side country is copy on country table.
You can insert a new record in country table at the time of approval
for e.g.
<?php
include_once("config.php");
if(isset($_POST['approve']))
{
if(isset($_POST['check']))
{
foreach ($_POST['check'] as $value){
echo $value;
$sql = "update user_details set is_approved ='1' where UserId = '$value'";
mysql_query($sql) or die (mysql_error());
$sql = "select other_country from user_details where UserId = '$value'";
$result = mysql_query($sql) or die (mysql_error());
if($Other_country_name = mysql_fetch_assoc($result))
{
$Other_country_name = $Other_country_name['other_country'];
}
$sql = "insert into country_table set name = '$Other_country_name'";
mysql_query($sql) or die (mysql_error());
}
}
}
?>
I have not implemented conditions. please do it by yourself
Question: What to do to fix my problem on handling the session because it is returning an incorrect value.
Situation: I'm having problem on this session variable from the table. I added data from database to a table using while loop. Here is my code:
<form action="edit2.php" method="get">
<?php
$link = mysql_connect("localhost", "root", "root");
mysql_select_db("ispot", $link);
$result4 = mysql_query("SELECT * FROM user_ispot", $link);
$num_rows = mysql_num_rows($result4);
$result = mysqli_query($con,"SELECT * FROM complaints");
echo "<table border='1'>
<tr>
<th>Id Number</th>
<th>Category</th>
<th>Problem</th>
<th>Date Reported</th>
<th>Complaint ID </th>
<th>Action</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td name=id_num>" . $row['id_number'] . "</td>";
$_SESSION['favcolor'] = "$row[id_number]";
echo "<td name=remarks>" . $row['remarks'] . "</td>";
echo "<td name=status>" . $row['status'] . "</td>";
echo "<td name=date>" . $row['date_reported'] . "</td>";
echo "<td>" . "<INPUT TYPE = text Name = cid VALUE = " . $row['complaint_id'] . ">" . "</td>";
echo "<td>" . "<INPUT TYPE = Submit Name = Submit1 VALUE =Edit>" . "</td>";
echo "</tr>";
}
echo "</table>" ;?>
And it looks like this:
As you can see, there is the edit button, where I can edit a specific row in the table.
When I press the edit button, this will show:
Notice that the User ID is wrong, what can I do to fix it? because the user id that is being post here was the last user_id that was inserted in the table.
And here is my code for the second image:
<b>Date:</b> <input type='text' name='today' placeholder='<?php echo $today ?>' disabled='disabled'> <br><br>
<b>User ID:</b> <input type='text' disabled='disables' name='userid' placeholder='<?php
//$comid = $_GET["cid"];
//echo $userid;
echo $_SESSION['userid'];
//$result = mysqli_query($con,"SELECT * FROM complaints WHERE id = XXX");
//$row = mysqli_fetch_assoc($result);
//print_r($row);
//$result2 = mysql_query("SELECT * FROM complaints WHERE complaint_id = '$comid'", $link);
//$result2 = mysql_query("SELECT * FROM complaints", $link);
//while($row = mysql_fetch_assoc($result2))
//{
//echo $row['id_number'];
//}
?>'></br><br>
Any help would be appreciated. Thank you.
i replaced the button with a link, used it to pass the value when edit is clicked, catch the value with a get and it works for me.
in edit.php
echo "<td> <a href = 'edit2.php?id=$num_id'>Edit</a></td>";
in edit2.php
$id = $_GET['id'];
<b>User ID:</b> <input type='text' disabled='disables' name='userid' value = '<?php echo $id;?>'></input type>
$result2 = mysql_query("SELECT * FROM complaints WHERE complaint_id = '$comid'", $link);
$result2 = mysql_query("SELECT * FROM complaints", $link);
You must use just one of them this rows. I think problem is the second row. This query not choose the "id" that is "comid".
Your first query row is enough:
$result2 = mysql_query("SELECT * FROM complaints WHERE complaint_id = '$comid'", $link);
Good evening, I'm trying to make a single php page wich can edit/delete multiple rows in mysql:
<html>
<head>
<title>Update/Delete Test Page</title>
</head>
<body>
<?
include 'connect.php';
if(isset($_POST['edit'])) // from button name="delete"
{
$title = $_POST['title'];
$description = $_POST['description'];
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
$ed = $check;
$sql = "UPDATE events SET title = '$title', description ='$description' WHERE id = $ed";
}
}
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
}
?>
<?php
include ("connect.php");
if(isset($_POST['delete'])) // from button name="delete"
{
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
$del = $check;
$sql = "DELETE from events where id = $del";
$result = $mysqli->query($sql) or die(mysqli_error($mysqli));
}
}
?>
<?php
include 'connect.php';
$query = 'SELECT id, title, description FROM events WHERE evdate = "1/9/2013" order by title asc';
$result2 = $mysqli->query($query) or die(mysqli_error($mysqli));
echo '<br><br><br>';
echo '<b><div align="center"> "1/9/2013"</div></b>';
if ($result2) {
// create a new form and then put the results
// into a table.
echo "<form method='post' action=".$_SERVER['PHP_SELF'].">";
echo "<table cellspacing='0' cellpadding='3'>
<th align='left'>Interval orar</th>
<th align='left'>Eveniment</th>
<th align='left'></th>
";
while ($row = $result2->fetch_object()) {
$title = $row->title;
$description = $row->description;
$id = $row->id;
//put each record into a new table row with a checkbox
echo "
<tr>
<td align='left'><input type='text' name='title' size='20' value='$title'></td>
<td align='left'><input type='text' name='description' size='50' value='$description'></td>
<td align='left'><input type='checkbox' name='checkbox[]' id='checkbox[]' value=$id />
</tr>
";
}
// when the loop is complete, close off the list.
echo "</table>
<p>
<input id='edit' type='submit' class='button' name='edit' value='Edit'/>
<input id='delete' type='submit' class='button' name='delete' value='Delete'/>
</p>
</form>";
}
?>
</body>
</html>
Connect.php looks like this:
<?php
$db_host = "localhost";
$db_username = "calendar";
$db_pass = "calendar";
$db_name = "ecalendar";
$con = mysql_connect ("$db_host", "$db_username", "$db_pass") or die ("could not connect to mysql database");
mysql_select_db("$db_name") or die ("no database");
$mysqli = new MySQLi($db_host, $db_username, $db_pass, $db_name) or die(mysqli_error());
?>
The delete works fine, but the edit doesn't do anything...
Table looks like this:
Start/End Time Event (Checkbox is here)
08:00-10:00 test1 X
10:00-12:00 test2 X
When I try to edit test2 to test2xx I get this in Firebug POST :
title=08%3A00-10%3A00&description=test1&title=10%3A00-12%3A00&description=test2xx&checkbox%5B%5D=53&edit=Edit
If I delete I get this (and it works)
title=08%3A00-10%3A00&description=test1&title=10%3A00-12%3A00&description=test2&checkbox%5B%5D=53&delete=Delete
Edit works but only for the last row (test2), if I try to edit the row above (test1), it insted updates it with the values of the last row (test2)
Guess you need del_id not id
$sql = "UPDATE events SET title = '$title', description ='$description' WHERE id = $del_id ";
1-look your value value=$id in your chekbob
change it to value= '$id'
2-and also , the delete query is with mysqli
but the update query is with mysql ??
3- u dont have to include connect.php 3 times
4- also what previous answer about the variable $del_id.
5- u are missing closing tag td
6- u are missing closing }
7- try this
<html>
<head>
<title>Update/Delete Test Page</title>
</head>
<body>
<?php
include 'connect.php';
if(isset($_POST['edit'])) // from button name="edit"
{
$checkbox = $_POST['checkbox']; //from name="checkbox[]"
$countCheck = count($_POST['checkbox']);
for($i=0;$i<$countCheck;$i++)
{
$del_id = $checkbox[$i];
$sql3 = "UPDATE events SET title = '$title', description ='$description' WHERE id = '$del_id' ";
$result3 = $mysqli->query($sql3) or die(mysqli_error($mysqli));
if (!$result3)
{
die(mysqli_error($mysqli)) ;
}
echo "1 record added";
}
}
if(isset($_POST['delete'])) // from button name="delete"
{
$checkbox = $_POST['checkbox']; //from name="checkbox[]"
$countCheck = count($_POST['checkbox']);
for($i=0;$i<$countCheck;$i++)
{
$del_id = $checkbox[$i];
$sql = "DELETE from events where id = '$del_id' ";
$result = $mysqli->query($sql) or die(mysqli_error($mysqli));
}
}
$query = 'SELECT id, title, description FROM events WHERE evdate = "1/9/2013" order by title asc';
$result2 = $mysqli->query($query) or die(mysqli_error($mysqli));
echo '<br><br><br>';
echo '<b><div align="center"> "1/9/2013"</div></b>';
if ($result2) {
// create a new form and then put the results
// into a table.
echo "<form method='post' action=".$_SERVER['PHP_SELF'].">";
echo "<table cellspacing='0' cellpadding='3'>
<th align='left'>Interval orar</th>
<th align='left'>Eveniment</th>
<th align='left'></th>
";
while ($row = $result2->fetch_object()) {
$title = $row->title;
$description = $row->description;
$id = $row->id;
//put each record into a new table row with a checkbox
echo "
<tr>
<td align='left'><input type='text' name='title' size='20' value='$title'></td>
<td align='left'><input type='text' name='description' size='50' value='$description'></td>
<td align='left'><input type='checkbox' name='checkbox[]' id='checkbox[]' value='$id' /></td>
</tr>
";
}
// when the loop is complete, close off the list.
echo "</table>
<p>
<input id='edit' type='submit' class='button' name='edit' value='Edit'/>
<input id='delete' type='submit' class='button' name='delete' value='Delete'/>
</p>
</form>";
}
?>
</body>
</html>
id=$del_id under for loop,double check your SQL statements and variables bound to them.
gave up on the code above and went with this example from
http://bohemiawebsites.com/PHP-MYSQL-Update-Multiple-Rows.html
Everything works if anybody needs it...
I have made a filter function but I am having trouble with the starting table, I want all the data selected from a table to be displayed if no category is selected for the filter. So far, I can show all the necessary data needed if a category is chosen but only if a category is chosen. If none is chosen, it shows a blank table. Help please,...
Here's the function
function listhistoryHost(){
$accountid=$_SESSION['userid'];
if(isset($_POST['button'])){
$filter=$_POST['historyfilter'];
if($filter==$filter){
$sql="select * from webhostrequest where status='$filter' order by webhostrequest.recentact desc";
$result=mysql_query($sql) or die("Error in selecting items ".mysql_error());
$out="<ul>";
while($row=mysql_fetch_array($result)){
$accountid=$row['userid'];
$requesttitle=$row['requesttitle'];
$requestid=$row['requestid'];
echo "<table width=\"100%\" border=\"0\" cellpadding=\"2\">";
echo "<tr class=\"rowcolor1\">";
echo "<td width=\"70%\"><span style=\"padding-left:20px\">Requested Web Hosting for ".$row["requesttitle"]. "</td>";
echo "<td width=\"20%\"><span style=\"padding-left:20px\">Status: " .$row["status"]. "</td>";
echo "<td>
<center>
<form id = \"form1\" method = \"post\" action = \"viewhistorywebhost.php?webhost=$requestid\">
<input type = \"submit\" name = \"button\" id = \"button\" value = \"Details\" />
</form>
</center>";
echo "</tr>";
}
echo "</table>";
return $out;
}
}
}
Here's the form and trigger
<form id = "form1" method = "post" action = "#">
<select id="select1" name="historyfilter">
<option value='' selected disabled>Select item type</option>
<?php
$options = array('approved' => 'Approved',
'cancelled'=>'Cancelled',
'rejected' => 'Rejected');
foreach($options as $value => $type){
echo "<option value=\"$value\">$type</option>";
}
?>
</select>
<input type = "submit" name = "button" id = "submit" value = "Go" />
</form>
<?php
$webhost=new requisition2();
echo $webhost->listhistoryHost();
?>
Just put if condition for query
function listhistoryHost(){
$accountid=$_SESSION['userid'];
if(isset($_POST['button'])){
$filter=$_POST['historyfilter'];
$sql="select * from webhostrequest order by webhostrequest.recentact desc";
if(isset($filter))
$sql="select * from webhostrequest where status='$filter' order by webhostrequest.recentact desc";
$result=mysql_query($sql) or die("Error in selecting items ".mysql_error());
$out="<ul>";
while($row=mysql_fetch_array($result)){
$accountid=$row['userid'];
$requesttitle=$row['requesttitle'];
$requestid=$row['requestid'];
echo "<table width=\"100%\" border=\"0\" cellpadding=\"2\">";
echo "<tr class=\"rowcolor1\">";
echo "<td width=\"70%\"><span style=\"padding-left:20px\">Requested Web Hosting for ".$row["requesttitle"]. "</td>";
echo "<td width=\"20%\"><span style=\"padding-left:20px\">Status: " .$row["status"]. "</td>";
echo "<td>
<center>
<form id = \"form1\" method = \"post\" action = \"viewhistorywebhost.php?webhost=$requestid\">
<input type = \"submit\" name = \"button\" id = \"button\" value = \"Details\" />
</form>
</center>";
echo "</tr>";
}
echo "</table>";
return $out;
}
}
You need to check out a $filter for empty
if (empty($filter)) {
$sql="select * from webhostrequest order by webhostrequest.recentact desc";
} else {
$sql="select * from webhostrequest where status='$filter' order by webhostrequest.recentact desc";
}