Forum Moderators: coopster

Message Too Old, No Replies

Using a form to retrieve data from a MySQL database

         

coho75

3:21 pm on Nov 24, 2003 (gmt 0)

10+ Year Member



Maybe someone here can help me out. What I am trying to do is make a form that contains checkboxes that users can check to compare a few different cell phones. When the form is submitted it will access the MYSQL database and display the phones that were chosen on the users screen.

The problem I am having is, that when the checkboxes are checked the form is only retrieving one row from the database. I cannot figure out how to display all rows that were chosen. Below is the code that I have been playing with.

<html>
<form name="form1" method="post" action="formaction.php">
<p>Select Phone
<input type="checkbox" name="number" value="1">phone 1
<input type="checkbox" name="number" value="2">phone 2
<input type="checkbox" name="number" value="3">phone 3
<input type="checkbox" name="number" value="4">phone 4
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</html>

<?
$host = "localhost";
$user = "username";
$pass = "password";
$dbname = "dbname";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);

$sql = "select * from cellphones where id='" . $_POST['number']. "'";
$result = mysql_query($sql);

if ($myrow = mysql_fetch_array($result)) {

echo "<table border=1>\n";

echo "<tr><td>Name</td><td>Position</td></tr>\n";

do {

printf("<tr><td>%s %s</td><td>%s</tr>\n", $myrow["1"], $myrow["2"], $myrow["3"]);

} while ($myrow = mysql_fetch_array($result));

echo "</table>\n";

} else {

echo "Sorry, no records were found!";

}

?>

Any help will be greatly appreciated!

Thanks,
Howard

Nova Reticulis

4:27 pm on Nov 24, 2003 (gmt 0)

10+ Year Member



You probably want to do something like

while ($row = mysql_fetch_assoc($result))
echo "<tr><td>$row[row1]</td><td>$row[row2]</td></tr>";

coho75

5:25 pm on Nov 24, 2003 (gmt 0)

10+ Year Member



Thanks Nova Reticulis, I tried your suggestion but still no luck.

It still returns only one phone.

For example, I have for checkboxes:
[]Phone 1 []Phone 2 []Phone3 []Phone4.

For some reason if more than one is checked, the last one checked is the only one that is returned. I would like to return all phones that are checked for a side-by-side comparison.

Thanks again,
Howard

jatar_k

5:52 pm on Nov 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld coho75

try something like this

$sql = "select * from cellphones where id IN (";
foreach ($_POST['number'] as $phonetype) $sql .= "'" . $phonetype . "',";
$sql = substr($sql,0,-1) . ")";

$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($result)) {

you are using id= which will only return matches for a single id you need to find any phone that has 1 of many ids that may be passed. This means you should use IN('value1','value2','value3','value4'). The while loop change Nova_Reticulis mentioned is also necessary to loop through all of the returned rows. The if would only ever return the first one.

Also take a look at this thread as well as the ones linked from it
Basics of extracting data from MySQL using PHP [webmasterworld.com]

coopster

2:22 am on Nov 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Actually, you are trying to retrieve multiple values from a form element that has the same name attribute. The only way to accomplish this is via PHP's recommendations for creating arrays in an HTML <form> [us4.php.net]. Read through this discussion thread and see if it helps:

[webmasterworld.com...]

coho75

9:56 pm on Nov 25, 2003 (gmt 0)

10+ Year Member



Thanks for all the help so far. I wish that I could say that i have got it all working, but I haven't.

Instead of returning only one value, I am now getting all four values. This occurs if I check no boxes, one box, or all boxes. Once again, I am trying to retrieve records from a MySQL database that correspond with the boxes that are checked on the form.

A form filled out like this:
phone1[x] phone2[ ] phone3[x] phone[ ]

would return a page with:
phone1 phone3
results results

This is the code that I have been playing with:

<html>
<form name="form1" method="post" action="formaction.php">
<p>Select Phone
<input type="checkbox" name="id[]">phone 1
<input type="checkbox" name="id[]">phone 2
<input type="checkbox" name="id[]">phone 3
<input type="checkbox" name="id[]">phone 4
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</html>

<?
include "common_db.inc";
error_reporting(0);

$link_id = db_connect(cellphones);
if(!$link_id) die(sql_error());
else echo "";

$query = "SELECT * FROM tablename WHERE id IN (1, 2, 3, 4)";
$result = mysql_query($query);
foreach ($_POST['id'] as $phone);

while ($row = mysql_fetch_array($result)) {

echo "{$row['name']}";
}

?>

Sorry that I am not catching on so quickly. I am kind of new with php/mysql. Thanks to those who have taken time to post here.

Thanks,
Howard

coopster

10:31 pm on Nov 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You are getting all four values because you have hardcoded them into your list as 1, 2, 3, 4:

$query = "SELECT * FROM tablename WHERE id IN (1, 2, 3, 4)";

You are almost there! Just change your query statement build as suggested earlier (and the loop, too) and you should get the desired results:

$sql = "select * from cellphones where id IN (";
foreach ($_POST['id'] as $phonetype) $sql .= "'" . $phonetype . "',";
$sql = substr($sql,0,-1) . ")";
$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($result)) {
print $row['name'];
}

coho75

10:51 pm on Nov 25, 2003 (gmt 0)

10+ Year Member



Ok, this is what I have now:

<?
include "common_db.inc";
error_reporting(0);

$link_id = db_connect(cellphones);
if(!$link_id) die(sql_error());
else echo "The connection to the db occurred";

$sql = "select * from cellphones where id IN (";
foreach ($_POST['id'] as $phonetype)$sql .= "'" .$phonetype . "',";
$sql = substr($sql,0,-1) . ")";
$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($result))

{

print $row['name'];

}

?>

I included in this "The connection to the db occurred" to make sure that it was connecting to the db. So now "The connection to the db occurred" is displayed at the top of the page but nothing else does. I am lost.

Thanks,
Howard

coopster

11:06 pm on Nov 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



OK, let's see if your query statement looks OK. Let's drop an exit statement in right after the query statement build to see if it looks good, especially the part where it builds the IN clause from the POSTed <form> data. Insert the exit statement as follows:

$sql = substr($sql,0,-1) . ")";
exit($sql);
$result = mysql_query($sql);

This will halt all processing and output the $sql variable to your browser so you can see where the statement isn't looking right. Hang in there, you'll get there ;)

coho75

11:11 pm on Nov 25, 2003 (gmt 0)

10+ Year Member



This is what outputted to the screen

The connection to the db occurredselect * from cellphones where id IN ('on','on')

thanks for the help Coopster.

coopster

12:22 am on Nov 26, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You don't have value attributes on your checkboxes anymore. You're going to need them to match the values you are trying to select from the database. Originally, in msg#1 you had values, you need to put them back in to match the id field in your table ;)

coho75

12:38 am on Nov 26, 2003 (gmt 0)

10+ Year Member



I still get nothing on the screen. I have tried what you (coopster) have suggested. I also tried numbering the array in the html name="id[1]". This did not work either. I have been working on this for quite a while now, and I seem to be making little progress. I do thank you for your time. I am trying to learn php/mysql but I just keep getting more frustrated.

This is now what I have:

<html>
<form name="form1" method="post" action="formaction.php">
<p>Select Phone
<input type="checkbox" name="id" value="1">phone 1
<input type="checkbox" name="id" value="2">phone 2
<input type="checkbox" name="id" value="3">phone 3
<input type="checkbox" name="id" value="4">phone 4
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</html>

<?
include "common_db.inc";
error_reporting(0);

$link_id = db_connect(cellphones);
if(!$link_id) die(sql_error());
else echo "The connection to the db occurred";

$sql = "SELECT * FROM cellphones WHERE id IN (";
foreach ($_POST['id'] as $phonetype)
$sql .= "'" . $phonetype . "',";
$sql = substr($sql,0,-1) . ")";
exit($sql);
$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($sql))

{
print $row['name'];
}

?>

Using the exit($sql) in the .php file now outputs this:

The connection to the db occurredSELECT * FROM cellpho_cellphonefacts.cellphones WHERE id IN )

Howard

coopster

12:54 am on Nov 26, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Looking at what you have here, I don't understand how you could get that value during the exit($sql)...unless we are dealing with more than one script file...

How many script files are involved?
And what are the names/functions of each?
Is this one displayed here named

formaction.php
?

And try not to get frustrated, you're getting very close.

coho75

12:57 am on Nov 26, 2003 (gmt 0)

10+ Year Member



The files being used are:

formtest.html
formaction.php
common_db.inc (connects to the db)

I am sorry that I am being such a pain. Thanks for the help.

coopster

1:31 am on Nov 26, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You're not being a pain. That's why this forum is here, to help each other!

I'm assuming the html listed here is from formtest.html and the php listed here is the formaction.php. Let's make sure we get the html correct first. You are going to need the brackets [webmasterworld.com] to return multiple values, that's just the way it works with checkboxes and select lists in PHP:


formtest.html
<html>
<form name="form1" method="post" action="formaction.php">
<p>Select Phone
<input type="checkbox" name="number[]" value="1">phone 1
<input type="checkbox" name="number[]" value="2">phone 2
<input type="checkbox" name="number[]" value="3">phone 3
<input type="checkbox" name="number[]" value="4">phone 4
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</html>

Now, let's make sure the script is ready:


formaction.php
<?php
include "common_db.inc";
error_reporting(0);
$link_id = db_connect(cellphones);
// Cleaned up your syntax a bit here, coho75:
if(!$link_id) {
die(sql_error());
} else {
echo "The connection to the db occurred";
}

$sql = "SELECT * FROM cellphones WHERE id IN (";
//number is the correct element now, not id:
foreach ($_POST['number[]'] as $phonetype) $sql .= "'" . $phonetype . "',";
$sql = substr($sql,0,-1) . ")";
//exit($sql); // commented this out for now
$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($result)) { // you had the wrong variable in here!
print $row['name'];
}
?>

This should work (assuming your common_db.inc script has no issues). If this doesn't work, uncomment the exit($sql) line and let's see what we have.

coopster

1:34 am on Nov 26, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The one thing I finally noticed missing was the brackets [] that need to be specified in the $_POST variable. I didn't catch that earlier myself! It's the little things...

coho75

1:58 am on Nov 26, 2003 (gmt 0)

10+ Year Member



I made all of your suggested changes and still no luck. I uncommented the exit($sql) section and it returned the same thing as before:

The connection to the db occurredSELECT * FROM cellphones WHERE id IN )

Thanks Again,
Howard

coho75

12:35 pm on Nov 26, 2003 (gmt 0)

10+ Year Member



I have been working on this all night and still no luck. I keep getting no output.

coopster

2:31 pm on Nov 26, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm sorry, coho75, I was wrong when I added the brackets to the $_POST variable (I made that adjustment last minute, too -- then even followed it up with an incorrect explanation post!). Anyway, my mistake, change this back and you'll get what you want (I tested it, which is what I should have done in the first place):

//number is the correct element now, not id:
//...but the brackets don't belong in the variable, as I (incorrectly) showed you before:
foreach ($_POST['number'] as $phonetype) $sql .= "'" . $phonetype . "',";

coho75

10:50 pm on Nov 26, 2003 (gmt 0)

10+ Year Member



Thanks coopster! I now get the numbers instead of the ('on', 'on') but the page still won't display anything from the database. With the numbers I figure that it should grab the corresponding phones with those ids from the database. However, it doesn't seem to do that. The script is clearly connecting to the db, so I am clearly lost. Sorry for all the trouble.

thanks,
Howard

jatar_k

10:58 pm on Nov 26, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what do you get if you echo the actual query?

echo "<p>",$sql;

coho75

11:10 pm on Nov 26, 2003 (gmt 0)

10+ Year Member



I get:

SELECT * FROM cellphones WHERE id IN ('1','3')

The numbers correspond to the id number in my db.

coho75

11:28 pm on Nov 26, 2003 (gmt 0)

10+ Year Member



Ok, I think I got it. I had $myrow instead of $row. Everything seems to be working now. Thanks to everyone for the time you spent helping me. I learned a lot. Thank you.

Howard

jatar_k

11:32 pm on Nov 26, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The query looks good as long as those are all the proper column and table names (It should return an error if not).

So, is there actual data in the table? If there is it should be working. I( am guessing that is an issue with your while loop then.

What are the names of the columns you need to output for each row?

<added>I was posting ;)

Glad you figured it out.