...

View Full Version : query on HTML form



badidoy
02-22-2009, 01:46 PM
Hi, im a newbie in PHP programming, im having a problem on how to display my query which i type in html forms then i want to display the result using php, here's my html code for the form,

<html>


<form action="$query" method="post">
<p>Enter mysql query here</p>
<textarea name="query" rows="5" cols="60"></textarea><br>
<input type="submit" value="submit query">
</form>

</html>


<?php

// connect to database


what's the code here.....?

here's my query example on the form, (for instance) SELECT firstname,lastname,balance from table;

any one who can help me what will be the code? thank you very much...

cyrus709
02-22-2009, 05:33 PM
I have a question, are you trying to get all the information from that table or just some of it? if you are trying to get all the information


<?php
//this connects you to your database
$connect = mysql_connect("host","user","yourpassword");
if (!$connect)
{
die('could not connect: ' . mysql_error());
}
else
{
print('Successfully connected');
}
mysql_select_db("yourdatabase", $connect);
//select from your table
$result = mysql_query("SELECT * FROM tablename");

//get the data, and display it
while($row = mysql_fetch_array($result))
{
echo $row['firstname'];
echo $row['lastname'];
echo $row['balance'];
}

?>

Note, this will display the data but it will display all the data under firstname
so if you have thirteen people who have registered for your site, and all of them are under the firstname listing, if you use this code they will all show, so its not good if your useing it for banking,
so instead of useing
$result = mysql_query("SELECT * FROM tablename");
you may want to use
$result = mysql_query("SELECT * FROM tablename
WHERE FirstName='exactnameyouwant'");

hope this helps

badidoy
02-23-2009, 12:43 AM
yes I got it CYrus, let's say i type"SELECT firstname,lastname,balance from table1 inner join table2 on table1.id=table2.id order by firstname;" from my html forms,I want this query to be pass on mysql using php so that i can display whatever query i type on the form to be displayed. sori on that...thanks again..do i have to assign a variables to my html forms to pass to mysql?

UnPossible
02-23-2009, 02:07 AM
The form will look like:




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form action="query.php" method="post">
<table>
<tr>
<td>What is your Query:</td><td><textarea name="query" rows="30" cols="50"></textarea></td>
<td colspan="2" style="text-align:center;"><input type="submit" value="Submit Query" /></td>
</tr>
</table>
</form>

</body>
</html>








<?php
$query = $_REQUEST['query'];
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$result = mysql_query($query);

echo "<table border='1'>
<tr>
<th>This is Stuff</th>
<th>This is More Stuff</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['stuff 1'] . "</td>";
echo "<td>" . $row['stuff 2'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>



If your just typing the mysql query all into the text field and passing it just store it in a variable and then have it display in a table. I think this will do what your wanting it to do. This would actually let you interact with the mysql database through the form, I think, completely. Not sure im kinda new to this game but I think this will work for you.

badidoy
02-23-2009, 02:48 AM
got an error:
Firstname Balance "; while($row = mysql_fetch_array($result)) { echo ""; echo "" . $row['firstname'] . ""; echo "" . $row['balance'] . ""; echo ""; } echo ""; mysql_close($con); ?>


<?php
$query = $_REQUEST['query'];
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("pvdorm", $con);

$result = mysql_query($query);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Balance</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['balance'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>

: is that on the tables rows?

badidoy
02-23-2009, 06:09 AM
Oh, never mind my previous post, here's the error;

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\News\query.php on line 19
Nickname Balance

code:
<?php
$query = $_REQUEST['query'];
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("pvdorm", $con);

$result = mysql_query($query);

echo "<table border='1'>
<tr>
<th>Nickname</th>
<th>Balance</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['nickname'] . "</td>";
echo "<td>" . $row['balance'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>

it seems that it's not getting the data written on the form?am i right?

badidoy
02-23-2009, 07:28 AM
it's ok now, a little typo on my part....with my deepest gratituted to UnPossible and Cyrus709....thanks alot guys for the help......

badidoy
02-23-2009, 10:06 AM
im modifying your code, what if i want to type in the textarea the queries and then display it, i mean in your code given;

<html>
<body bgcolor="white">
</html>
<?php
$query = $_REQUEST['query'];
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("pvdorm", $con);

$result = mysql_query($query);




while($row = mysql_fetch_array($result))
{
echo $result;
}
mysql_close($con);
?>

is this possible? i have an resource id#3 error.

im modifying your code, because what if the queries in the textarea is different queries, i have to change again the fetch array results display. on the previous code. any idea?thanks again...
im completing my school project and this the only features i dont have(add/delete/update is done!).

UnPossible
02-23-2009, 02:09 PM
I dont understand, I think, you are wanting to just show the query that you are executing and then show the results?

If that is what you are looking to do just echo your $query before you send it to the table.




<html>
<body bgcolor="white">
</html>
<?php
$query = $_REQUEST['query'];
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("pvdorm", $con);

$result = mysql_query($query);

echo $query;

while($row = mysql_fetch_array($result))
{
echo $result;
}
mysql_close($con);
?>



Something like that should work I think if this is what you are looking to do.

badidoy
02-24-2009, 02:36 AM
Thanks a lot buddy! One thing more I have to ask, im going to deploy my work on the website, how am i going to connect my database to the web? i mean mysql database? coz when i uploaded my files theres no data or tables to the files i uploaded,the tables are there but no data, and some sites dont accept my .php files(it says bad filename). Coz our instructor will going to check our projects on the web. pls help.thanks in advance...

gaurav
02-24-2009, 06:23 AM
Hello sir,
I am trying to fetch my data from Mysql Database but there is No data Retrieves.

<html>
<head>

<meta http-equiv="Content-Type">
<meta http-equiv="Content=text/html;charset=iso-8859-1">
</head>

<?php
$link = mysql_connect('localhost', 'root', 'dbname');
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully<br>';
if(!mysql_select_db("gv"))
{
die("select db failed");}


$q=mysql_query("select crop from mandi where 1");
if(!$q)
{
die("Please Correct the My SQL Query");}
echo "select a crop:<br>";
//echo "===".$_GET['dropdown']."===<br>";
if($_GET['dropdown'] == "")
{
echo "<form action='dropdownmandi.php' method='get'> <select name='dropdown'>";
while($r=@mysql_fetch_row($q))
{
echo "<OPTION VALUE='$r[0]'>$r[0]";
}
echo "</select> <INPUT TYPE=SUBMIT VALUE=\"Go\">
</form>";
}else
{
$crop=$_GET['dropdown'];
$q=mysql_query("select `max`,`min` from mandi where crop='$crop'");
$r=mysql_fetch_row($q);
echo "<br>";
echo "Maximum Rate of $crop = $r[0]<br>";
echo "Minimum Rate of $crop = $r[1]<br>";
}
mysql_close($link);
?>

gaurav
02-24-2009, 06:28 AM
hello Sir,
I have a Php Code to insert data from anther website.I wan to Update these data.My Insertion Code is-
<?php

$f=fopen("gaurav.txt", "r"); /****************/
$v1 = fgets($f);
$v2 = fgets($f);
$v3 = fgets($f);


$link = mysql_connect("ip", "root", "dbname"); /****************/
if(!mysql_select_db("gv", $link)){ /***************/
echo "error";
exit(0);
}

while(!feof($f)) {
if((intval($v2)) & (intval($v3))) {
echo $v1." ".$v2." ".$v3."<br>";
mysql_query("insert into gaurav values ('$v1', '$v2', '$v3')"); /****************/

$v1 = fgets($f);
$v2 = fgets($f);
$v3 = fgets($f);
}
else {
$v1 = $v2;
$v2 = $v3;
$v3 = fgets($f);
}
}

fclose($f);
?>

Please suggest me how to update my Data from website.I will wait for your reply.Thanking You

badidoy
02-24-2009, 07:55 AM
here are the three steps in updating your data, i also got from the web, ti works for me, just change the necessary values.
//html forms for update
<html>
<head>
<title>SystemsDoc Update</title>
</head>
<body bgcolor="white">
<form method="POST" action="sysdocupdate.php">
<table>
<col span="1" align="right">
<tr>
<td><font color="blue">UID to Update:</font></td>
<td><input type="text" name="UID" size=100></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>


Here is the PHP file sysdocupdate.php that populates the fields for updating:

<?php
foreach($HTTP_POST_VARS as $varname => $value)
$formVars[$varname]=$value;
require_once("config.php");
$db1=mysql_connect($dbhost, $dbuname, $dbpass);
mysql_select_db("sysops");
$query="SELECT * FROM systemsdoc WHERE UID = \"".$formVars["UID"]."\"";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$formVars = array();
$formVars["manu"]=$row["manu"];
$formVars["model"]=$row["model"];
$formVars["addr"]=$row["addr"];
$formVars["zip"]=$row["zip"];
$formVars["phone"]=$row["phone"];
$formVars["deploy_date"]=$row["deploy_date"];
$formVars["sernum"]=$row["sernum"];
$formVars["assetnum"]=$row["assetnum"];
$formVars["machname"]=$row["machname"];
$formVars["sysversion"]=$row["sysversion"];
$formVars["UID"]=$row["UID"];
mysql_close($db1);
?>

//postupdate
<html>
<head>
<title>SystemsDoc Update</title>
</head>
<body bgcolor="white">
<form method="post" action="postupdate.php">
<table>
<col span="1" align="right">
<tr>
<td><font color="blue">Manufacturer:</font></td>
<td><input type="text" name="manu"
value="<? echo $formVars["manu"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Model:</font></td>
<td><input type="text" name="model"
value="<? echo $formVars["model"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Address:</font></td>
<td><input type="text" name="addr"
value="<? echo $formVars["addr"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Zip:</font></td>
<td><input type="text" name="zip"
value="<? echo $formVars["zip"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Phone:</font></td>
<td><input type="text" name="phone"
value="<? echo $formVars["phone"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Deployment Date:</font></td>
<td><input type="text" name="deploy_date"
value="<? echo $formVars["deploy_date"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Serial Number:</font></td>
<td><input type="text" name="sernum"
value="<? echo $formVars["sernum"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Asset Number:</font></td>
<td><input type="text" name="assetnum"
value="<? echo $formVars["assetnum"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Machine Name:</font></td>
<td><input type="text" name="machname"
value="<? echo $formVars["machname"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">System Version:</font></td>
<td><input type="text" name="sysversion"
value="<? echo $formVars["sysversion"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">UID:</font></td>
<td><input type="text" name="UID"
value="<? echo $formVars["UID"]; ?>" size=100></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</body>
</html>


gudluck!

badidoy
02-24-2009, 07:58 AM
here's the insert/add codes:

<html>

<body ">
<form method="post" action="insert2.php"

style="border: 1px solid #000000;
width :230px; margin-top:
50px;margin-left: 70px;
padding:20px 20px 20px 20px;
background-color: #F5F5FF;">
<table cellpadding="5">
<tr >
<td>Name</td>
<td>&nbsp;</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Class</td>
<td>&nbsp;</td>
<td><input type="text" name="class"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>

</form>

<div style="border: 1px solid #000000;
width :230px; margin-top:
50px;margin-left: 70px;
padding:20px 20px 20px 20px ;
background-color: #F5F5FF;">

<?php

$connection = mysql_connect('localhost','root','')
or die("Could not connect: ".mysql_error());
$connection1 = mysql_connect('localhost','root','')
or die("Could not connect: ".mysql_error());

mysql_select_db('db',$connection)
or die("Error in selecting the database:".mysql_error());

if (isset($_POST['name'])) {
$name=$_POST["name"];
$class=$_POST["class"];

$sql="insert into stu(name,class)
values('".$name."',".$class.")";

mysql_query($sql,$connection)
or exit("Sql Error".mysql_error());
mysql_close($connection);

}

$sql="Select * from stu";

$sql_result=mysql_query($sql,$connection1)
or exit("Sql Error".mysql_error());

$sql_num=mysql_num_rows($sql_result);

echo "<table width=\"100%\">";
echo "<tr>";
echo "<td ><b>Id</b></td><td><b>Name</b></td>
<td><b>Class</b></td>";
echo "</tr>";
while($sql_row=mysql_fetch_array($sql_result))
{
$id=$sql_row["id"];
$name=$sql_row["name"];
$class=$sql_row["class"];
echo "<tr><td>".$id."</td>";
echo "<td>".$name."</td>";
echo "<td>".$class."</td></tr>";
}
echo "</table>";
mysql_close($connection1);
?>
</div>
</body>
</html>

UnPossible
02-24-2009, 02:42 PM
Guys,

Please use the [ php] [ /php] code tags when you are posting php on here. Makes it MUCH MUCH MUCH easier to read.

Quarav:

Are you using an MySQL database or an flat file (csv, text)? You have code to look at info in a MySQL db but your insertion code is for a flat file. If you do have access to MySQL I would strongly encourage the use of it.

badiboy:

http://w3schools.com/php/php_mysql_connect.asp

That is wealth of information about using php and mysql. Read up on it and alot of this will become alot clearer.

Another great reference is HERE! (http://google.com). Just enter what your wanting to know about and go baby go!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum