Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

editing db with checkboxes 2

Status
Not open for further replies.

dunskii

Programmer
Sep 14, 2001
107
AU
Hi there,

I'm trying to to have an edit page where some data is collect via check boxes.

how do you check the boxes that have previously been checked and storded in the db.

I have a table for the admissions and one for the employees which are joined by bar_admis, this contains the id from both tables

This is how i am trying to do it.

<td><input type=&quot;checkbox&quot; name=&quot;<? echo($myrow[&quot;admissions.admis_id&quot;]); ?>&quot; <? if ($myrow[&quot;bar_admis.bar_id&quot;]) != 0 { echo (&quot;checked&quot;);} ?>><span class=&quot;text&quot;><? echo($myrow[&quot;admissions.admission&quot;]); ?></span></td>

but i keep getting a parse error.

thanks again,

dunskii
 
well ... i think one of your errors is the use of the tablenames. Usually you don't need to use them, only the name of the fields.
About the code, is fine. I don't get the syntax error, if is in this line.

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
i still cant get it working,

heres the code for that section

<?php

$db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;);

mysql_select_db(&quot;fjc&quot;,$db);

$res2 = mysql_query(&quot;SELECT * FROM admissions, bar_admis WHERE (barristers.bar_id = bar_admis.bar_id) and (bar_admis.admis_id = admissions.admis_id&quot;,$db);




$cols = 4;
$counter = 1;

print '<table width=\&quot;40%\&quot; cellspacing=\&quot;4\&quot;><tr>';
while ($myrow = mysql_fetch_array($res2)) {
if (is_int($counter / $cols)) {

?>
<td><input type=&quot;checkbox&quot; name=&quot;<? echo ($myrow[&quot;admissions.admis_id&quot;]); ?>&quot; <? if ($myrow[&quot;bar_admis.bar_id&quot;]) != 0 { echo (&quot;checked&quot;);} ?>><span class=&quot;text&quot;><? echo($myrow[&quot;admissions.admission&quot;]); ?></span></td></tr><tr>

<?
}
else {
?>

<td><input type=&quot;checkbox&quot; name=&quot;<? echo ($myrow[&quot;admissions.admis_id&quot;]); ?>&quot; <? if
($myrow[&quot;bar_admis.bar_id&quot;]) != 0 { echo &quot;checked&quot;;} ?>><span class=&quot;text&quot;><? echo ($myrow[&quot;admissions.admission&quot;]); ?></span></td>

<?
}
$counter++;
}
print '</tr></table>';

?>

thanks,

dunskii
 
what is the output of it?

once again i say. You don't need to use the table name in the data fields. You are not going to get the correct values. So, instead of $myrow[&quot;admissions.admis_id&quot;] use $myrow[&quot;admis_id&quot;]

If you have repeated field names in different tables you must name them like:

select t1.name as t1_name, t2.name as t2_name ...

and you fetch data with:
$array[t1_name] and $array[t2_name]



Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
I'm wondering about your quoting, also...


<td><input type=&quot;checkbox&quot; name=&quot;<? echo ($myrow[&quot;admissions.admis_id&quot;]); ?>&quot; <? if ($myrow[&quot;bar_admis.bar_id&quot;]) != 0 { echo (&quot;checked&quot;);} ?>><span class=&quot;text&quot;><? echo($myrow[&quot;admissions.admission&quot;]); ?></span></td></tr><tr>

See what's enclosed in bold &quot;'s on the first line there? That might be what's throwing everything off. It may be seeing name=&quot;<? echo ($myrow)&quot; and stops parsing with that second double quote. Have you tried single quotes?

<input type=&quot;checkbox&quot; name=&quot;<? echo ($myrow['admissions.admis_id']) ?>&quot; ...... > Matt
matt@paperlove.org
If I can help, I will.
 
That's not the problem. you can use double quotes in the index of the arrays.

He must tell use the line number and the line too of the error and then we can try to figure it out.

As a parse error is something missing, a semicolon, a parenthisis, a double_quotes, etc. So many things to seek. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
You can use double quotes in array indices, but within other double quotes? That's what I was pointing out.

I was thinking the HTML parsing will go bad when it gets to the <input type=&quot;checkbox&quot; name=&quot;<? echo ($myrow[&quot;admissions.admis_id&quot;]); ?>&quot; ...etc...> because the value for name will be parsed as &quot;<? echo ($myrow[&quot;.

If that's not it, so be it... The parser is cleverer than I give it credit for. :) But I avoid nesting double quotes like the plague though. Too easy to run into such problems.
Matt
matt@paperlove.org
If I can help, I will.
 
no way.

what is outside <? ?> is passed directly to the output.
inside is processed by the php server before sending anything to the user.
so it will not go bad with that. i use that code thousands of times without errors.

Yes the parser is clever. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
:) Glad to hear it and learn something new myself!

Though I'll still avoid nested double quotes like the plague... ;) Matt
matt@paperlove.org
If I can help, I will.
 
Hi there,

thanks for all your input,

The parse error turned out to be a typo, but now i get an error saying: Supplied argument is not a valid MySQL result resource

On the red line, i change the $myrow to $row1 but it didnt change the error.

<?php

$db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;);

mysql_select_db(&quot;fjc&quot;,$db);

$res3 = mysql_query(&quot;SELECT * FROM area, bar_area WHERE (barristers.bar_id = bar_area.bar_id) and (bar_area.area_id = areas.area_id&quot;,$db);

$cols = 3;
$counter = 1;

print '<table width=\&quot;40%\&quot; cellspacing=\&quot;4\&quot;><tr>';
while ($myrow = mysql_fetch_array($res3)) {
if ($counter%$cols==0) {

?>
<td><input type=&quot;checkbox&quot; name=&quot;<? echo($myrow[&quot;areas.area_id&quot;]); ?>&quot; <? if ($row1[&quot;bar_area.bar_id&quot;] != 0 ) { print (&quot;checked\n&quot;);} ?>><span class=&quot;text&quot;><? echo($row1[&quot;areas.area&quot;]); ?></span></td></tr><tr>

<?
}
else {
?>

<td><input type=&quot;checkbox&quot; name=&quot;<? echo($myrow[&quot;areas.area_id&quot;]); ?>&quot; <? if ($row1[&quot;bar_area.bar_id&quot;] != 0 ) { print (&quot;checked\n&quot;);} ?>><span class=&quot;text&quot;><? echo($row1[&quot;areas.area&quot;]); ?></span></td>

<?
}
$counter++;
}
print '</tr></table>';


dunskii
?>
 
That's another problem.

This is your query:
SELECT * FROM area, bar_area WHERE (barristers.bar_id = bar_area.bar_id) and (bar_area.area_id = areas.area_id)

You are using fields of the barristers table without catching them in the from.

another thing, you can use alias for the table names, soyou can do something like this:
SELECT * FROM area a, bar_area ba, barristers b WHERE b.bar_id = ba.bar_id and ba.area_id = a.area_id

This is similar and you can reduce some chars in the query.

Other thing, the parenthisis are nothing but junk there. You can remove them.
The (=) operator is executed before the (and) operator.


Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Thanks for all your help on that, much appriciated.

When processing this to update the database, what will the name of the check box be, or will i have to set up another array for it.

thanks

dunskii
 
This has been really puzzling me...please put me out of my misery :)

If you have assigned the name of the checkbox from a query result, how do you capture and process the (checked/unchecked) back into the mysql UPDATE statement using the checkbox name and value ?
(especially with a checkbox for every item returned?)

An example would be great, I posted a question about checkboxes ages ago and it attracted no attention.
Thanks in advance,

Kev ***************************************
Party on, dudes!
[cannon]
 
suppose you have

<input type=checkbox name=blue value=1> Blue

then

$blue=$blue?$blue:0;
update table set blue=$blue where ... Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Hehe sorry, I really don't get this checkbox stuff at all.

I mean I wouldn't get it if it fell out of the sky and landed on head.

All I can get to happen is :
$blue=$blue?$blue:0; returns a 0 always. ***************************************
Party on, dudes!
[cannon]
 
ok ...

replace $blue by $_GET[blue] or $_POST[blue], if you are using GET or POST as the method of the form.

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Ah, now we're smokin' :) Thanks Anikin.

Whoopeeeee I finally can use da checkbox (LOL) only took 6 months... ***************************************
Party on, dudes!
[cannon]
 
That all just went over my head. How would that fit into this.


$db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;);

mysql_select_db(&quot;fjc&quot;,$db);

$admissions = mysql_query(&quot;INSERT INTO bar_admis (bar_id, admis_id) VALUES (LAST_INSERT_ID(), '$admis_id')&quot;,$db);


dunskii
 
What are you trying to fit in your problem? the checkbox?

You have a checkbox and you want to insert her status in the database?

if so: (supponsing the check's name is admins_id)

$admins_id=$_GET[admins_id]?$_GET[admins_id]:0;
$admissions = mysql_query(&quot;INSERT INTO bar_admis (bar_id, admis_id) VALUES (LAST_INSERT_ID(), '$admis_id')&quot;,$db);


Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top