Jump to content

[SOLVED] Checkboxes value are from database - send through form?


djfox

Recommended Posts

I have a sort of survey-ish poll created using database entries. Here`s the code:

<form action="sth_poll_1_vote.php" method="post">
<?php
  $recentchara = 0;
  $reschara = mysql_query("SELECT id, nme, approv FROM beloved_sonic_charac WHERE approv='1' ORDER BY nme ASC")or die( mysql_error() );
  while( ($chara = mysql_fetch_row($reschara)) && $recentchara < 100 ){
    $recentchara = $recentchara + 1;
    echo "<input type='checkbox' name='$chara[0]'> $chara[1] <br>";
}
?>
<p>
<input type="submit" value="Cast Your Vote" name="submit">
</form>

 

Here`s a link if you want to see the end result of the forum: http://secrettrance.net/beloveddoll/sth_poll_1.php

 

How would I get the code to process the entries (because there would be more than one being sent) so that a vote could be added to the table for each entry that is voted for?

Link to comment
Share on other sites

sry.. i don't understand your script and logic..

 

but i think this perhaps the near what u wanted

<form action="sth_poll_1_vote.php" method="post">
<?php
  $recentchara = 0;
  $reschara = mysql_query("SELECT id, nme, approv FROM beloved_sonic_charac WHERE approv='1' ORDER BY nme ASC")or die( mysql_error() );
  while( ($chara = mysql_fetch_row($reschara)) ){
    $recentchara = $recentchara + 1;
    echo "<input type='checkbox' name='chara[]' value=$chara[0] > $chara[1] <br>";
}
?>
<p>
<input type="submit" value="Cast Your Vote" name="submit">
</form>

btw..

* i erase "&& $recentchara < 100 "

* i change input type....

Link to comment
Share on other sites

It`s not the form itself I`m having a problem with, it`s the page that the form submits the info to. I can`t get the code to pick up the submitted info unless I do something like this:

if (isset($_POST['1'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='1' LIMIT 1');
}

 

for each and every single entry in the database. This is not efficient or practical, and a pain to deal with later as more stuff gets added to the database. I`ve been trying many different ways to get it to go on it`s own so I don`t have to hand enter stuff like that but nothing is working.

Link to comment
Share on other sites

<?php
if (isset($_POST['1'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='1' LIMIT 1');
}
if (isset($_POST['2'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='2' LIMIT 1');
}
if (isset($_POST['3'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='3' LIMIT 1');
}
if (isset($_POST['5'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='5' LIMIT 1');
}
if (isset($_POST['6'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='6' LIMIT 1');
}
if (isset($_POST['7'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='7' LIMIT 1');
}
if (isset($_POST['8'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='8' LIMIT 1');
}
if (isset($_POST['9'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='9' LIMIT 1');
}
if (isset($_POST['10'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='10' LIMIT 1');
}
if (isset($_POST['11'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='11' LIMIT 1');
}
if (isset($_POST['12'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='12' LIMIT 1');
}
if (isset($_POST['13'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='13' LIMIT 1');
}
if (isset($_POST['14'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='14' LIMIT 1');
}
if (isset($_POST['15'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='15' LIMIT 1');
}
if (isset($_POST['16'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='16' LIMIT 1');
}
if (isset($_POST['17'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='17' LIMIT 1');
}
if (isset($_POST['18'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='18' LIMIT 1');
}
if (isset($_POST['19'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='19' LIMIT 1');
}
if (isset($_POST['20'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='20' LIMIT 1');
}
if (isset($_POST['21'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='21' LIMIT 1');
}
if (isset($_POST['22'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='22' LIMIT 1');
}
if (isset($_POST['23'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='23' LIMIT 1');
}
if (isset($_POST['24'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='24' LIMIT 1');
}
if (isset($_POST['25'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='25' LIMIT 1');
}
if (isset($_POST['26'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='26' LIMIT 1');
}
if (isset($_POST['27'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='27' LIMIT 1');
}
if (isset($_POST['28'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='28' LIMIT 1');
}
if (isset($_POST['29'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='29' LIMIT 1');
}
if (isset($_POST['30'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='30' LIMIT 1');
}
if (isset($_POST['31'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='31' LIMIT 1');
}
if (isset($_POST['32'])) {
	mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='32' LIMIT 1');
}

mysql_close($con);
header("Location: sth_polls.php");
?>

Link to comment
Share on other sites

Looks quite simple really.

 

The structure would be:

 

FORM_
<form action="" method="post">
<input type="checkbox" name="POLL[id_number]">id_name <Br />
<input type="checkbox" name="POLL[id_number2]">id_name2 <Br />
<input type="checkbox" name="POLL[id_number3]">id_name3
</form>

 

 

You can generate something like this eg:

echo "<input type='checkbox' name='POLL[".$chara[0]."]'> $chara[1] <br>";

 

So, when you get these result on the next page, you would loop the "POLL" array, eg:

 

foreach($_POST['POLL'] As $PollItem=>$PollValue){
   // update $PollItem add 1 etc. ($PollValue will always be "on" or w/e).
}

 

Hope this helps :P

Link to comment
Share on other sites

omg.. talking about horible..

auch.. forgive my manner

 

well.. after i see your logic.. the logic u use are not effective.

wew.. if i add new chara like Baby sonic..

i must type in the script..

that's no effective.. u must think.. how to make script that u not change it all..

except there is an error in it ^^

 

try using my script above than

use this for result page

<?php

foreach($_POST(chara) as $val)
{
   $sql="UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='$val' LIMIT 1";
    mysql_query($sql) or die(mysql_error());


}

?>

fyi.. your script have false on this

mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='7' LIMIT 1');

 

u see.. u use ' if you use ".. perhaps it work ^^ wew..

 

uniflare: nice idea but u wrong about the 1st. after execute.. it should be like this

<form action="" method="post">
<input type="checkbox" name="1"> name <Br />
<input type="checkbox" name="2">name2 <Br />
<input type="checkbox" name="3"> name3
</form>

hmm.. something wrong here?

Link to comment
Share on other sites

Ok, I get this error:

Parse error: syntax error, unexpected T_VARIABLE in /home/secrett1/public_html/beloveddoll/sth_poll_1_vote.php on line 46

 

Which is this:

foreach($_POST['POLL'] As $PollItem=>$PollValue){
   mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` SET `like` = like +1 WHERE `beloved_sonic_charac`.`id` ='$PollValue' LIMIT 1');
}

 

Which is odd because I use the same updating code for a different part of the site and it works fine.

Link to comment
Share on other sites

djfox:

omg.. i think u type wrong in above script

i just check.. and i found this

<input type='checkbox' name='POLL[5]'> Amy Rose <br>
<input type='checkbox' name='POLL[29]'> Bark the Polarbear <br>
<input type='checkbox' name='POLL[28]'> Bean the Dynamite <br>
<input type='checkbox' name='POLL[10]'> Big the Cat <br>.....

above script must type

while( ($chara = mysql_fetch_row($reschara)) && $recentchara < 100 ){
    $recentchara = $recentchara + 1;
    echo "<input type='checkbox' name='POLL[ $chara[0] ]'> $chara[1] <br>";
}

 

djfox:

the problem are here

WHERE `beloved_sonic_charac`.`id` ='$PollValue'

'$PollValue' >> see begin with what? u should type

WHERE `beloved_sonic_charac`.`id` ='.$PollValue.'....

Link to comment
Share on other sites

foreach($_POST['POLL'] As $PollItem=>$PollValue){
   mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac` 
      SET `like` = (like +1) WHERE `beloved_sonic_charac`.`id` =\'$PollValue\' LIMIT 1');
}

 

You did not escape the single quotes around $PollValue.

 

:P

Link to comment
Share on other sites

uniflare:

wakakakaka... lets pretend

$PollValue = 111;

if you do like that.. the query that will be execute is

 

UPDATE `secrett1_artgallery`.`beloved_sonic_charac`

      SET `like` = (like +1) WHERE `beloved_sonic_charac`.`id` ='$PollValue' LIMIT 1

 

 

fyi.. $PollValue are not change into 111 ^^

Link to comment
Share on other sites

M code is fine, it works, if you can use it or dont know how dont tell me its wrong!! it really gets on my nerves when people say the code is completely wrong when they dont even copy it right....

 

A)

uniflare:

wakakakaka... lets pretend

$PollValue = 111;

if you do like that.. the query that will be execute is

 

UPDATE `secrett1_artgallery`.`beloved_sonic_charac`

      SET `like` = (like +1) WHERE `beloved_sonic_charac`.`id` ='$PollValue' LIMIT 1

 

 

fyi.. $PollValue are not change into 111 ^^

 

First: its not $PollValue... READ my post - its $PollKey !!.

 

Second: if $PollValue = 111; The nthe query you just gave would work "Flawlessly", it would add 1 to the row with the id 111, which should be one of the checkboxes he checked. ok?

 

----

 

B) This is a BURN - Consult my signature!

==

 

PS, what is: wakakakaka?

--

 

There is more to FLAME you for but i cba...

Link to comment
Share on other sites

allright.. that's my fault

 

foreach($_POST['POLL'] As $PollItem=>$PollValue){
   mysql_query('UPDATE `secrett1_artgallery`.`beloved_sonic_charac`
      SET `like` = (like +1) WHERE `beloved_sonic_charac`.`id` =\'$PollValue\' LIMIT 1');
}

lets explain about the inside mysql_query which is

'UPDATE `secrett1_artgallery`.`beloved_sonic_charac`

      SET `like` = (like +1) WHERE `beloved_sonic_charac`.`id` =\'$PollValue\' LIMIT 1'

 

if you use this query.. the real query u execute are not (pretended $PollValue=5)

'UPDATE `secrett1_artgallery`.`beloved_sonic_charac`

      SET `like` = (like +1) WHERE `beloved_sonic_charac`.`id` =\'5\' LIMIT 1'

but

'UPDATE `secrett1_artgallery`.`beloved_sonic_charac`

      SET `like` = (like +1) WHERE `beloved_sonic_charac`.`id` ='$PollValue' LIMIT 1'

 

why?

 

how about type code

<?php
$PollValue=5;
echo 'UPDATE `secrett1_artgallery`.`beloved_sonic_charac`
      SET `like` = (like +1) WHERE `beloved_sonic_charac`.`id` =\'$PollValue\' LIMIT 1';

echo "UPDATE `secrett1_artgallery`.`beloved_sonic_charac`
      SET `like` = (like +1) WHERE `beloved_sonic_charac`.`id` ='$PollValue' LIMIT 1";

?>

which above this will return

UPDATE `secrett1_artgallery`.`beloved_sonic_charac`

      SET `like` = (like +1) WHERE `beloved_sonic_charac`.`id` =\'5\' LIMIT 1

Link to comment
Share on other sites

He is trying to explain "Literal" Strings in php.

Nice catch landavia.

 

Basically;

 

Any string enclosed in single quotes ', will NOT 'parse' variables and functions inside the string; as it is taken.. "Literally".

 

For Example;

 

$variable = "not";
$teststring = 'This string will $variable parse';
// Echos: This string will $variable parse

$variable = "not";
$teststring = "This string will $variable parse";
// Echos: This string will not parse. (but it does, obviously)

To use variables inside a literal string you must use "Concatenation"; eg:
$teststring = 'This string will '.$variable.' parse';
// Echos: This string will not parse. (but it does, obviously)

 

Hope it makes sense.




 

Now to fix your script. If you were to start all over again, ask the same question you did when u started, i would tell you this;

 

Change this:

<?php
  $recentchara = 0;
  $reschara = mysql_query("SELECT id, nme, approv FROM beloved_sonic_charac WHERE approv='1' ORDER BY nme ASC")or die( mysql_error() );
  while( ($chara = mysql_fetch_row($reschara)) && $recentchara < 100 ){
    $recentchara = $recentchara + 1;
    echo "<input type='checkbox' name='$chara[0]'> $chara[1] <br>";
}

 

to this:

<?php
  $query = "SELECT id, nme, approv 
    FROM beloved_sonic_charac 
    WHERE approv='1' 
    ORDER BY nme ASC 
    LIMIT 100"; // Limit to 100 results rather than using extra variables.
  
  $result = mysql_query($query)or die( mysql_error() . "<HR>".$query); // Execute Query Seperately
  
  while($chara = mysql_fetch_row($reschara)){
    echo "<input type='checkbox' name='POLL[".$chara[0]."]'> $chara[1] <br>";
  }
  // So this will create the entire checkbox form. Sorted, now to save these puppies.

 

The page where the form is submitted:

 

<?php
foreach($_POST['POLL'] As $PollItem=>$PollValue){
   $query = "UPDATE `table` SET `column`='(table.column + 1)' WHERE `id`='".$PollItem."';";
   $result = mysql_query($query) or die ("ERROR: ".mysql_error()."<HR>".$query);
}
?>

 

Hope this helps you out. And i hope you understand whats going on.

 

The checkboxes are all in an array "POLL". Basically the checkboxes only exist in POST data if the checkbox has been ticked (or selected), so, the only checkboxes that will end up in the POLL array are the ones that are checked. (which are the ones you want to update).

 

So all you do loop through each POLL item, and update accordingly. Just remember the `id` for that checkbox is the Array KEY, NOT the VALUE.

$array = array(
  "KEY" => "VALUE"
);

 

Voila.

Link to comment
Share on other sites

It still doesn`t want to add the 1 to the field for the items that are checked off

 

You want to add 1 to the items that have bee checked ON, or OFF? (ticked, or unticked)

 

And no that code was to explain what i meant about using the KEY rather than the VALUE to get the ID number.

 

if the query isnt working, echo the query like so:

 

<?php
foreach($_POST['POLL'] As $PollItem=>$PollValue){
   $query = "UPDATE `table` SET `column`='(table.column + 1)' WHERE `id`='".$PollItem."';";
   echo("ID NUMBER: $PollItem --- QUERY: ".$query."<br />"); // add this line here. give us what it says
   $result = mysql_query($query) or die ("ERROR: ".mysql_error()."<HR>".$query);
}
?>

Link to comment
Share on other sites

We want to add 1 to the items that are checked.

 

The results of the echo:

ID NUMBER: 6 --- QUERY: UPDATE `beloved_sonic_charac` SET `like`='(beloved_sonic_charac.like + 1)' WHERE `id`='6';

ID NUMBER: 1 --- QUERY: UPDATE `beloved_sonic_charac` SET `like`='(beloved_sonic_charac.like + 1)' WHERE `id`='1';

 

So the id numbers are going through fine. (I only chose two for this test.)

Link to comment
Share on other sites

Now we get:

 

ID NUMBER: 6 --- QUERY: UPDATE `beloved_sonic_charac` SET `like`=like+1 WHERE `id`='6';

ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like+1 WHERE `id`='6'' at line 1UPDATE `beloved_sonic_charac` SET `like`=like+1 WHERE `id`='6';

 

(I used the same two items I tested last time with.)

Link to comment
Share on other sites

It is because you named your column "like", this is very bad naming issue since mysql has a control clause name LIKE,

 

either, do this:

 

$query = "UPDATE `beloved_sonic_charac` SET `like`=(like+1) WHERE `id`='$PollItem';";

 

or rename your like column to something better (recommended)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.