Solved

web page - Check Box - How to update Mysql Data

Posted on 2014-04-21
10
746 Views
Last Modified: 2014-05-12
I created a form using PHP.  I have it linked to mysql and when someone select the Button the form they to send the data to MySQL, the check box data is not getting written to the database.
  Well the Name, address, email, all work fine they will update MySQL data base....but I not sure how to write code to update MYSQL from the check box?


********************Update MYSQL*********************************
('$_POST[RegDate]','$_POST[FirstName]','$_POST[LastName]','$_POST[EmailAddress]','$_POST[PhoneNumber]','$_POST[CECredits]')";
//THIS IS A TEST
  /*if (empty ($_Post["RegDate"]))*/
    /*{$$RegDate = $_POST['RegDate'];*/
  $RegDate = $_POST['RegDate'];
  //$IP = $_POST['IP'];
  $FirstName = $_POST['FirstName'];
  $LastName = $_POST['LastName'];
  $EmailAddress = $_POST['EmailAddress'];
  $PhoneNumber = $_POST['PhoneNumber'];
  $CECredits = $_post['CECredits'];

Open in new window

*****************form (Create Entry form_)********************************
<form action="http://www.biotone.com/insert.php" method="post" class="ohregister" style="padding:10px 0 0 15px; color:#b61351;">
        <fieldset style="width:400px; border:1px solid #b61351;">
          <legend style="font-size:16px; color:#b61351;"><b>SIGN UP NOW</b> </legend>
          <p>
            <label style="text-align:right; width:150px; float:left;">First Name: </label>
            <input type="text" name="FirstName" style="width:200px;" />
          </p>
          <p>
            <label style="text-align:right; width:150px; float:left;">Last Name: </label>
            <input type="text" name="LastName" style="width:200px;" />
          </p>
          <p>
            <label style="text-align:right; width:150px; float:left;">Email: </label>
            <input type="text" name="EmailAddress" style="width:200px;" />
          </p>
          <p>
            <label style="text-align:right; width:150px; float:left;">Phone: </label>
            <input type="text" name="PhoneNumber" style="width:200px;" />
          </p>
          <p>
            <label style="text-align:right; width:190px; float:left;">CE Credits? (Yes)  *:</label>
            <input type="Checkbox" name="CECredits" style="width:180px;" id="CECredits" />

Open in new window

0
Comment
Question by:Scott Johnston
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 21

Expert Comment

by:Kim Walker
ID: 40013535
You should give your checkbox a value to pass to the database.
<input type="checkbox" name="CECredits" style="width:180px;" id="CECredits" value="some value" />

Open in new window

0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40013538
If the checkbox is not checked, it will Not be posted.  That's the way checkboxes work.  This is the way I handle all the $_POST data.  This way, even when it is not checked, it has a default value and you won't get the 'missing index' error.
if (!isset($_POST['CECredits'])){$CECredits = "";} else {$CECredits = $_POST['CECredits'];}

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40013546
If you're new to PHP and want some good learning resources, this article will help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Checkboxes are unique in HTML forms input, in that if they are not checked, they do not appear in the request variables.  This article explains how it works with some examples.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_5450-Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html

If you read that over and still have questions, please post back and I'll show you some examples (however most examples that are good for teaching the principles will simply duplicate the examples in the article).
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40013555
Kind of a sidebar note, but you might want to fix this before you release the web site into the wild.  Google does not like to index sites with invalid markup!
http://validator.w3.org/check?uri=http%3A%2F%2Fwww.biotone.com%2F&charset=%28detect+automatically%29&doctype=Inline&group=0
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:Scott Johnston
ID: 40013592
My friends, thank you for your all your comments but it is not  resolving the problem.  The value always comes up "null" in my sql.
 I must have the field incorrectly setup.

Varchar, 1 length,  Default is Null, NULL is YES
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40013760
The default value for a checkbox is 'on' which is 2 characters, not 1, so the value won't fit in the field so it won't be inserted.  You could use value="Y" to make it one character.
<input type="Checkbox" name="CECredits" style="width:180px;" id="CECredits" value="Y" />

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40014559
Try defining the SQL column this way:

CECredits VARCHAR(1) NOT NULL DEFAULT ''

Open in new window

Then you can test for an empty() string from the data base to indicate "not checked" and any condition other than empty() indicates "checked."  You can find rows with checked values in a query like this:

SELECT things FROM tables WHERE CECredits <> ''

Open in new window

You can find rows with unchecked values with a query like this:

SELECT things FROM tables WHERE CECredits = ''

Open in new window

If you want to set up your HTML documents to provide a default value for the CDCredits input, read the article about checkboxes for understanding, paying particular attention to the paragraph with "How do I get a default value for a checkbox?"  It teaches how to use type=hidden form controls to make your life easier.
0
 

Author Comment

by:Scott Johnston
ID: 40028508
Sorry, I still working on this project and have not been able to confirm or deny results...I should be able review all comments tomorrow.
Scott
0
 

Author Closing Comment

by:Scott Johnston
ID: 40059845
Tis fixed my situation. I needed to setup the field with 2 characters.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now