How to asking user input data in TEXT BOX and run a Query

  • Thread starter Thread starter Anonymous
  • Start date Start date
A

Anonymous

Guest
:oops:

Hi there!

Can i get an advise about how to asking user input data in text box and then query will perform according the data inputed by user. For example here is the website, regarding the matter


http://www.kopkastam.org.my/ahli
 
sarjanajava said:
:oops:

Hi there!

Can i get an advise about how to asking user input data in text box and then query will perform according the data inputed by user. For example here is the website, regarding the matter


http://www.kopkastam.org.my/ahli

<input type=text name=some value='variable'>

result page

Code:
<?
echo $_GET['some'];
?>

or if you use POST method


Code:
<?
echo $_POST['some'];
?>
 
Can you describe step by step instruction ?

here is coding to perform FORM for user input data into text box
filename: input.html
-------
<body>

<form action="data.php" method="post">
Name:
<input type=text name=some value='variable'>


<input type="submit" name="" value="find">

</form>
-----------
and this is coding for data.php
<?php

echo $_POST['some'];


//connect to database

if(!($dbLink = mysql_pconnect("localhost", "", "")))
{
print("Failed to connect to database!<BR>\n");
print("Aborting!<BR>\n");
exit();
}
//QUERY
$Query = "SELECT 'data that user input'";
$Query .= "FROM tablename;
if(!($dbResult = mysql_query($Query, $dbLink)))
{
print("Couldn't execute query!<BR>\n");
print("MySQL reports: " . mysql_error() . "<BR>\n");
print("Query was: $Query<BR>\n");
exit();
}

HOW TO GET what user input from input.html and then process with data.php especially SQL statement in $Query about variable that have been inputed by user
 
I found this the other day, if I'm right about what you're asking, this will work great.
Code:
<html>
<body>
Homesite:<a href="http://www.watkins-farm.com/php/">http://www.watkins-farm.com/php/</a><br>
<form action="sqler.php" method="post">
  <TEXTAREA cols=70 name=sql rows=5><?PHP echo(stripslashes($_POST["sql"])); ?></TEXTAREA>
  <input type="submit" name="sql_post">
</form>
<?PHP

$db_host	="localhost";
$db_user	="username";
$db_pwd		="password";
$db_db		="database";

$ok = $_POST["sql_post"];
if($ok){
	$sql = $_POST["sql"];
//$sql="SELECT * cl_ip LIMIT 10"; //DEMO restriction

	$dbh=mysql_connect ("$db_host", "$db_user", "$db_pwd") or die ('I cannot connect to the database.');
	$res=mysql_select_db("$db_db") or die("Wrong DB<br>");
	$sql = stripslashes ( $sql );
echo("$sql<hr>");
	$res = mysql_query($sql) or die(mysql_error()."<hr>");
	$affr = @mysql_affected_rows($res);
	$numr = @mysql_num_rows($res);
	$numf = @mysql_num_fields($res);
	if(!$affr)$affr=0;
	if(!$numr)$numr=0;
	if(!$numf)$numf=0;
	$table =<<< ETT
		<table border=1><tr><td colspan=$numf>Results:</td></tr>
ETT;


	if ($numf > 0){
		$table .= "<tr>";
	}
	
	for($i=0;$i<$numf;$i++){
		$fn = mysql_field_name($res,$i);
		$table .="<td><b> $fn</b></td>";
	}

	if ($numf > 0){
	$table .= "</tr>\n";
	}

	while($arr = @mysql_fetch_array($res) ){
		$table .= "<tr>";
		for($i=0;$i<$numf;$i++){
			$table .= "<td> ".$arr[$i]." </td>";
		}
		$table .= "</tr>\n";

	}
	$table .="</table>\n";
	echo("Affected rows: $affr, Number of rows: $numr, Number of fields: $numf<br>");
	echo("$table");
}
?>
</body>
</html>
 
Back
Top