Solved

web page - Check Box - How to update Mysql Data

Posted on 2014-04-21
10
763 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 22

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 83

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 109

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 109

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
 

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 83

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 109

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Exporting image to excel from php document 10 26
Web forms being filled out by bots 4 31
AWS EC2 & RDS Instance 5 34
Presenting past 6 months bug 7 25
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

815 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

8 Experts available now in Live!

Get 1:1 Help Now