Page 1 of 1

Hi everyone. Looking to use an html form to get info from db

Posted: Thu Oct 14, 2010 1:38 pm
by hlmijendrix
Hi,

I'm new to php and working on a contact director that will do the following:

*User selects item from drop-down menu in html form.
*The php script connects to the db and returns all information about the one element (department abbreviation) that was selected.
*Prints the information (ideally) into a read only html form, but could also return the information as a plain table.

As of right now, I can connect to the db fine through my php script but that's about it. I would greatly appreciate any help I could get, even if it's just telling me a method or class that I should check out that would accomplish this task easily.

The following is the php script I have so far.

Thanks in advance,
hlmijendrix

Code: Select all

<?php
$username = "loginName";
$password = "password";
$hostname = "host"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL");
$info=$_POST["abbr"];

$query=mysql_query("select Abbr, Last, First, Email, PhoneNumber, CampusAddress, NetID from Contacts where Abbr=('$info')", $dbhandle);
while ($row = mysql_fetch_array($query , MYSQL_NUM)
	printf("Abbr: %s" , $query[0]);
?>

Re: Hi everyone. Looking to use an html form to get info fro

Posted: Fri Oct 15, 2010 5:59 am
by jraede
First of all you don't need the parentheses in your SQL query. Second, you're trying to access $query[0], with $query being your MySQL query object. It should be $row[0]

Re: Hi everyone. Looking to use an html form to get info fro

Posted: Fri Oct 15, 2010 9:22 am
by hlmijendrix
Hey jraede, thanks for the help. I changed that around, along with some other things and I think i'm pretty darn close to it now. I got it to read the inputted variable, but printing everything out is proving to be somewhat of a problem. I tried a different way of doing it and think that this might work a bit better. Here's the edited code, with a couple other changes as well:

Code: Select all

<?php
$username = "asdf";
$password = "zxcv";
$hostname = "asdf"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL");
$info=$_GET["department"];
$db = mysql_select_db('Contacts');


//performs query
$dbquery="select * from Contacts where Abbr=('$info')";
$dbresult = mysql_query ($dbquery, $dbhandle);

//prints results of query
if ($dbresult == false)
        {
                die  ("No matches<br>");
        }

while ($dbrow = mysql_fetch_array($dbresult, MYSQL_ASSOC))
        {
                print_r($dbrow);
                echo '<br>';
        }
		
//closes connection
mysql_close($dbhandle);
?>
It's returning no matches and the variable should definitely match something in the DB. Any obvious issues that I'm missing?

Thanks

Re: Hi everyone. Looking to use an html form to get info fro

Posted: Fri Oct 15, 2010 9:47 am
by hlmijendrix
Figured it out. Here's the code:

Code: Select all

<?php

//connection to the database
$dbhandle = mysql_connect('asdf', 'asdf', 'asdf') 
or die("Unable to connect to MySQL");
$info=$_GET["department"];
$db = mysql_select_db('registrar');

if ($db == false)
        {
                 die  ("Unable to Select MySQL Database<br>");
        }

//performs query
$dbquery="select * from Contacts where Abbr=('$info')";
$dbresult = mysql_query ($dbquery, $dbhandle);

//prints results of query
if ($dbresult == false)
        {
                die  ("No matches<br>");
        }

while ($dbrow = mysql_fetch_array($dbresult, MYSQL_ASSOC))
        {
                print_r($dbrow);
                echo '<br>';
        }
		
//closes connection
mysql_close($dbhandle);
?>
Now I just have to work on formatting the output into a more readable version.