php and oracle detele row [duplicate] - php

This question already exists:
PHP & Oracle database search data
Closed 9 years ago.
I am using this code for searching data from my table CUSTOMER but I am getting an error:
in my keyword text felid show this error----->>>>
Notice: Undefined index: txtKeyword in C:\xampp\htdocs\new_test\php_oracle_search.php on line 10
Code:
<html>
<head>
</head>
<body>
<?php
$txtKeyword = '';
if(isset($_GET['txtKeyword']) && strlen(trim($_GET['txtKeyword'])) > 0) {
$txtKeyword = trim($_GET['txtKeyword']);
}
?>
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
<table width="599" border="1">
<tr>
<th>Keyword
<input name="txtKeyword" type="text" id="txtKeyword" value=" =$_GET["txtKeyword"];?>">
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<?
if($_GET["txtKeyword"] != "")
{
$objConnect = oci_connect("system","dm","XE");
$strSQL = "SELECT * FROM CUSTOMER WHERE (NAME LIKE '%".$_GET["txtKeyword"]."%'
or EMAIL LIKE '%".$_GET["txtKeyword"]."%' ) ";
$objParse = oci_parse ($objConnect, $strSQL);
oci_execute ($objParse);
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
</tr>
<?
while($objResult = oci_fetch_array($objParse,OCI_BOTH))
{
?>
<tr>
<td><div align="center"><?=$objResult["CUSTOMERID"];?></div></td>
<td><?=$objResult["NAME"];?></td>
<td><?=$objResult["EMAIL"];?></td>
<td><div align="center"><?=$objResult["COUNTRYCODE"];?></div></td>
<td align="right"><?=$objResult["BUDGET"];?></td>
<td align="right"><?=$objResult["USED"];?></td>
</tr>
<?
}
?>
</table>
<?
oci_close($objConnect);
}
?>
</body>
</html>

Use below code to validate txtKeyword before using it:
if(isset($_GET['txtKeyword']) && strlen(trim($_GET['txtKeyword'])) > 0) {
//handle
}
For the notice error inside textbox, do below:
Put following code after opening <body> tag.
$txtKeyword = '';
if(isset($_GET['txtKeyword']) && strlen(trim($_GET['txtKeyword'])) > 0) {
$txtKeyword = trim($_GET['txtKeyword']);
}
Then, wherever you want $_GET['txtKeyword'], use $txtKeyword instead.
Cannot explain much simpler than this.

Related

Data not being pulled when typing keyword in the search box

I have search feature that I setup. When I type the keyword in I get no records back and no error message. Just the table header. I see the department other in the database. When I type it in the keyword box I get nothing back.
<html>
<head>
<title></title>
</head>
<body>
<form name="frmSearch" method="get" action="">
<table width="599" border="1">
<tr>
<th>Keyword
<input name="txtKeyword" type="text" id="txtKeyword" value="<?php echo $_GET["txtKeyword"];?>">
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<?php
if($_GET["txtKeyword"] != "")
{
$serverName = "localhost";
$objConnect = new PDO( "sqlsrv:server=$serverName ; Database=maintenance", "TestUser", "test") or die("Error Connect to Database");
// Search By lanId or department
$objQuery = $objConnect->prepare("SELECT * FROM requests WHERE (lanId LIKE '%".$_GET["txtKeyword"]."%' or department LIKE '%".$_GET["txtKeyword"]."%' ) ");
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">lanId </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">department </div></th>
</tr>
<?php
while( $objResult = $objQuery->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<td><div align="center"><?php echo $objResult["lanId"];?></div></td>
<td><?php echo $objResult["name"];?></td>
<td><?php echo $objResult["department"];?></td>
<?php
}
?>
</table>
<?php
}
?>
</body>
</html>
When you use prepare() statement you should also use execute() :
http://coursesweb.net/php-mysql/pdo-prepare-execute

sort data from mysql table using php

Can you help me how to sort my data from MySQL table ?
I want to sort is by using the table head :
<?php
if(isset($_POST['search'])) {
$valueTosearch = $_POST['searchvalue'];
$query = "SELECT * FROM `admin` WHERE CONCAT(`id`, `name`, `gender`, `user_group`, `date_registered`) LIKE '%".$valueTosearch."%'";
$search_result = filterTable($query);
} else {
$query = "SELECT * FROM `admin`";
$search_result = filterTable($query);
}
function filterTable($query)
{
include('config/dbconnect.php');
$filter_result = mysqli_query($con, $query);
return $filter_result;
}
?>
<form method='post' action=''>
<div><input type = 'text' name = 'searchvalue' placeholder="search by name">
<span>
<div style='margin-bottom:3px; margin-top:3px'>
<input id='gradient' class='search-btn' type = 'hidden' name = 'search' value = 'search'>
</div>
</span>
<div style="height: auto">
<table id='responsive_table'>
<thead>
<tr>
<th scope="col">name</th>
<th scope="col">sex</th>
<th scope="col">user group</th>
<th scope="col">date register</th>
</tr>
</thead>
<?php while($row = mysqli_fetch_array($search_result)): ?>
<tbody>
<tr>
<td scope="row" data-label='name'><?php echo $row['name']; ?></td>
<td data-label='sex'><?php echo $row['gender']; ?></td>
<td data-label='user group'><?php echo $row['user_group']; ?></td>
<td data-label='date register'><?php echo $row['date_registered']; ?></td>
</tr>
</tbody>
<?php endwhile; ?>
</table>
</div>
</form>
Why don't you use order by clause:
SELECT * FROM table ORDER BY column;
Order By Reference
You can modified your query as below for sorting:
sql > select * from <table name> order by <column name>;
default sorting is ascending order else for descending you can do like
sql > select * from <table name> order by <column name> desc;
If you could use JQuery, it's very simple, you have just to add the following javascript code:
$( document ).ready(function() {
$("#responsive_table").DataTable({
ordering: true,
searching: true
});
});
for a complete example see the following code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css">
<script>
$( document ).ready(function() {
$("#responsive_table").DataTable({
ordering: true,
searching: true
});
});
</script>
</head>
<body>
<form method='post' action=''>
<div style="height: auto">
<table id='responsive_table'>
<thead>
<tr>
<th scope="col">name</th>
<th scope="col">sex</th>
<th scope="col">user group</th>
<th scope="col">date register</th>
</tr>
</thead>
<tbody>
<tr>
<td scope="row" data-label='name'>HERE</td>
<td data-label='sex'>Your</td>
<td data-label='user group'>data</td>
<td data-label='date register'>loaded</td>
</tr>
<tr>
<td scope="row" data-label='name'>via</td>
<td data-label='sex'>PHP</td>
<td data-label='user group'>loop</td>
<td data-label='date register'>bye</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
</html>

Submit data from PHP to HTML

I have created a search page on PHP that searches for user from database. After the results are retrieved, i have created a view button that would display profile information of that user in HTML page. I am not quite sure how to send information from PHP to HTML. Below is the code for your reference. I would appreciate if someone could shed some light on this.
retrieve.php
<?php
echo "<body style='background-color:#DCDCDC'>";
// echo '<img src="back to school.jpg'. $row['filename'] .'" style="width:600px; height:300px;" alt="" /><br />';
include ("account.php");
( $dbh = mysql_connect( $hostname, $username, $password ))
or die ( "unable to connect to MYSQL database" );
mysql_select_db( $project );
if (isset($_POST['search'])) {
$sql= "SELECT * FROM BPi_registration ";
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= "WHERE first_name= '{$search_term}'";
$sql .= " OR last_name= '{$search_term}'";
$query=mysql_query($sql) or die(mysql_error());
}
?>
<html>
<head>
<title>Jon</title>
</head>
<body>
<form name="search_form" method="POST" action="retrieve.php">
<table width="599" border="2">
<tr>
<th>Search Here
<input type ="text" name ="search_box" value=""/>
<input type="submit" name="search" value="Find Users">
</tr>
</table>
</form>
<table width="600" border="2">
<tr>
<th width="91"> <div align="center">First Name </div></th>
<th width="98"> <div align="center">Last Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">City </div></th>
<th width="97"> <div align="center">State </div></th>
<th width="59"> <div align="center">Country </div></th>
<th width="59"> <div align="center">View </div></th>
<tr>
<?php if (isset($_POST['search'])) {
while ($row=mysql_fetch_array($query)){ ?>
<tr>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['address_city'];?></td>
<td><?php echo $row['address_state'];?></td>
<td><?php echo $row['address_country'];?></td>
<td><input type="button" value="view"/> <td>
<tr>
<?php }} ?>
</table>
handler.php
<?php
$userid = $_GET['id'];
?>
Maybe you can use PHP Sessions.
* edits *
In this part you nested a button inside a tag.
<td><input type="button" value="view"/> <td>
Try just adding text there, like:
<td>View<td>
This will appear as clickable text "View" in blue font color and will redirect to hander.php .
Hope that helps.

Error reading from database in a web form

I'm trying to pull data from the database which matches data from the text box and the code I am using throws an error.
I think there is something wrong with my SQL query, but I'm not sure what.
The error is:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\search.php on line 65
My syntax:
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"></link>
</head>
<body>
<form action="" name="formdownload" method="post">
<table>
<tr><td colspan=2><h1>Domestic Flights</h1></td></tr></br>
<td height=50> From:</td><td><input type="From" name="from" size=30/>
<tr><td height=50>To: </td><td><input type="To" name="to" size=30/>
<tr><td><input name="submit" type="submit" value ="Search"></tr></td>
<table border="1" align="center" id="table1" cellpadding="0" cellspacing="0">
<tr>
<th>Flight No</th>
<th>Flight Company</th>
<th>Plane Type</th>
<th>From</th>
<th>To</th>
</tr>
<center>
<?php
$submit = #$_POST['submit'];
$from = #$_POST['from'];
$to = #$_POST['to'];
if($submit)
{
$select=mysql_query("select * from flight where ffrom='$from'and To='$to'");
while($row1=mysql_fetch_array($select))
{
$FlightNo = $row1['FlightNo'];
$FlightCompany=$row1['FlightCompany'];
$PlaneType = $row1['PlaneType'];
$From =$row1['ffrom'];
$To =$row1['To'];
?>
<tr>
<td width="90" align="center">
<?php echo $FlightNo;?>
</td>
<td width="90" align="center">
<?php echo $FlightCompany;?>
</td>
<td width="90" align="center">
<?php echo $PlaneType;?>
</td>
<td width="90" align="center">
<?php echo $From;?>
</td>
<td width="90" align="center">
<?php echo $To;?>
</td>
</tr>
<?php }}?>
</table>
</table>
</form>
</div>
</div>
</div>
</div>
</center>
</body>
</html>
You have an error at the line on the variable name :
$select=mysql_query("select * from logintbl where name='$uname'");
Replace it with :
$select=mysql_query("select * from logintbl where name='$name'");
EDIT : since your field is named 'uname' in your database, here is the correct code (a little bit more secure with the addition of mysql_real_escape) :
$select=mysql_query('select * from logintbl where uname="'.mysql_real_escape($name).'";');
REEDIT : you want a second parameter but didn't retrieve it in PHP before your query. Your code should be like this then :
<html>
<body>
<form enctype="multipart/form-data" action="" name="formdownload" method="post">
<table border="1" align="center" id="table1" cellpadding="0" cellspacing="0">
<tr>
<th>ID</th>
<th>Name</th>
<th>Password</th>
<th>User Type</th>
</tr>
<center>
<br/><br/><br/>
ID: <input type="text" name="idno" /> <br/><br/>
Name : <input type="text" name="name" /><br/><br/>
Marks : <input type="text" name="marks" /><br/><br/>
<input type="submit" name = "submit" value="submit">
</center>
<?php
$submit = #$_POST['submit'];
$name = #$_POST['name'];
$idno = #$_POST['idno'];
if($submit)
{
$select=mysql_query("select * from logintbl where uname='$name' and Id='$idno'");
while($row1=mysql_fetch_array($select))
{
$id = $row1['ID'];
$name=$row1['uname'];
$pass = $row1['pass'];
$type =$row1['type'];
?>
<tr>
<td width="300" align="center">
<?php echo $id;?>
</td>
<td width="300" align="center">
<?php echo $name;?>
</td>
<td width="300" align="center">
<?php echo $pass;?>
</td>
<td width="300" align="center">
<?php echo $type;?>
</td>
</tr>
<?php }}?>
</table>
</form>
</body>
I still strongly advise you to filter your variables before making a query with it with mysql_real_escape.

search data from database with multiple textbox using php

search with multiple textbox using php
I am try this code for search with multiple textbox using php and i am using orcle database but this code is not work. and any one let me know what is the problem why this code not search with multiple text box field and also i have getting error Warning: oci_execute(): ORA-00920: invalid relational operator in
plz check .....my code..
<form action="Optr_Search.php" method="get">
<table width="500" border="0" align="center">
<tr>
<td width="203"><div align="right"><strong>Operator ID:</strong>
</div></td>
<td width="148"><input type="text" name="OPRID" /></td>
</tr>
<tr>
<td><div align="right"><strong>Operator Name:</strong></div></td>
<td><input type="text" name="OPRDEFNDESC" /></td>
</tr>
<tr>
<td><div align="right"><strong>Person ID:</strong></div></td>
<td><input type="text" name="EMPLID" /></td>
</tr>
<tr>
<td><div align="right"><strong>Email ID:</strong></div></td>
<td><input type="text" name="EMAILID" /></td>
</tr>
<tr>
<td colspan="2">
<div align ="center">
</br>
<input type="submit" align ="middle"value="Search" name ="submit" />
</div>
</td>
</tr>
</table>
</form>
<?php
$ora_conn = oci_connect('system','oracle','//localhost/XE');
if(!$ora_conn)
{
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else
{
print "You are connected to the database!<br/>";
}
if(isset($_POST['submit']))
{
$optid = $_POST['OPRID'];
$optdec = $_POST['OPRDEFNDESC'];
$empid = $_POST['EMPLID'];
$empmail = $_POST['EMAILID'];
$query = "SELECT * FROM OPERATOR WHERE 1";
if(isset($optid) && $optid != '') {
$query .= " And OPRID Like'%$optid%'";
}
if(isset($optdec) && $optdec != '') {
$query .= " OR OPRDEFNDESC Like'%$optdec%'";
}
if(isset($empid) && $empid != '') {
$query .= " OR EMPLID Like '%$empid%'";
}
if(isset($empmail) && $empmail != '') {
$query .= " OR EMAILID Like '%$empmail%'";
}
$objParse = oci_parse($ora_conn,$query);
$objResult = oci_execute ($objParse, OCI_DEFAULT);
?>
<table width="500" border="1" align="center">
<tr>
<th width="98"> <div align="center">Operator ID</div></th>
<th width="98"> <div align="center">Operator Name</div></th>
<th width="98"> <div align="center">Person ID</div></th>
<th width="98"> <div align="center">Email ID</div></th>
<th width="98"> <div align="center">Edit</div></th>
</tr>
<?php
if(oci_execute($objParse,OCI_DEFAULT)){
while($objResult = oci_fetch_array($objParse, OCI_RETURN_NULLS+OCI_ASSOC))
{
?>
<tr>
<td><div align="center"><?=$objResult["OPRID"];?></div></td>
<td><?=$objResult["OPRDEFNDESC"];?></td>
<td><?=$objResult["EMPLID"];?></td>
<td><div align="center"><?=$objResult["EMAILID"];?></div></td>
<td align="center">Edit
</td>
</tr>
<?
}
?>
</table>
<?
oci_free_statement($objParse);
oci_close($ora_conn);
}
?>
You are mixing ands and ors in SQL and this is not a good idea. If you set them all to or, you will have an issue because where 1 will always be true. I would make your or conditional on the where clause not being empty.

Categories