Author Topic: Checkboxs  (Read 426 times)

0 Members and 1 Guest are viewing this topic.

Offline Melon FreshTopic starter

  • Irregular
  • Posts: 7
    • View Profile
Checkboxs
« on: August 27, 2006, 10:43:50 AM »
Hi I know, a bit about php and mysql but I am having problems putting text boxes into a mysql table.

Here is the code for the checkboxes (I don't know if that is right)

Code: [Select]
     <tr>
        <td>&nbsp;Expansions:</td>
        <td><input type="checkbox" id="expansions[]" value="sims2," />
Sims 2
  <input type="checkbox" id="expansions[]" value="university," />
University
 
<input type="checkbox" id="expansions[]" value="nightlife," />
Nightlife
<input type="checkbox" id="expansions[]" value="ofb," />
Open For Business</td>
      </tr>
      <tr>
        <td>Extra Packs </td>
        <td><input type="checkbox" id="expansions[]" value="christmass," />
Christmas
  <input type="checkbox" id="expansions[]" value="familyfunstuff," />
Family Fun Stuff
<input type="checkbox" id="expansions[]" value="glamoursims," />
Glamour Life</td>
      </tr>

and here is the sql insert


$addsims2 
"insert into web_sims2 values ('','$_POST[name]','$_POST[date]','$_POST[description]','$_POST[download]','$_POST[custom_content]','(This is where the check box data needs to go) ','$_POST[type]')";
    
mysql_query($addsims2$ServerConnect) or die(mysql_error()); 


The sql row type is

Code: [Select]
set('sims2', 'university', 'christmass', 'nightlife', 'ofb', 'familyfunstuff', 'glamoursims')

Could any one help me please  :)
« Last Edit: August 27, 2006, 10:52:26 AM by Melon Fresh »

Offline shocker-z

  • Devotee
  • Posts: 1,257
    • View Profile
Re: Checkboxs
« Reply #1 on: August 27, 2006, 11:10:56 AM »
Code: [Select]
<tr>
        <td>&nbsp;Expansions:</td>
        <td><input type="checkbox" id="expansions[]" value="sims2," />
Sims 2
  <input type="checkbox" id="expansions[]" value="university," />
University
 
<input type="checkbox" id="expansions[]" value="nightlife," />
Nightlife
<input type="checkbox" id="expansions[]" value="ofb," />
Open For Business</td>
      </tr>
      <tr>
        <td>Extra Packs </td>
        <td><input type="checkbox" id="expansions[]" value="christmass," />
Christmas
  <input type="checkbox" id="expansions[]" value="familyfunstuff," />
Family Fun Stuff
<input type="checkbox" id="expansions[]" value="glamoursims," />
Glamour Life</td>
      </tr>

needs to be

Code: [Select]
<tr>
        <td>&nbsp;Expansions:</td>
        <td><input type="checkbox" name="expansions[]" value="sims2," />
Sims 2
  <input type="checkbox" name="expansions[]" value="university," />
University
 
<input type="checkbox" name="expansions[]" value="nightlife," />
Nightlife
<input type="checkbox" name="expansions[]" value="ofb," />
Open For Business</td>
      </tr>
      <tr>
        <td>Extra Packs </td>
        <td><input type="checkbox" name="expansions[]" value="christmass," />
Christmas
  <input type="checkbox" name="expansions[]" value="familyfunstuff," />
Family Fun Stuff
<input type="checkbox" name="expansions[]" value="glamoursims," />
Glamour Life</td>
      </tr>

and

Code: [Select]
foreach ($_POST['expansions'] as $expan) {
$expansions .= $expan.' ';
}
$expansions=trim($expansions);
$expansions=str_replace(' ',', ', $expansions);

$addsims2 = "insert into web_sims2 values ('','$_POST[name]','$_POST[date]','$_POST[description]','$_POST[download]','$_POST[custom_content]','$expansions','$_POST[type]')";
    mysql_query($addsims2, $ServerConnect) or die(mysql_error());


that should do the job of comma seperating all the expansions when clicked..

Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

Offline Melon FreshTopic starter

  • Irregular
  • Posts: 7
    • View Profile
Re: Checkboxs
« Reply #2 on: August 27, 2006, 11:56:31 AM »
It does not seem to work, I made a new table to try and test it but nothing, so here is the whole code and the sql from mysql


Here is the code for the page test.php
Code: [Select]
<center><form action="" method="post" name="blogform" id="blogform" onsubmit="return checkForm();">
  <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#000000">
  <tr>
    <td bgcolor="#E0E9F2">
<table width="100%" border="0" cellspacing="0" cellpadding="2">
      <tr>
        <td width="10%">&nbsp;Answer: </td>
        <td width="90%"><input name="for" type="radio" value="yes" checked="checked"/>
         Yes
          <input name="for" type="radio" value="no" />
          No</td>
      </tr>
      <tr>
        <td>&nbsp;Name:</td>
        <td><input type="textfield" name="name" /></td>
      </tr>
    <tr>
        <td>&nbsp;tests:</td>
        <td><input type="checkbox" name="test[]" value="sims2" />
Sims 2
  <input type="checkbox" name="test[]" value="sims3" />
Sims 3
 
<input type="checkbox" name="test[]" value="sims4" />
Sims 4
</td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td bgcolor="#E0E9F2"><p><input name="checkp" type="submit" value="Add check" action".$_SERVER['REQUEST_URI']"/>
      <p></td>
  </tr>
</form></center>


	
if(isset(
$_POST['checkp'])) 

foreach (
$_POST['test'] as $expan) {
$test33 .= $expan.' ';
}
$test33 trim($test33);
$test33 str_replace(' ',', '$test33);


	
$addtest "insert into test values ('','$_POST[name]','$test33','$_POST[for]')";
    
mysql_query($addtest$ServerConnect) or die(mysql_error()); 
	

	



Here is the sql
Code: [Select]
CREATE TABLE `test` (
  `tid` int(11) NOT NULL auto_increment,
  `name` varchar(150) default NULL,
  `test` set('sims2','sims3','sims4') default NULL,
  `for` enum('yes','no') default NULL,
  PRIMARY KEY  (`tid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


I hope this helps more

Offline .josh

  • Administrator
  • 'Insane!'
  • *
  • Posts: 13,150
  • Grumpy Old Man
    • View Profile
Re: Checkboxs
« Reply #3 on: August 27, 2006, 12:38:16 PM »
your sql syntax should look like this:

insert into tablename (column1,column2,column3) values ('value1','value2','value3')

also you should not insert data from the post array directly into your query like you're doing. that's begging for sql injection; a huge security risk.  you should do something like this instead:

Code: [Select]
<?php
function clean_var($value){
   if (
get_magic_quotes_gpc()) { stripslashes($value); }
   if (!
is_numeric($value)) { mysql_real_escape_string($value); }    
   return 
$value;
}

$blah1 clean_var($_POST['blah1']);
$blah2 clean_var($_POST['blah2']);
$blah3 clean_var($_POST['blah3']);
//or you could make a loop to cycle through $_POST

$query "insert into tablename (column1,column2,column3) values ('$blah1','$blah2','$blah3')";
?>

« Last Edit: August 27, 2006, 12:40:24 PM by Crayon Violent »

Did I help you? Feeling generous? Donate to me! | Donate to phpfreaks!