Jump to content

Checkboxs


Melon Fresh

Recommended Posts

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]
    <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>
[/code]

and here is the sql insert

[code=php:0]
$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());
[/code]

The sql row type is

[code]
set('sims2', 'university', 'christmass', 'nightlife', 'ofb', 'familyfunstuff', 'glamoursims')
[/code]

Could any one help me please  :)
Link to comment
Share on other sites

[code]<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>
[/code]

needs to be

[code]<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>
[/code]

and

[code]
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());
[/code]


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

Regards
Liam
Link to comment
Share on other sites

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]
<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>
[/code]

[code=php:0]
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());

}
[/code]

Here is the sql
[code]
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;
[/code]


I hope this helps more
Link to comment
Share on other sites

your sql syntax should look like this:

[b][color=red]insert into tablename (column1,column2,column3) values ('value1','value2','value3')[/color][/b]

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]
<?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')";
?>
[/code]
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.