Jump to content

Archived

This topic is now archived and is closed to further replies.

Melon Fresh

Checkboxs

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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.