checkbox using a query and php

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Locked
meandrew
Forum Commoner
Posts: 49
Joined: Mon Feb 24, 2003 1:03 pm

checkbox using a query and php

Post by meandrew »

Hi all

I was using a select option for an insert and have discovered I need to have mnore than one of the records displyed available to be selected:

This is what I have which works and I don't want!

And this is what I now have that I want bvut doesn't show the check boxes :(

It displays the records, this could be a html error as I have had before but I have tried that with no joy

$result=mysql_query("SELECT CountyID, City, CityID FROM city WHERE CountyID=".$CountyID." ORDER BY City");
while ($row = mysql_fetch_array($result))
{
$county_id=$row['CountyID'];
$city_id=$row['CityID'];
$city=$row['City'];
if ($CityID == $city_id)
echo "<input type=\"checkbox\" name=\"$city_id\">";

echo "> $city $city</td>";
$count++;
if($count ==3){
echo "</tr><tr>";
$count=0;
}

}

Thank you
Andrew
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

Not sure about all your conditions, but the following should output checkboxes. You forgot the value attribute.

Code: Select all

<?php
while ($row = mysql_fetch_array($result))
{
echo "<input type="checkbox" name="$city_id" value="$Value">";
} 
?>
You also have the following line:
echo "> $city $city</td>";
The angle bracket (>) does not have a corresponding tag, so it will screw up your HTML formatting.
meandrew
Forum Commoner
Posts: 49
Joined: Mon Feb 24, 2003 1:03 pm

Post by meandrew »

I have now manged to sort this out thank you for your help :)

Code: Select all

echo "<tr><td>";
echo "Select Cities and Towns<br>";
echo "</td></tr>";
$queryC="SELECT CountyID, City, CityID FROM city WHERE CountyID=".$CountyID." ORDER BY City";
$result=mysql_query($queryC,$con_id);
$count=0;
echo "<tr>";
while($row=mysql_fetch_array($result))&#123;

$county_id=$row&#1111;'CountyID'];
        $city_id=$row&#1111;'CityID'];
		$city=$row&#1111;'City'];
if ($CityID == $city_id)

$city="city_".$city_id;
echo"<td width="25%"><input type="checkbox" name="$CityID">$city</td>";
$count++;
 if($count ==3)&#123;
  echo "</tr><tr>";
  $count=0;
 &#125;
&#125;
Andrew
meandrew
Forum Commoner
Posts: 49
Joined: Mon Feb 24, 2003 1:03 pm

Part two the Inserts for Two Tables

Post by meandrew »

I am now working on the insert into two tables :)

items and items_city

In each table there is the ID which is unique. This is what I have so far and I am not getting very far with it.

select from city and display ready for check box selection.

Code: Select all

echo "Select Cities and Towns<br>";
echo "</td></tr>";
$queryC="SELECT CountyID, City, CityID FROM city WHERE CountyID=".$CountyID." ORDER BY City";
$result=mysql_query($queryC,$con_id);
$count=0;
echo "<tr>";
while($row=mysql_fetch_array($result))&#123;

$county_id=$row&#1111;'CountyID'];
        $city_id=$row&#1111;'CityID'];
		$city=$row&#1111;'City'];
if ($CityID == $city_id)

$city="city_".$cityId;
echo"<td width="25%"><input type="checkbox" name="$city_id">$city</td>";
$count++;
 if($count ==3)&#123;
  echo "</tr><tr>";
  $count=0;
 &#125;
&#125;
once selected this is the form that should do the inserts ->


Code: Select all

<?
require("connection.php");
DBInfo();
Root();
$con_id=mysql_connect("$DBHost","$DBUser","$DBPass");
mysql_select_db($DBName);
mysql("$DBName","INSERT INTO Items VALUES(
'$ItemSKU',
'$ItemName',
'$ItemDescription',
'$PostCode',
'$Category',
'$CityID',
'$CTelephone',
'$ItemID',
'$Cfax',
'$Cemail',
'$Caddress',
'$CTown',
'$Cwww')");
$nItemId=mysql_insert_id();

$queryC="select * from city";
$result=mysql_query($queryC,$con_id);

while($row=mysql_fetch_array($result))&#123;

$cityId=$row&#1111;'CityID'];
$city="city_".$cityId;
 $c=$$city;
if($c == "on")&#123;
	$query1="insert into item_city (item_id,city_id)values('$nItemId','$cityId')";
	$result1=mysql_query($query1);
 &#125;
&#125;
/////////////////////////
?>

I have looked at this for too long and seem to be seeing the same thing :( so if someone can see something aloof that would be great :)

Andrew
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Locked