Php search not working 100% - php

I have the following search script,but its not working 100%. My goal is to have 4 textbox to search for records in a table called users and only after the search button is submitted the table must be display, but at the at the moment when the page loads the table header is being displayed secondly if search for a record it display all my records that is in my table and not the just the record that I'm searching for
Any suggestion will be very helpfull
<form id="form1" name="form1" method="post" action="View.php">
<label for="from">First Name</label>
<input name="first" type="text" id="first" size="10" value="<?php echo $_REQUEST["first"]; ?>" />
<label for="to">Last Name</label>
<input name="last" type="text" id="last" size="10" value="<?php echo $_REQUEST["last"]; ?>"/>
<label>Email:</label>
<input type="text" name="email" id="string" value="<?php echo stripcslashes($_REQUEST["email"]); ?>" />
<label>Company</label>
<select name="company">
<option value="">--</option>
<?php
include("config.php");
$sql = "SELECT * FROM users GROUP BY company ORDER BY company";
$sql_result = mysql_query ($sql, $dbConn ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["company"]."'".($row["company"]==$_REQUEST["company"] ? " selected" : "").">".$row["company"]."</option>";
}
?>
</select>
<input type="submit" name="button" id="button" value="Filter" />
</label>
<a href="View.php">
reset</a>
</form>
<br /><br />
<table width="700" border="1" cellspacing="0" cellpadding="4">
<tr>
<td width="90" bgcolor="#CCCCCC"><strong>First Name</strong></td>
<td width="95" bgcolor="#CCCCCC"><strong>Last Name</strong></td>
<td width="159" bgcolor="#CCCCCC"><strong>Company</strong></td>
<td width="191" bgcolor="#CCCCCC"><strong>Email</strong></td>
<td width="113" bgcolor="#CCCCCC"><strong>Contact Number</strong></td>
<td width="113" bgcolor="#CCCCCC"><strong>Position</strong></td>
<td width="113" bgcolor="#CCCCCC"><strong>How do you know the person</strong></td>
<td width="113" bgcolor="#CCCCCC"><strong>Comment</strong></td>
</tr>
<?php
if($_POST["button"])
{
if ($_REQUEST["first"]<>'') {
$search_first = " AND fname LIKE '%".mysql_real_escape_string($_REQUEST["fname"])."'";
}
if ($_REQUEST["last"]<>'') {
$search_last = " AND lname='".mysql_real_escape_string($_REQUEST["last"])."'";
}
if ($_REQUEST["email"]<>'') {
$search_email = " AND email='".mysql_real_escape_string($_REQUEST["email"])."'";
}
if ($_REQUEST["company"]<>'') {
$search_company = " AND company='".mysql_real_escape_string($_REQUEST["company"])."'";
}
else {
$sql = "SELECT * FROM users WHERE id>0".$search_first.$search_last.$search_email.$search_company;
}
$sql_result = mysql_query ($sql, $dbConn) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0)
{
while ($row = mysql_fetch_assoc($sql_result))
{
?>
<tr>
<td><?php echo $row["fname"]; ?></td>
<td><?php echo $row["lname"]; ?></td>
<td><?php echo $row["company"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td><?php echo $row["contactnumber"]; ?></td>
<td><?php echo $row["position"]; ?></td>
<td><?php echo $row["howdoyouknow"]; ?></td>
<td><?php echo $row["comment"]; ?></td>
</tr>
<?php
}
} else {
?>
<tr><td colspan="5">No results found.</td>
<?php
}
}
?>
</table>

Please remove else condition,
else {
$sql = "SELECT * FROM users WHERE id>0".$search_first.$search_last.$search_email.$search_company;
}
use like below i.e without else tag
$sql = "SELECT * FROM users WHERE id>0".$search_first.$search_last.$search_email.$search_company;

How would you expect that this:
else {
$sql = "SELECT * FROM users WHERE id>0".$search_first.$search_last.$search_email.$search_company;
}
would even work when you declare the $search_company in the IF
Also I do think the else wouldn't even be needed in your case since the
$sql = "SELECT * FROM users WHERE id>0".$search_first.$search_last.$search_email.$search_company;
wouldn't work without executing the IF statements

Related

Undefined Variable Warning in PHP

So I've been doing this school project and need to make it so I can edit whatever is in the table. Whenever i click on "Edit" it redirects me correctly but in the form it says there is an undefined variable eventhough that variable is used pretty much everywhere.
Here is some code of the table:
<table style='margin-left:auto ; margin-right:auto;'>
<tr>
<th>#</th>
<th>Name</th>
<th>Zeit</th>
<th>Datum</th>
<th>Titel</th>
<th>Inhalt</th>
<th>Ort</th>
</tr>
<?php
if($stmt=$db->prepare("SELECT * FROM terminkalender")) {
$stmt->execute();
$stmt->store_result();
$zeilen = $stmt->num_rows();
$stmt->close();
}else {
$zeilen = 0;
}
if($zeilen > 0) {
//nur wenn Einträge, dann ausgeben
if($stmt = $db->prepare("SELECT * FROM terminkalender ORDER BY zeit,datum DESC")) {
$stmt->execute();
$stmt->bind_result($id,$name,$zeit,$datum,$ort,$titel,$inhalt);
$stmt->store_result();
//Ausgabe starten
while($stmt->fetch()){
echo "<tr>";
?>
<td><?php echo $id ;?></td>
<td><?php echo htmlspecialchars($name) ;?></td>
<td><?php echo htmlspecialchars($datum) ;?></td>
<td><?php echo htmlspecialchars($zeit) ;?></td>
<td><?php echo htmlspecialchars($ort) ;?></td>
<td><?php echo htmlspecialchars($titel) ;?></td>
<td><?php echo htmlspecialchars($inhalt); ?></td>
<td><a href='edit.php?id=<?php echo $id;?>'>Edit</a></td>
<td><a href='delete.php?id=<?php echo $id;?>'>Delete</a></td>
<?php
echo "</tr>" ;
}
}
}
?>
</table>
and here for the edit.php file:
<?php
include("./config/connect.inc.php");
$id = $_GET['id']; // get id through get string
$result=mysqli_query($db,"SELECT * FROM terminkalender WHERE id=$id");
if(isset($_POST['update'])) {
$name=$_POST['name'];
$datum=$_POST['datum'];
$zeit=$_POST['zeit'];
$ort=$_POST['ort'];
$titel=$_POST['titel'];
$inhalt=$_POST['inhalt'];
$result = "UPDATE terminkalender
SET name='$name',
datum='$datum',
zeit='$zeit',
ort='$ort',
titel='$titel',
inhalt='$inhalt'
WHERE id=$id";
header("location: ausgabe.php");
}
?>
<form name="form" method="POST" action="edit.php">
<input type="text" name="name" value="<?php echo $name; ?>" Required>
<input type="date" name="datum" value="<?php echo $datum; ?>" Required>
<input type="time" name="zeit" value="<?php echo $zeit; ?>" Required>
<input type="text" name="ort" value="<?php echo $ort; ?>" Required>
<input type="text" name="titel" value="<?php echo $titel; ?>" Required>
<input type="text" name="inhalt" value="<?php echo $inhalt; ?>" Required>
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
<input type="submit" name="update" value="Update">
</form>
Ẁould be really awesome if anyone could help. Thanks is advance!
Maybe you can try using isset function of php to check if that variable contains a value or not for more details check:-https://www.stechies.com/notice-undefined-variable-in-php/

Echo Array into new lines

I have an edit page where a user can edit an invoice form. I need to echo the array into a table but separate each array into a new line on the table.
When i echo the value, its bunched up into one line. When i echo the value into an input field, i only get one record from the array.
Here's what it looks like in my table:
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id=$id");
while($res = mysqli_fetch_array($result))
{
$partnumber = $res['partnumber'];
$partdescription = $res['partdescription'];
$partprice = $res['partprice'];
$partquantity = $res['partquantity'];
}
>
And then the table:
<tr>
<td><input type="text" class="input-small" name="partnumber[]" value=<?php echo $partnumber;?>></td>
<td><input type="text" class="input-small" name="partdescription[]" value=<?php echo $partdescription;?>></td>
<td><input type="text" class="input-small" name="partprice[]" size="4" value=<?php echo $partprice;?>></td>
<td><input type="text" class="input-small" name="partquantity[]" size="4" value=<?php echo $partquantity;?>></td>
</tr>
I get this:
<tr>
<td><?php echo $partnumber;?></td>
<td><?php echo $partdescription;?></td>
<td><?php echo $partprice;?></td>
<td><?php echo $partquantity;?></td>
</tr>
I get this:
I tried the following but they both result in a blank echo:
<tr>
<td><? echo $res['partnumber']; ?></td>
</tr><tr>
<td><? echo $res['partdescription']; ?></td>
</tr><tr>
<td><? echo $res['partprice']; ?></td>
</tr><tr>
<td><? echo $res['partquantity']; ?></td>
</tr>
And
<? foreach ($res as $row) { ?>
<tr>
<td><? echo $row['partnumber']; ?></td>
<td><? echo $row['partdescription']; ?></td>
<td><? echo $row['partprice']; ?></td>
<td><? echo $row['partquantity']; ?></td>
</tr>
<?
}
?>
This is the GOAL:
Please help
EDIT***************
I will fix the security issue once i have the table working.
I tried this and the output is still on ONE line.
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id='".mysql_real_escape_string($id)."' ");
while($res = mysqli_fetch_array($result))
{
?>
<tr>
<td><input type="text" class="input-small" name="partnumber[]" value=<?php echo $res['partnumber'];?>></td>
<td><input type="text" class="input-small" name="partdescription[]" value=<?php echo $res['partdescription'];?>></td>
<td><input type="text" class="input-small" name="partprice[]" size="4" value=<?php echo $res['partprice'];?>></td>
<td><input type="text" class="input-small" name="partquantity[]" size="4" value=<?php echo $res['partquantity'];?>></td>
<?php
}
?>
Does this help?
Please note, its only displaying 1 record because i have multiple values in one record and them i'm separating them with a comma. Please check screenshots above. Here's the code:
$partnumber = $_POST['partnumber'];
$partnumberarray = implode( ", ", $partnumber);
$partdescription = $_POST['partdescription'];
$partdescriptionarray = implode( ", ", $partdescription);
$partprice = $_POST['partprice'];
$partpricearray = implode( ", ", $partprice);
$partquantity = $_POST['partquantity'];
$partquantityarray = implode( ", ", $partquantity);
$result = mysqli_query($mysqli, "INSERT INTO invoicespartnumber, partdescription, partprice, partquantity, login_id) VALUES('$partnumberarray', '$partdescriptionarray', '$partpricearray', '$partquantityarray', '$loginId')");
Is there anyway i can echo the values from the 1 record which is separated by a comma into multiple lines on the table?
You have to put your html inside while loop that you do over result array.
It will look like this.
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE ID='".mysql_real_escape_string($id)."'");
while($res = mysqli_fetch_array($result)){ //close php here to easier write html. ?>
<tr>
<td><input type="text" class="input-small" name="partnumber[]" value="<?php echo $res['partnumber'];?>"></td>
<td><input type="text" class="input-small" name="partdescription[]" value="<?php echo $res['partdescription'];?>"></td>
<td><input type="text" class="input-small" name="partprice[]" size="4" value="<?php echo $res['partprice'];?>"></td>
<td><input type="text" class="input-small" name="partquantity[]" size="4" value="<?php echo $res['partquantity'];?>"></td>
</tr>
<?php //open php tag again to add end scope to while loop
}
?>
<table width = "100%">
<thead>
<tr>
// here will be all columns names in th
</tr>
</thead>
<tbody>
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id=$id");
while($res = mysqli_fetch_array($result))
{ ?>
<tr>
<td><?php echo $res['partnumber']; ?></td> <!-- Also use input box here -->
<td><?php echo $res['partdescription'];?><td>
<td><?php echo $res['partprice']; ?></td>
<td><?php echo $res['partquantity'];?></td>
</tr>
<?php
}
?>

passing variables from dynamic table

Good morning,
I was searching, but couldn't find answer :(
I have a table created dynamically with data taken from MSSQL:
$tabelka = '
<form action="index.php" method="post">
<table border =1 width=40% class="hoverTable"><tr>
<td width=10%><B>Biuro</B></td>
<td width=10%><b>Drukarka</b></td>
<td width=20%><b>Toner</b></td>
<td width=10%><b>Na stanie</b></td>
<td width=10%><b>Zamówionych</b></td>
<td width=10%><b>Akcja</b></td></tr>';
$sql = 'select * from raportTonerow';
if ($result = sqlsrv_query($conn, $sql)) {
while ($row = sqlsrv_fetch_array($result)) {
$tabelka .= '
<tr><td width=10%>'.$row['kodBiura'].'</td>
<td width=10%>'.$row['nazwaDrukarki'].'</td>
<td width=20%>'.$row['toner'].'</td>
<td width=10%>
<input type="number" name="naStanie" size=10% maxlength=1 value="'.$row['naStanie'].'"></td>
<td width=10%><input type="number" name="zamowionych" size=10% maxlength=1 value="'.$row['zamowionych'].'"></td>
<td width=10%><input type="submit" value="Aktualizuj" name="akt"></td>';
}
}
$tabelka .= '</form></table>';
and
if (isset($_POST['akt'])) {
echo "test ".$_POST['zamowionych'];
}
it is showing me only last data from table. How can I pass variables from each row?
What I want to have is: when user click on button on the row only data from that row will be send in form.
thank you
Lukasz
<?php
while($row=mysql_fetch_array($sql))
{
?>
<td> <?php echo $row->kodBiura;?></td>
<td> <?php echo $row->nazwaDrukarki; ?></td>
<td> <?php echo $row->toner; ?></td>
<?php }?>

SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '{search_term}''

I can't do the live search table thing. Can someone help me please?
Here is my code. I want to show only the data I've search.........................................................................................................................................................................................................................................................................
<?php
//include the connection file
include "conn.php";
$sql = "SELECT * FROM tblreservation";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= "WHERE Name = '{search_term}'";
}
$query = mysql_query($sql) or die(mysql_error());
?>
<form name="search_form" method="POST" action="trys.php" align="center">
Search: <input type="text" name="search_box" value="" />
<input type="submit" name="search" value="Search the table...">
</form>
<table width="70%" cellpadding="5" cellspace="5">
<tr>
<td>ID</td>
<td>Name</td>
<td>Email</td>
<td>Packages</td>
<td><select name="Packages" class="fieldsize">
<option value="">select package</option>
<option value="budget" <?php if($valid_Packages=='budget') echo "selected='selected'";?>>Budget</option>
<option value="standard" <?php if($valid_Packages=='standard') echo "selected='selected'";?>>Standard</option>
<option value="super" <?php if($valid_Packages=='super') echo "selected='selected'";?>>Super</option>
<option value="mega" <?php if($valid_Packages=='mega') echo "selected='selected'";?>>Mega</option>
</select>
<span class="err"><?php echo $error["Packages"];?></span></td>
</tr>
<td>Contactno</td>
<td>Gender</td>
<td><input type="radio" name="gender" value="male" <?php if($valid_gender=='male') echo "checked='checked'";?> />
Male
<input type="radio" name="gender" value="female" <?php if($valid_gender=='female') echo "checked='checked'";?>/>
Female <span class="err"><?php echo $error["gender"];?></span></td>
<td>file</td>
<td><input type="file" name="file" value="upload" />
<span class="err"><?php echo $error["file"];?></span></td>
<td>Address</td>
</tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<td><?php echo $row['id']; ?> </td>
<td><?php echo $row['Name']; ?> </td>
<td><?php echo $row['Email']; ?> </td>
<td><?php echo $row['Packages']; ?> </td>
<td><?php echo $row['Contactno']; ?> </td>
<td><?php echo $row['Gender']; ?> </td>
<td><?php echo $row['file']; ?> </td>
<td><?php echo $row['Address']; ?> </td>
</tr>
<?php } ?>
</table>
You are missing a $ and a space in this line:
$sql .= "WHERE Name = '{search_term}'";
The correct line should be as follows:
$sql .= " WHERE Name = '{$search_term}' ";
The SQL statement you are currently generating is exactly this:
SELECT * FROM tblreservationWHERE Name = '{search_term}'
Additionally, I would recommend checking for the existence of $_POST['search_box'] rather than $_POST['search'] in your if-statement and that it actually has a value before appending it as this is what you actually want to use in your query:
if (isset($_POST['search_box']) && $_POST['search_box']) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= " WHERE Name = '{$search_term}' ";
}

Getting the value of checkbox

Good day. I'm a newbie in php.
What I'm trying to do is i want to get the id of the specific data row from my database. And update some column on my table.
What I did is this..
<td style="width:50px;"><input type="checkbox" id="chkPending" name="chkPending[]" value="<?php echo $row['RequestNumber']; ?>"/></td>
<td><span class="label label-success">View Details</span></td>
<td style="width:100px;"><?php echo $row['RequestNumber']; ?></td>
<td style="width:100px;"><?php echo $row['Requestor']; ?></td>
<td style="width:100px;"><?php echo $row['Department']; ?></td>
<td style="width:50px;"><?php echo $row['category']; ?></td>
<td style="width:100px;"><?php echo $row['AssignedTo']; ?></td>
<td style="width:100px;"><?php echo $row['status']; ?></td>
<td style="width:100px;"><?php echo $row['DateRequested']; ?></td>
<td style="width:100px;"><?php echo $row['TimeRequested']; ?></td>
PHP CODE
<?php
$RequestNumber = $POST_['chkPending'];
$sql = "UPDATE tblrequest SET Assignedto = 'personnel', status = 'Assigned' WHERE RequestNumber = '$RequestNumber'";
if (isset($_POST['submit'])) {
$success = mysql_query($sql) or die(mysql_error());
mysql_close();
}
if ($success == TRUE) {
?>
<script>
alert('You have successfully update account.');
</script>
<?php
}
?>
I would appreciate any help. Thank you in advance.
This is my whole code:**
Change your input name by this:
<input type="checkbox" id="chkPending" name="chkPending[<?= $row['RequestNumber']; ?>]" />
In your PHP code do this:
<?php
if (isset($_POST['submit'])) {
foreach($_POST['chkPending'] as $RequestNumber=>$value)
{
$sql = "UPDATE tblrequest SET Assignedto = 'personnel', status = 'Assigned' WHERE RequestNumber = '".$RequestNumber."'";
$success = mysql_query($sql) or die(mysql_error());
}
mysql_close();
}
if ($success == TRUE) {
?>
<?php
<script>
alert('You have successfully update account.');
</script>
<?php
}
?>
You can use this code to compare it. This code is working and will print 2,4.
<form action="#" method="POST">
<input name="test[1]" type="checkbox" />
<input name="test[2]" checked type="checkbox" />
<input name="test[3]" type="checkbox" />
<input name="test[4]" checked type="checkbox" />
<input type="submit" />
</form>
<?php
if($_POST)
{
foreach($_POST['test'] as $id => $value)
{
echo $id.'<br />';
}
}
?>

Categories