Forum Moderators: coopster
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
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
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]
[webmasterworld.com...]
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
$query = "SELECT * FROM tablename WHERE id IN (1, 2, 3, 4)";
$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'];
}
<?
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
$sql = substr($sql,0,-1) . ")";
exit($sql);
$result = mysql_query($sql);
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
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.
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'];
}
?>
//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 . "',";
thanks,
Howard
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.