updating a mysql database...using checkboxes...

  • ClericVash
  • Banned
  • Banned
  • User avatar
  • Posts: 65

Post 3+ Months Ago

Hi all this is my first time using checkboxes with mysql and ive probably totaly fluffed it up!

Here is what i currently have;

Code: [ Select ]
<?php
if ($view == modlinks) {
    $get = mysql_query("select * from links where allowed='0'");
    while ($display = mysql_fetch_array($get)) {
    print "
    <form name=\"modlinks\" method=\"post\" action=\"admin.php?action=allowlink\">
    <a href=$display[url]>$display[name] - $display[url]</a><input name=\"ad\" type=\"checkbox\" value=\"checkbox\"><br>
    <input type=\"submit\" value=\"Allow Link(s)\">
    </form>
    <form name=\"deleteall\" method=\"post\" action=\"admin.php?action=deletealllinks\">
    <input type=\"submit\" value=\"Delete All\">
    </form>
    ";
}}
if ($action == allowlink) {
    mysql_query("update links set allowed='1' where ad='checked' && where id=$display[id]") or die("Could not allow!");
    }
if ($action == deletealllinks) {
    mysql_query("delete from links where allowed='0'") or die("Could not delete!");
    }
?>
  1. <?php
  2. if ($view == modlinks) {
  3.     $get = mysql_query("select * from links where allowed='0'");
  4.     while ($display = mysql_fetch_array($get)) {
  5.     print "
  6.     <form name=\"modlinks\" method=\"post\" action=\"admin.php?action=allowlink\">
  7.     <a href=$display[url]>$display[name] - $display[url]</a><input name=\"ad\" type=\"checkbox\" value=\"checkbox\"><br>
  8.     <input type=\"submit\" value=\"Allow Link(s)\">
  9.     </form>
  10.     <form name=\"deleteall\" method=\"post\" action=\"admin.php?action=deletealllinks\">
  11.     <input type=\"submit\" value=\"Delete All\">
  12.     </form>
  13.     ";
  14. }}
  15. if ($action == allowlink) {
  16.     mysql_query("update links set allowed='1' where ad='checked' && where id=$display[id]") or die("Could not allow!");
  17.     }
  18. if ($action == deletealllinks) {
  19.     mysql_query("delete from links where allowed='0'") or die("Could not delete!");
  20.     }
  21. ?>


Basicly it retreives links from the databes that arnt currently allowed in the links page (allowed = '0'). And basicly i want to click the boxes of the ones i want to allow...click the allow button and they get set to allowed = '1'.

Have a fluffed this up or what!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

I am not sure if I do understand your question. I will make a comment anyway:

Code: [ Select ]
<input type="checkbox" name="check" value="1">
<input type="checkbox" name="check" value="1">
<input type="checkbox" name="check" value="1">
  1. <input type="checkbox" name="check" value="1">
  2. <input type="checkbox" name="check" value="1">
  3. <input type="checkbox" name="check" value="1">


Will make a variable like this:
Code: [ Select ]
$check[0] = 1


Then, you have to build a form using a single checkbox name, then browse the array for values of 1:

ie:
$check[0] -> 1 Checked
$check[1] -> 0 Not checked
$check[2] -> 1 Checked



[/code]
  • ClericVash
  • Banned
  • Banned
  • User avatar
  • Posts: 65

Post 3+ Months Ago

that sorta helps but i dont know how to put that in exactly?

as i would need it in the bit that updates the mysql using the things that are checked

now like this;

Code: [ Select ]
<a href=$display[url]>$display[name] - $display[url]</a><input type=\"checkbox\" name=\"check\" value=\"1\">


thats for the link name and url with your checkbox bit on it....now how to update the mysql...? as in;

Code: [ Select ]
mysql_query("update links set allowed='1' where ????") or die("Could not allow!");


I dont undestand how to now pick ones that are checked put them into something so it can update it?
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

Ok, you will have to go with one problem at a time.

1. The form:

Let's say you have 5 items.

Code: [ Select ]
<input type="checkbox" name="item" value="1">
<input type="checkbox" name="item" value="1">
<input type="checkbox" name="item" value="1">
<input type="checkbox" name="item" value="1">
<input type="checkbox" name="item" value="1">
  1. <input type="checkbox" name="item" value="1">
  2. <input type="checkbox" name="item" value="1">
  3. <input type="checkbox" name="item" value="1">
  4. <input type="checkbox" name="item" value="1">
  5. <input type="checkbox" name="item" value="1">


The resulting POST of the form should (and you need to try this first) make an array variable named item with five elements:

$item[0]
$item[1]
$item[2]
$item[3]
$item[4]

Each one with a value of 0 or 1.
1 for checked items
0 for unchecked items

Use a "debug" script for being sure about it. Create a PHP like this:


Code: [ Select ]
<?
$item = $_POST['item']; // You need this if globals is Off

for ($i=0; $i<5; $i++) {
  print("Item $i = ".$item[$i]."<br>");
}
?>
  1. <?
  2. $item = $_POST['item']; // You need this if globals is Off
  3. for ($i=0; $i<5; $i++) {
  4.   print("Item $i = ".$item[$i]."<br>");
  5. }
  6. ?>


Then post your form to that script. You will be able to know if the checkboxes are being reflected in the array variable.

2. You need to start by knowing HOW will you store the data. Let's say you have a table of links with:

int() link_id
varchar() link
bool update

In this case you will need to pass the link_id along with the checkbox state. But there are many ways to solve it. If you already have a data model, post it here. If not, you need to build one.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 46 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.