473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

checkbox not passing to sql database

I am not a programmer, just putting bits and pieces I've found on the net. I am missing something here, can you help?
The "publishcityonline" checkbox is not sending the required 0 or 1 to mysql database. It shows up when the form requests the info, just not when I add or edit. I guess the 0 could be null if that helps.
If you need more info, don't hesitate to ask.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <p> click on name to display information for editing.<br>
  4. at bottom of page, fill in form and click enter information to submit new information.<br>
  5. click delete to delete record, be careful, there is no un-delete.
  6.  
  7. <?php
  8. include("header1.php");
  9.  
  10. if ($submit) {
  11.   // here if no ID then adding else we're editing
  12.  
  13.   if ($id) {
  14.     $sql = "UPDATE member SET
  15. EXPIRE='$EXPIRE',FIRSTNAME='$FIRSTNAME',LASTNAME='$LASTNAME',ADDRESS='$ADDRESS',PUBLISHCITYONLINE='
  16.  
  17. $PUBLISHCITYONLINE'
  18.   WHERE id=$id";
  19.   } 
  20.   else {
  21.     $sql = "INSERT INTO member (EXPIRE,FIRSTNAME,LASTNAME,ADDRESS,PUBLISHCITYONLINE) VALUES 
  22.  
  23. ('$EXPIRE','$FIRSTNAME','$LASTNAME','$ADDRESS','$PUBLISHCITYONLINE')";
  24.   }
  25.  
  26.   // run SQL against the DB
  27.  
  28.   $result = mysql_query($sql);
  29.   echo "Record updated/edited!<p>";
  30. } elseif ($delete) {
  31.  
  32.     // delete a record
  33.  
  34.     $sql = "DELETE FROM member WHERE id=$id";    
  35.     $result = mysql_query($sql);
  36.     echo "$sql Record deleted!<p>";
  37. } else {
  38.  
  39.   // this part happens if we don't press submit
  40.  
  41.   if (!$id) {
  42.  
  43.     // print the list if there is not editing
  44.  
  45.     $result = mysql_query("SELECT * FROM member order by LASTNAME",$db);
  46.     while ($myrow = mysql_fetch_array($result)) {
  47.       printf("<a href=\"%s?id=%s\">%s, %s</a> \n", $PHP_SELF, $myrow[0],$myrow[3], $myrow[2]);
  48.       printf("<a href=\"%s?id=%s&delete=yes\">  (Delete)</a><br>", $PHP_SELF, $myrow[0]);
  49.     }
  50.   }
  51.   ?>
  52.   <P>
  53. <!-- This part does not work: -->
  54. <a href="<?php echo $PHP_SELF?>">ADD A RECORD</a>
  55.   <P>
  56.  
  57. <form method="post" action="<?php echo $PHP_SELF?>">
  58.  
  59.  <?php
  60.   if ($id) {
  61.  
  62.     // editing so select a record
  63.  
  64.     $sql = "SELECT * FROM member WHERE id=$id";
  65.     $result = mysql_query($sql);
  66.     $myrow = mysql_fetch_array($result);
  67.     $id = $myrow[0];
  68. $EXPIRE=$myrow[1];
  69. $FIRSTNAME=$myrow[2];
  70. $LASTNAME=$myrow[3];
  71. $ADDRESS=$myrow[4];
  72. $PUBLISHCITYONLINE=$myrow[30];
  73.  
  74.     // print the id for editing
  75.  
  76.     ?>
  77.     <input type=hidden name="id" value="<?php echo $id ?>">
  78.     <?php
  79.   }
  80.   ?>
  81.  
  82. Chapter Expiration Date:<input type="Text" name="EXPIRE" value="<?php echo $EXPIRE ?>"><br>
  83. FIRSTNAME:<input type="Text" name="FIRSTNAME" value="<?php echo $FIRSTNAME ?>"><br>
  84. LASTNAME:<input type="Text" name="LASTNAME" value="<?php echo $LASTNAME    ?>"><br>
  85.  
  86. ADDRESS:<input type="Text" name="ADDRESS" value="<?php echo $ADDRESS ?>"><br>
  87.  
  88. PUBLISH CITY ONLINE:
  89. <input type="checkbox" name="PUBLISHCITYONLINE" value="<?PHP if($PUBLISHCITYONLINE =="1") { echo "checked"; } ?>"><br>
  90.  
  91.   <input type="Submit" name="submit" value="Enter information">
  92.   </form>
  93.  
  94. <?php
  95. }
  96. ?>
  97. <p> if no id we are adding, if id, then we're editing.  submit.</p>
  98. <P> <a href=display29.php>display29</a><br>
  99.  
  100. </body>
  101. </html>
Sep 8 '07 #1
8 2366
pbmods
5,821 Expert 4TB
Heya, Nick. Welcome to TSDN!

For your protection, I have removed your email address from your post text.

Please use CODE tags when posting source code:

[CODE=php]
PHP code goes here.
[/code]

Try using $_SERVER['PHP_SELF']. You may be working on a system that has register_globals turned off.
Sep 9 '07 #2
That didn't work.
Received error
404 File Not Found
The requested url /404.shtml was not found on this server.
Nick

I'm thinking I need some kind of
if isset[$PUBLISHCITYONLINE ==checkbox, checked then $PUBLISHCITYONLINE ==1 else $PUBLISHCITYONLINE ==0

I'm just not sure on the syntax and where it should go.
Sep 9 '07 #3
Try this:

Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" name="PUBLISHCITYONLINE" value="1" <?PHP if($PUBLISHCITYONLINE =="1") { echo "checked"; } ?>>
  2.  
The value field is passed iff the box is checked. The way you had it, if the value of the field was "1" then you were changing the value to "checked".

It's highly inadvisable to rely on register globals to be on. You should access the data through the post array, like this:

Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" name="PUBLISHCITYONLINE" value="1" <?PHP if($_POST['PUBLISHCITYONLINE'] =="1") { echo "checked"; } ?>>
  2.  
Though, if you do that change, you'll need to substantially rework this script. Doing so will, however, remove the SQL injection attack that your code is now easily subject to.
Sep 9 '07 #4
Thank you very much. Been on this most of the day.
Found the script at webmonkey.com, without the checkboxes.
The script is within a password protected directory. I hope this is secure enough that we will not sustain a sql attack or loose members names and addresses.
The ideal script would allow members to access and update their own information by entering key info only they know. Membership managers would be able to add, edit, and delete any or all records.
If anyone has scripts already written, I'd be greatful if they would share them.
Thanks again
Nick
Sep 9 '07 #5
Well, you're trusting all your members to never have their password compromised and to never try and hack you. Call me cynical, but I don't offer such trust to anyone.

It could help a bit to add extract($_POST) above all of your processing as that will replace variables set via the $_POST array that could be otherwise overwritten, but people can edit which fields are posted fairly easily.
Sep 9 '07 #6
I just thought of a slightly better quick-fix. AFTER you have connected to your database and BEFORE you use any of the POST variables, insert this code:

Expand|Select|Wrap|Line Numbers
  1. foreach ($_POST as $name=>$value) {
  2.   $_POST[$name] = mysql_real_escape_string(stripslashes($value));
  3. }
  4. extract ($_POST);
  5.  
Sep 9 '07 #7
I tried it and now it doesn't display the member list, just ",delete" for each id.
If I use seperate pages for display all, edit, and add, I think your code will work ok.


As allways with me, any and all comments are welcome.
Thanks again, I really appreciate your help.
Nick
Sep 9 '07 #8
pbmods
5,821 Expert 4TB
Heya, Nick.

When a form contains checkboxes, only the checked boxes will be submitted.

You may want to try something like this:
Expand|Select|Wrap|Line Numbers
  1. echo '<input type="checkbox" name="delete[', $row['id'], ']" /> Delete';
  2.  
Which will give you something that looks like this:
Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" name="delete[2]" /> Delete
  2. <input type="checkbox" name="delete[3]" /> Delete
  3.  
and so on.

Now let's pretend that the User checks the 'delete[3]' checkbox, but not the 'delete[2]' checkbox. When the User submits the form, here's what $_POST looks like:
Expand|Select|Wrap|Line Numbers
  1. array
  2. (
  3.     'delete' =>
  4.         array
  5.         (
  6.             3 => 'on'
  7.         ),
  8.     .
  9.     .
  10.     .
  11. );
  12.  
Note that only the checked box showed up in $_POST, which makes checking to see if you need to delete anything fairly simple:
Expand|Select|Wrap|Line Numbers
  1. if( ! empty($_POST['delete']) )
  2. {
  3.     foreach( $_POST['delete'] as $_id => $_on )
  4.     {
  5.         // Delete record # $_id.
  6.     }
  7. }
  8.  
Sep 9 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Bill | last post by:
I have a catalog of books which need to be categorized into different groups. Some of the books can be listed under more than one category. I'm creating a page where I can assign a group of books...
4
by: Jack | last post by:
Hi, I have a checkbox the value which goes to a database via a asp page that builds the sql string. In the front end asp page, the checkbox code is written as follows: <i><input...
9
by: mike7510uk | last post by:
Hi, I am using a gridview with a templatefield containing a checkbox. I want to update the database with a 1 or 0 depending on if a checkbox is checked or unchecked (then use the 1 or 0 later on...
3
by: shobu | last post by:
passing array checkbox value and update the database <?include 'dbconnect.php'; error_reporting(0);$update_qr="update...
3
by: delram | last post by:
I'm trying to get user selections (using checkboxes) on one JSP page(A.jsp) and pass them on to another page. So I have one page with a list of dynamically populated items like this 1 2 ...
0
by: cyberdawg999 | last post by:
Greetings all in ASP land I have overcome one obstacle that took me 2 weeks to overcome and I did it!!!!! I am so elated!! thank you to all who invested their time and energy towards helping me...
11
by: manjunath1985 | last post by:
Hi, I am really having problem in checkbox.... i have a php page which displays the username retrieved from the database i want to create a checkbox for each username and pass the selected...
3
by: Archanak | last post by:
Hi, Any limits exists while passing values of checkbox to another program? Here is the code: <script type="text/javascript"> function valuate() {
6
by: mukeshrasm | last post by:
Hi I want to update records in database based on selected records using checkbox. if user does not select a record and clicks update button it should show the message please select record to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.