Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Inserting data from a check box into a form
Old 09-23-2008, 08:20 AM Inserting data from a check box into a form
Novice Talker

Posts: 13
Location: UK
Trades: 0
Hi,

I am inserting some data from my checkbox, based on selection, which 1 is selected and 0 is not, both get sent to the database. But the code seems to give a successful response, but that column named 'Priority' does not yield and results and gives a blank row. This code also inserts data from a text area into the database fine and data does appear. The column in the table description called 'priority' had type varchar - it didn't work, and now 'int' and still does not work, any ideas what is wrong with this code?

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="my_db"; // Database name
$tbl_name="description"; // Table name
// Connect to server and select database.
mysql_connect('localhost', 'root')or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

if (priority != NULL) {
$priority = 1;
}
else {
$priority = 0;
}

// Get values from form
$comments=$_POST['comments'];
$priority=$_POST['priority'];

// Insert data into mysql
$sql="INSERT INTO $tbl_name(Issue, Priority)VALUES('$comments', '$priority')";
$result=mysql_query($sql);

// successfully insert data into database
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='loginsuccess.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
patelh5 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-23-2008, 08:45 AM Re: Inserting data from a check box into a form
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
actually an unchecked checkbox will be empty when submitted

and in your code you set a value for $priority, then promptly overwrite it

AND you need to learn how to sanitise form/querystring data, otherwise you open your database to injection attacks.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-23-2008, 08:49 AM Re: Inserting data from a check box into a form
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
This should work.

If in doubt you should echo out your variables.

You can use print_r to echo out the contents of the POST array
print_r($_POST);

PHP Code:
<?php
$host
="localhost"// Host name 
$username=""// Mysql username 
$password=""// Mysql password 
$db_name="my_db"// Database name 
$tbl_name="description"// Table name 
// Connect to server and select database.
mysql_connect('localhost''root')or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form 
 
$comments=$_POST['comments'];
 
$priority=$_POST['priority'];
 
if (
$priority != NULL) { 
$priority 1

else { 
$priority 0
}  
 

 
// Insert data into mysql 
$sql="INSERT INTO $tbl_name(Issue, Priority)VALUES('$comments', '$priority')";
$result=mysql_query($sql);
 
// successfully insert data into database 
if($result){
echo 
"Successful";
echo 
"<BR>";
echo 
"<a href='loginsuccess.php'>Back to main page</a>";
}
else {
echo 
"ERROR";
}
?>
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 09-23-2008, 01:49 PM Re: Inserting data from a check box into a database
Novice Talker

Posts: 13
Location: UK
Trades: 0
Hey,

Thanks to you, I forgot to put in $ sign in front of priority, thanks soo much!!! I have been working on it all day! and I just checked this now!!!
patelh5 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Inserting data from a check box into a form
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13786 seconds with 12 queries