-3

Update: This is the result I get after query execution:

NewiTem9,desc,video9.swf,9,0,0,1,0,550,500,item9.jpg,0,swf,prev,0,00,0,2013-04-29 03:23:43,1,0,0,0,newitem9,0

For the HTML I have the following checkbox:

<input type="checkbox" name="starter" value="1" id="starter0">

When inserting this into mysql with other elements (with other elements i don't have any problem, they are inserted without a problem and then displaying properly)

mysql_query("
    INSERT INTO datatab (
        name, description, url, category_id, category_parent, width, height,
        image, activado, filetype, instructions, date_added, show_ads, rmode,
        power_id, starter, seo_url, submitter
    )
    VALUES (
        '".escape($_POST['name'])."', '".escape($_POST['description'])."',
        '$url', $_POST[category], $category[parent_id], '$_POST[width]',
        '$_POST[height]', '$img', $_POST[activado], '$ext',
        '".escape($_POST['instructions'])."', '$date', $_POST[show_ads],
        $_POST[rmode], '$_POST[power_id]', '$_POST[starter]', '$seo_url',
        '$_POST[submitter]')
    ") or die ('There was a MySql error when adding the game: '.mysql_error());

Then the data is added but without the mentioned checkbox value.

Database table settings:

Name: starter
Type: TinyINT
Lenght/Values: 1
default: As defined : 0

I don't know why the data of the checkbox is not inserted when all other data are inserted and working?

Could someone help me with this?

Thanks

  • 4
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – h2ooooooo Apr 29 '13 at 06:17
  • What is the error it showing? – Yogesh Suthar Apr 29 '13 at 06:17
  • What is the exact query being executed? (without variables) – Jocelyn Apr 29 '13 at 06:20
  • It gives me no error. The error only appear if I put the checkbox value in this way : $_POST[starter] and not '$_POST[starter]' it says There was a MySql error when adding the game. – Temur Pipia Apr 29 '13 at 06:21
  • Click "edit" below the question... – Jocelyn Apr 29 '13 at 06:22
  • possible duplicate of [POST arrays not showing unchecked Checkboxes](http://stackoverflow.com/questions/7988325/post-arrays-not-showing-unchecked-checkboxes) and many others. – Jocelyn Apr 29 '13 at 06:39
  • Hey dear Jocelyn, I have no arrays so please, do not rate it like negative thread. I have been checking this issue before posting here. Instead of helping you're giving false information! Where do you see an array? – Temur Pipia Apr 29 '13 at 06:43
  • @TemurPipia $_POST is an array and you are using it everywhere in the code you posted. – Jocelyn May 05 '13 at 08:24

4 Answers4

1

If a checkbox is unchecked, then absolutely nothing is sent to the server. Not even a value of blank or 0. An unchecked checkbox is as if the form element doesn't exist.

vcardillo
  • 1,646
  • 3
  • 24
  • 29
0
if (isset($_POST['starter']))
$starter = 1;
else
$starter = 0;

then save $starter in your DB :

mysql_query("INSERT INTO {table name}(..,starter) VALUES (..,'".$starter."');
Mohammad Mehdi Habibi
  • 1,601
  • 1
  • 14
  • 30
0

A nifty trick you can use with checkboxes is to provide the unchecked value in a hidden input prior to the checkbox, eg

<input type="hidden" name="starter" value="0">
<input type="checkbox" name="starter" value="1" id="starter0">

This way, if the checkbox remains unchecked, at least a key / value pair will be submitted with the form.

On to your query, this is just a terrible mix of string concatenation and bad sanitising methods.

You should be using MySQLi or PDO these days, eg (PDO)

// Assuming the $pdo variable contains your PDO instance and all $_POST values
// have been verified as existing

$stmt = $pdo->prepare('
    INSERT INTO datatab (
        name, description, url, category_id, category_parent, width, height,
        image, activado, filetype, instructions, date_added, show_ads, rmode,
        power_id, starter, seo_url, submitter
    ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
');

$stmt->bindParam(1, $_POST['name']);
$stmt->bindParam(2, $_POST['description']);
// etc
$stmt->bindParam(16, $_POST['starter']);
$stmt->bindParam(17, $seo_url);
$stmt->bindParam(18, $_POST['submitted']);

$stmt->execute();
Phil
  • 157,677
  • 23
  • 242
  • 245
  • Of course this would work, but it seems like the "easy but not the best way out of it". There could be a whole lot of extra html-elements (input-fields) which decreases speed of the web-page. I also think you only should use hidden input fields when absolutely necessary. – bestprogrammerintheworld Apr 29 '13 at 06:31
  • @bestprogrammerintheworld Show me some benchmarks on how hidden inputs (1 per checkbox) slow down a page and I might believe you – Phil Apr 29 '13 at 06:38
  • I thought there were more checkboxes involved but in this case when only one checkbox is involved of course it wouldn't matter. The more info in html of course it will slow things down, but I don't know much it will slow down things though. It may also be relevant when targeting users that have smartphones. – bestprogrammerintheworld Apr 29 '13 at 06:47
0

Checkboxes are somewhat special. They only get posted IF they are checked.

If you have foollowing HTML:

<input type="checkbox" name="starter" value="1" id="starter0">

and a user only check the checkbox, then result of post would be like

$_POST['starter'] = 1

You have to check whether or not a checkbox is sent and after that do the correct insert.

If you have it checked and it still does not work, it is an issue with actual query even if you don't get an explicit error. MySQL is somewhat forgiving when inserting things with "wrong" datatype.

UPDATE: You can try this:

if (isset($_POST['starter']) && $_POST['starter'] == 1) {
    $starter = 1;
}
else {
    $starter = 0;
}

mysql_query("
    INSERT INTO datatab (
        name, description, url, category_id, category_parent, width, height,
        image, activado, filetype, instructions, date_added, show_ads, rmode,
        power_id, starter, seo_url, submitter
    )
    VALUES (
        '".escape($_POST['name'])."', '".escape($_POST['description'])."',
        '$url', $_POST[category], $category[parent_id], '$_POST[width]',
        '$_POST[height]', '$img', $_POST[activado], '$ext',
        '".escape($_POST['instructions'])."', '$date', $_POST[show_ads],
        $_POST[rmode], '$_POST[power_id]', $starter, '$seo_url',
        '$_POST[submitter]')
    ") or die ('There was a MySql error when adding the game: '.mysql_error());
bestprogrammerintheworld
  • 5,417
  • 7
  • 43
  • 72