Page 1 of 1

php mysql query from input textbox

Posted: Thu Oct 22, 2009 3:21 am
by yudha2008
Hi,

I have a text box, in that i have given the mysql query. I can able to get the query in variable. But after submit button i want to get data to be printed on a same page. But how can i print the rows of the table according to the query.

can any one assist me with some examples or some sample Programmes.

Thanks with Regards,

Yudha2008

Re: php mysql query from input textbox

Posted: Thu Oct 22, 2009 7:46 am
by markusn00b
Run a mysql_query() based on the input data.

Check out these tutorials on mysql & php.

Post back with your attempt.

Mark.

Re: php mysql query from input textbox

Posted: Fri Oct 23, 2009 1:07 am
by yudha2008
Hi,

I have tried myself and find solution for the question what i asked in previews post.

Code: Select all

 
<table width="703" border="1" with="500" cellpadding="5" cellspacing="5">
<tr bgcolor="#466275">
<td width="200" style="color:#FFFFFF"><b>Mysql Query </b></td>
<td width="450"></td>
</tr>
<tr><td><h3>Query</h3></td><td><textarea name="qry" cols="100" rows="7" class="font"></textarea></td></tr>
<tr><td></td><th scope="col"><input type="submit" name="submit" value="Report" class=butt></th></tr>
</table>
<br/>
<br/>
<?php
include("conn.php");
$qry = $_POST['qry'];
$submit = $_POST{'submit'};
if($submit == 'Report')
{
$result=mysql_query("$qry") or die ("could not run Query1");
$num_rows = mysql_num_rows($result);  
$fields_num = mysql_num_fields($result);
//echo "There are $num_rows records.<P>";  
echo "<table width=400 border=1>\n";
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
while ($get_info = mysql_fetch_row($result))
{ 
echo "<tr>\n"; 
foreach ($get_info as $field)  
echo "\t<td><font face=arial size=1/>$field</font></td>\n";  
echo "</tr>\n";  
}  
echo "</table>\n"; 
}
?>
 
</center>
</form></td></tr></table>
</center>
 


Now i can able to get with filed name and rows also. I have posted this because it will help to some one.

Thanks with Regards,

Yudha2008

Re: php mysql query from input textbox

Posted: Fri Oct 23, 2009 2:56 am
by markusn00b
By the way, you're wide open to SQL injection.