Jump to content

[SOLVED] Problems writing checkboxes to DB in php


mkalinow

Recommended Posts

So, I've been having an issue setting up checkboxes on a supplier database insert form.  A user has the ability to add a new entity into the database (Company name, address, etc.) via textboxes which work just fine.  However, when I try to get the checkboxes to write to the mySQL database along with the textboxes, I begin getting errors.  I've gotten to the point in the code where either one or the other works, but not both at the same time.  I was searching around and saw that someone mentioned isset as a solution to their checkbox problem but thus far, it hasn't helped too much :( 

 

I'm new to this language so if you guys need any more additional information, please let me know.

 

<?php

$host="localhost"; // Host name
$username="root"; // Mysql usernamer
$password="pw"; // Mysql password
$db_name="movedb"; // Database name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$Company=$_POST['company'];
$City=$_POST['city'];
$State=$_POST['state'];
$Country=$_POST['country'];
$Address1=$_POST['address1'];
$Address2=$_POST['address2'];
$Zip=$_POST['zip'];
$Division=$_POST['division'];
$YearEstablished=$_POST['yearestablished'];
$Homepage=$_POST['homepage'];
$Qaemployees=$_POST['qaemployees'];
$Numberofemployees=$_POST['numberofemployees'];
$Mainnumber=$_POST['mainnumber'];
$Contactname=$_POST['contactname'];
$Telephone=$_POST['telephone'];
$Mobile=$_POST['mobile'];
$Contactposition=$_POST['contactposition'];
$Emailaddy=$_POST['emailaddy'];
$Faxnumber=$_POST['faxnumber'];
$Comments=$_POST['comments'];

// Insert data into mysql
[b]$sql="INSERT INTO supplier_data(company, city, state, country, address1, address2, zip, division, yearestablished, homepage, qaemployees, numberofemployees, mainnumber, contactname, telephone, mobile, contactposition, emailaddy, faxnumber, comments)VALUES('$Company', '$City', '$State', '$Country', '$Address1', '$Address2', '$Zip', '$Division', '$YearEstablished', '$Homepage', '$Qaemployees', '$Numberofemployees', '$Mainnumber', '$Contactname', '$Telephone', '$Mobile', '$Contactposition', '$Emailaddy', '$Faxnumber', '$Comments')";[/b]

if(isset($_POST['headquarters']))
{
    $Headquarters = implode(",",$_POST['headquarters']);
    echo $Headquarters;
    [b]$sql="INSERT INTO supplier_data(headquarters)VALUES('$Headquarters')";[/b]
}   

echo $sql;
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='add.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

// close connection
mysql_close();?>

 

I've bolded the two $sql statements I'm having trouble with and headquarters, by the way, is the aforementioned checkbox.  I couldn't simply place it in the first INSERT statement along with all of the other variables so there I am.  With the code above, it simply writes a 1 or 0 to the db depending on whether or not the box was ticked.  If I comment it out, all of the textboxes now work.  I figure it's because the second $sql for headquarters is just overwriting the first $sql variable, which I understand.  However, I don't understand these PHP conventions because when I rename one of the $sql variables to $sql1 or anything else, I begin receiving errors.

 

Hopefully this wasn't too drawn out but let me say that I appreciate you guys taking a look at this either way :]

Link to comment
Share on other sites

Also if this site is going to be publicly available you need to read about SQL injection attacks.

 

It's an internal site so I'm not too concerned but for best practices, I'll be reading up on it soon.

 

i think i may see what your problem is, but im not certain. what errors are you getting? and can you paste the code for the check boxes

 

Well, to make this simple, I'll try to document it in full.

 

$Faxnumber=$_POST['faxnumber'];
$Comments=$_POST['comments'];
[b]$Headquarters=$_POST['headquarters'];[/b]

// Insert data into mysql
$sql="INSERT INTO supplier_data(company, city, state, country, address1, address2, zip, division, yearestablished, homepage, qaemployees, numberofemployees, 

mainnumber, contactname, telephone, mobile, contactposition, emailaddy, faxnumber, comments, [b]headquarters[/b])VALUES('$Company', '$City', '$State', '$Country', 

'$Address1', 

'$Address2', '$Zip', '$Division', '$YearEstablished', '$Homepage', '$Qaemployees', '$Numberofemployees', '$Mainnumber', '$Contactname', '$Telephone', 

'$Mobile', '$Contactposition', '$Emailaddy', '$Faxnumber', '$Comments', [b]'$Headquarters'[/b])";

 

When I add $headquarters back into my first insert statement, I get the following error:

 

INSERT INTO supplier_data(company, city, state, country, address1, address2, zip, division, yearestablished, homepage, qaemployees, numberofemployees, mainnumber, contactname, telephone, mobile, contactposition, emailaddy, faxnumber, comments, headquarters)VALUES('Test', '', '', '', '', '', '', '', '', 'http://', 'unk', 'unk', '', '', '', '', '', '', '', '', 'Array')ERROR

 

On the form page, headquarters has a value of "headquarters[]."  If I change the value to just simply "headquarters," I get:

 

INSERT INTO supplier_data(company, city, state, country, address1, address2, zip, division, yearestablished, homepage, qaemployees, numberofemployees, mainnumber, contactname, telephone, mobile, contactposition, emailaddy, faxnumber, comments, headquarters)VALUES('w', '', '', '', '', '', '', '', '', 'http://', 'unk', 'unk', '', '', '', '', '', '', '', '', '')ERROR

 

 

Link to comment
Share on other sites

if you use headquarters[] you will get the error because it is an array. you have to get the array to a basic string to insert it.

 

What does your form look like??

Are you just trying to insert numbers or strings for the check boxes??

 

Ray

Link to comment
Share on other sites

if you are using an array in the form (headquaters[]) then look into using foreach to retrieve all of the values.

 

Well, it's a single checkbox but I was trying to rig it so it'd work.  What would you recommend if I get rid of the array?

 

if you use headquarters[] you will get the error because it is an array. you have to get the array to a basic string to insert it.

 

What does your form look like??

Are you just trying to insert numbers or strings for the check boxes??

 

Ray

 

Form.php

<tr><td><form name="form2" method="post" action="http://localhost/scm/new-design/insert_com.php"></td><td> </td></tr>

<tr><td><input type="checkbox" name="headquarters" value="1">Headquarters</td>

 

It just enters a 1 into the DB if the record entry serves as headquarters (ticked) to a company and 0 if it doesn't (unticked).

 

When this was all started, I wasn't using an array and sort of sifted around online and this is where I'm sitting at currently.  As I said earlier, either the textareas will write to the DB or the checkboxes will, but not both.  Is it still necessary to convert the data?

 

Link to comment
Share on other sites

With checkboxes you have to be careful. If you do not check them they will not be passed. You will get an error when trying to use the value if it is not checked.

 

2 things you can do.

 

1.) use php to set value

$Headquarters = isset($_POST['headquarters']) ? $_POST['headquarters'] : "0";

then use the query

// Insert data into mysql
$sql="INSERT INTO supplier_data(company, city, state, country, address1, address2, zip, division, yearestablished, homepage, qaemployees, numberofemployees, 
        mainnumber, contactname, telephone, mobile, contactposition, emailaddy, faxnumber, comments, headquarters) 
        VALUES('$Company', '$City', '$State', '$Country', '$Address1', '$Address2', '$Zip', '$Division', '$YearEstablished', '$Homepage', '$Qaemployees', 
        '$Numberofemployees', '$Mainnumber', '$Contactname', '$Telephone', '$Mobile', '$Contactposition', '$Emailaddy', '$Faxnumber', '$Comments', '$Headquarters')";

 

2.) Set the default value for the field to "0" in MySQL, and do not try to insert a value for it in your query and it will be set to 0 when a new row is inserted.

the use query

// Insert data into mysql
if(!isset($_POST['headquarters'])){
$sql="INSERT INTO supplier_data(company, city, state, country, address1, address2, zip, division, yearestablished, homepage, qaemployees, numberofemployees, 
        mainnumber, contactname, telephone, mobile, contactposition, emailaddy, faxnumber, comments) 
        VALUES('$Company', '$City', '$State', '$Country', '$Address1', '$Address2', '$Zip', '$Division', '$YearEstablished', '$Homepage', '$Qaemployees', 
        '$Numberofemployees', '$Mainnumber', '$Contactname', '$Telephone', '$Mobile', '$Contactposition', '$Emailaddy', '$Faxnumber', '$Comments')";
} else {
$sql="INSERT INTO supplier_data(company, city, state, country, address1, address2, zip, division, yearestablished, homepage, qaemployees, numberofemployees, 
        mainnumber, contactname, telephone, mobile, contactposition, emailaddy, faxnumber, comments, headquarters) 
        VALUES('$Company', '$City', '$State', '$Country', '$Address1', '$Address2', '$Zip', '$Division', '$YearEstablished', '$Homepage', '$Qaemployees', 
        '$Numberofemployees', '$Mainnumber', '$Contactname', '$Telephone', '$Mobile', '$Contactposition', '$Emailaddy', '$Faxnumber', '$Comments', '$Headquarters')";
}

 

Personally I would use the 1st option, otherwise you will have to use an additional check to see if the value is set anyway.

 

Ray

Link to comment
Share on other sites

With checkboxes you have to be careful. If you do not check them they will not be passed. You will get an error when trying to use the value if it is not checked.

 

2 things you can do.

 

1.) use php to set value

$Headquarters = isset($_POST['headquarters']) ? $_POST['headquarters'] : "0";

then use the query

// Insert data into mysql
$sql="INSERT INTO supplier_data(company, city, state, country, address1, address2, zip, division, yearestablished, homepage, qaemployees, numberofemployees, 
        mainnumber, contactname, telephone, mobile, contactposition, emailaddy, faxnumber, comments, headquarters) 
        VALUES('$Company', '$City', '$State', '$Country', '$Address1', '$Address2', '$Zip', '$Division', '$YearEstablished', '$Homepage', '$Qaemployees', 
        '$Numberofemployees', '$Mainnumber', '$Contactname', '$Telephone', '$Mobile', '$Contactposition', '$Emailaddy', '$Faxnumber', '$Comments', '$Headquarters')";

 

2.) Set the default value for the field to "0" in MySQL, and do not try to insert a value for it in your query and it will be set to 0 when a new row is inserted.

the use query

// Insert data into mysql
if(!isset($_POST['headquarters'])){
$sql="INSERT INTO supplier_data(company, city, state, country, address1, address2, zip, division, yearestablished, homepage, qaemployees, numberofemployees, 
        mainnumber, contactname, telephone, mobile, contactposition, emailaddy, faxnumber, comments) 
        VALUES('$Company', '$City', '$State', '$Country', '$Address1', '$Address2', '$Zip', '$Division', '$YearEstablished', '$Homepage', '$Qaemployees', 
        '$Numberofemployees', '$Mainnumber', '$Contactname', '$Telephone', '$Mobile', '$Contactposition', '$Emailaddy', '$Faxnumber', '$Comments')";
} else {
$sql="INSERT INTO supplier_data(company, city, state, country, address1, address2, zip, division, yearestablished, homepage, qaemployees, numberofemployees, 
        mainnumber, contactname, telephone, mobile, contactposition, emailaddy, faxnumber, comments, headquarters) 
        VALUES('$Company', '$City', '$State', '$Country', '$Address1', '$Address2', '$Zip', '$Division', '$YearEstablished', '$Homepage', '$Qaemployees', 
        '$Numberofemployees', '$Mainnumber', '$Contactname', '$Telephone', '$Mobile', '$Contactposition', '$Emailaddy', '$Faxnumber', '$Comments', '$Headquarters')";
}

 

Personally I would use the 1st option, otherwise you will have to use an additional check to see if the value is set anyway.

 

Ray

 

So after a solid day of trying to figure this out, I stuck my headquarters variable at the top of the list and at the end of the $sql query and it worked and posted textarea as well :D  You're right, though.  Just saw that if it remains unchecked, it errors out.  I'm sure you've saved me from dealing with another pain in the ass so I appreciate it. 

 

Code below if you're curious

 

<?php

$host="localhost"; // Host name
$username="root"; // Mysql usernamer
$password="pw"; // Mysql password
$db_name="movedb"; // Database name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$Headquarters=$_POST['headquarters'];
$Company=$_POST['company'];
$City=$_POST['city'];
$State=$_POST['state'];
$Country=$_POST['country'];
$Address1=$_POST['address1'];
$Address2=$_POST['address2'];
$Zip=$_POST['zip'];
$Division=$_POST['division'];
$YearEstablished=$_POST['yearestablished'];
$Homepage=$_POST['homepage'];
$Qaemployees=$_POST['qaemployees'];
$Numberofemployees=$_POST['numberofemployees'];
$Mainnumber=$_POST['mainnumber'];
$Contactname=$_POST['contactname'];
$Telephone=$_POST['telephone'];
$Mobile=$_POST['mobile'];
$Contactposition=$_POST['contactposition'];
$Emailaddy=$_POST['emailaddy'];
$Faxnumber=$_POST['faxnumber'];
$Comments=$_POST['comments'];

// Insert data into mysql
$sql="INSERT INTO supplier_data(company, city, state, country, address1, address2, zip, division, yearestablished, homepage, qaemployees, numberofemployees, mainnumber, contactname, telephone, mobile, contactposition, emailaddy, faxnumber, comments, headquarters)VALUES('$Company', '$City', '$State', '$Country', '$Address1', '$Address2', '$Zip', '$Division', '$YearEstablished', '$Homepage', '$Qaemployees', '$Numberofemployees', '$Mainnumber', '$Contactname', '$Telephone', '$Mobile', '$Contactposition', '$Emailaddy', '$Faxnumber', '$Comments', '$Headquarters')";

echo $sql;
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='add.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

// close connection
mysql_close();?>

 

Edit: Hit enter too quickly.  The Headquarters checkbox is the first field in the table and if I had to guess, I have to enter all of these variables in the order that they're coded on the form.  In other words, headquarters is the first identifier, company name is the second but in the post PHP, company name came before headquarters.  Am I off-base with this understanding?

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.