Jump to content


Photo

Checkboxs


  • Please log in to reply
3 replies to this topic

#1 Melon Fresh

Melon Fresh
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 August 2006 - 02:43 PM

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)

     <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

set('sims2', 'university', 'christmass', 'nightlife', 'ofb', 'familyfunstuff', 'glamoursims')

Could any one help me please  :)

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 27 August 2006 - 03:10 PM

<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

<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

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

#3 Melon Fresh

Melon Fresh
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 August 2006 - 03:56 PM

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
<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
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

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 27 August 2006 - 04:38 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:

<?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')";
?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users