Jump to content
Sign in to follow this  
cocolino

Checkbox problem

Recommended Posts

Hello, i have the following problem with checkbox option...
 
Wen i have selected one or more checkboxes and update a page the checkbox return same values doubled, tripled or how many time is page updated.
 
I want to resolve this error. Can anybody help me?
 
Here is the 2 files, file.php and template.php
 
 
the file.php code
 

<?php

elseif ($action == 'edit_category') {
    $category_id = (int) $_GET['categ'];
    if (isset($_POST['submit_edit_category'])) {
    $edit_category_query = mysql_query("UPDATE categories SET name = '". $_POST['name'] ."', description = '". $_POST['description'] ."' WHERE catid = '". $category_id ."'");


// categories sidebar   
$delete_category_query = mysql_query("delete FROM map_category WHERE catid2 = '". $category_id ."'");

if (empty($_POST['check_list'])) {echo '';}
if (is_array($_POST['check_list']))
{
foreach ($_POST['check_list'] as $selected) {         
$insert_category_query = mysql_query("INSERT INTO map_category(catid1, catid2) VALUES ('". $category_id ."','". $selected ."')");
}
}

// categories footer    
$delete_category_query = mysql_query("delete FROM map_category_footer WHERE catid2 = '". $category_id ."'");

if (empty($_POST['check_list_footer'])) {echo '';}
if (is_array($_POST['check_list_footer']))
{
foreach ($_POST['check_list_footer'] as $selected) {          
$insert_category_query = mysql_query("INSERT INTO map_category_footer(catid1, catid2) VALUES ('". $category_id ."','". $selected ."')");
}
}

    redirect_page('/admin/file.php?select=edit_category&categ='. $category_id);
    }

    $category_query = mysql_query("SELECT * FROM categories WHERE catid = '". $category_id ."' LIMIT 1");   
    $category_row = mysql_fetch_assoc($category_query);
    $edit_category = array (
        'name'          =>  $category_row['name'],  
        'description'   =>  $category_row['description']
    );

// select categories sidebar
    $top_category_query = mysql_query("SELECT c.catid, c.name, (CASE WHEN mc.catid1 IS NULL THEN 0 ELSE 1 END) AS is_selected
                                    FROM categories c
                                    LEFT JOIN map_category mc 
                                        ON c.catid = mc.catid2
                                        and mc.catid1 = '". $category_id ."' 
                                    WHERE c.catid <> '". $category_id ."' 
                                    ORDER BY c.name ASC "); 

    while($top_category_row = mysql_fetch_assoc($top_category_query))
        {
            $top_edit_category[] = $top_category_row;
        }   


// select categories footer     
    $top_category_footer_query = mysql_query("SELECT c.catid, c.name, (CASE WHEN mc.catid1 IS NULL THEN 0 ELSE 1 END) AS is_selected
                                    FROM categories c
                                    LEFT JOIN map_category_footer mc 
                                        ON c.catid = mc.catid2
                                        and mc.catid1 = '". $category_id ."' 
                                    WHERE c.catid <> '". $category_id ."' 
                                    ORDER BY c.name ASC "); 

    while($top_category_footer_row = mysql_fetch_assoc($top_category_footer_query))
        {
            $top_edit_category_footer[] = $top_category_footer_row;
        }       

edit_category_admin();
}

?>

 
 
and template.php code
 

<?php

function edit_category_admin()
    {global $edit_category,$top_edit_category,$top_edit_category_footer;
    main_header();
    main_menu();
    echo'<div id="center"><div id="admin"><h3>Edit category</h3>';
    echo '<form action="" method="POST" name="form" onsubmit="return verify_add()">
          <div class="left">Name</div>
          <div class="right"><input type="text" name="name" size="55" value="'. $edit_category['name'] .'" /></div>
          <div class="clear"></div>   
          <div class="left">Description</div>
          <div class="right"><textarea name="description" rows="5" cols="95">'. $edit_category['description'] .'</textarea></div>
          <div class="clear"></div>
          <div class="left">Sidebar Category:</div>       
          <div class="right"><div id="cat" style="background-color:#fcfebc;width: 100%px;height: 400px;overflow: scroll;">';
foreach ($top_edit_category as $top_cat)
{         
echo '<label><input type="checkbox" name="check_list[]" value="'. $top_cat['catid'] .'"  '. ($top_cat['is_selected'] == '1' ? 'checked' : '') .' /> '. $top_cat['name'] .'</label>';
}   

echo '</div></div>                    
          <div class="clear"></div>
          <div class="left">Footer Category:</div>        
          <div class="right"><div id="cat" style="background-color:#DAF7A6;width: 100%px;height: 200px;overflow: scroll;">';          
foreach ($top_edit_category_footer as $top_cat)
{
echo '<label><input type="checkbox" name="check_list_footer[]" value="'. $top_cat['catid'] .'"  '. ($top_cat['is_selected'] == '1' ? 'checked' : '') .' /> '. $top_cat['name'] .'</label>';
}

echo '</div></div>                    
          <div class="clear"></div>
           <div align="center">
            <input type="submit" name="submit_edit_category" value="Edit category" />
          </div><br />
         </form>'; 
    echo '</div></div>
    <div class="clear"></div>'; 
    main_footer();
    }

?>  

 
Many thanks!

Share this post


Link to post
Share on other sites

I don't want to sound nasty, but duplicate form elements on page refresh is the least of your problems.

 

What have you tried so far to fix it?

Did you piss some code from the start of the files.php because it seems to be starting with an elseif which makes no sense.

Share this post


Link to post
Share on other sites

because that is the code witch make the call of execute... i don't see the sense to post entire code. thanks

Share this post


Link to post
Share on other sites

I don't want to sound nasty, but duplicate form elements on page refresh is the least of your problems.

 

What have you tried so far to fix it?

Did you piss some code from the start of the files.php because it seems to be starting with an elseif which makes no sense.

 

I'm beginner and i hope to get answer from advanced members.

The code from file start with elseif because that is the code witch make the call of execute... i don't see the sense to post entire code.

Share this post


Link to post
Share on other sites

it's not clear what the issue is. what update are you talking about? you have an UPDATE query, but i suspect you are talking about the INSERTed data or are you talking about just refreshing the page?

 

in general, when asking for help, since we are not sitting there with you, you need to show what result you are getting and at what point you are getting it, and either clearly state or show what is wrong with that result, and also show or state what result you expected to get.

Share this post


Link to post
Share on other sites

it's not clear what the issue is. what update are you talking about? you have an UPDATE query, but i suspect you are talking about the INSERTed data or are you talking about just refreshing the page?

 

in general, when asking for help, since we are not sitting there with you, you need to show what result you are getting and at what point you are getting it, and either clearly state or show what is wrong with that result, and also show or state what result you expected to get.

 

Wen i update the page. every time wen i push submit button the checkboxes selected is duplicate.

Here is the printscreen of issue.

abc.jpg

 

Thanks!

Share this post


Link to post
Share on other sites

So looking at the problem logically (because we don't have all the code) The issue happens on page reload/refresh and only effects checkboxes that have been checked.  Therefor the issue is with if(isset(...)) checks (or perhaps a lack there of) and relates to how you render the previously entered form information to the page.

 

I suspect you have some logic that takes the checked boxes and adds them to the form for persistence?  So that whatever get's checked stays checked after the form is submitted or page refreshed?  

 

But what you are doing is rendering the element every time in addition to the previous full form elements. Instead of this you should be using it to alter the existing elements to being "checked".  

 

Chances of seeing the rest of the code?

Share this post


Link to post
Share on other sites

unfortunately the OP didn't actually answer the question that was asked.

 

the problem is most likely because his code/database design allows multiple rows with the same combination of values to be inserted into his database table.

 

your database table needs to enforce uniqueness. you need to define a unique composite index for the (catid1, catid2) columns so that you can only insert one row for any combination of values. you should also give those columns names that indicate their true purpose so that anyone reading the code/query can understand what the values mean.

Share this post


Link to post
Share on other sites

unfortunately the OP didn't actually answer the question that was asked.

 

the problem is most likely because his code/database design allows multiple rows with the same combination of values to be inserted into his database table.

 

your database table needs to enforce uniqueness. you need to define a unique composite index for the (catid1, catid2) columns so that you can only insert one row for any combination of values. you should also give those columns names that indicate their true purpose so that anyone reading the code/query can understand what the values mean.

 

Thanks but i want solutions because i'm beginner and don't know what to do in the code!

Share this post


Link to post
Share on other sites

So looking at the problem logically (because we don't have all the code) The issue happens on page reload/refresh and only effects checkboxes that have been checked.  Therefor the issue is with if(isset(...)) checks (or perhaps a lack there of) and relates to how you render the previously entered form information to the page.

 

I suspect you have some logic that takes the checked boxes and adds them to the form for persistence?  So that whatever get's checked stays checked after the form is submitted or page refreshed?  

 

But what you are doing is rendering the element every time in addition to the previous full form elements. Instead of this you should be using it to alter the existing elements to being "checked".  

 

Chances of seeing the rest of the code?

 

Hi, i understand your opinion but this part of code make a action for that request. Way you want all code?

Share this post


Link to post
Share on other sites

...but this part of code make a action for that request...

Sorry, I don't understand what you're saying here.

 

I don't really want all the code, but it's becoming apparent that the problem most likely lies with a part of the code that you haven't identified.  As such, to allow us to have a chance at finding it we need to have visibility of it all (for example, you have cut the top off the decision logic that applies to the behaviour of the page in relation to the variables in question and don't think that's a problem.  In short, the problem does not lie in the code that you have posted already (at least not solely) so we need to see more.

 

 

 

 

Thanks but i want solutions because i'm beginner and don't know what to do in the code!

 

As a beginner you shouldn't want solutions that you don't understand from other people to fix problems you couldn't properly identify - you should want people to help you to understand the problem and create your own solution to it once you have learned where it went wrong - which is exactly what mac_gyver the other people of this board are going to be: those people who can help you learn and grow. If you just want someone to do it for you you want the freelance board where you can recruit someone to do the task for you.

Share this post


Link to post
Share on other sites

Sorry, I don't understand what you're saying here.

 

I don't really want all the code, but it's becoming apparent that the problem most likely lies with a part of the code that you haven't identified.  As such, to allow us to have a chance at finding it we need to have visibility of it all (for example, you have cut the top off the decision logic that applies to the behaviour of the page in relation to the variables in question and don't think that's a problem.  In short, the problem does not lie in the code that you have posted already (at least not solely) so we need to see more.

 

 

 

 

 

As a beginner you shouldn't want solutions that you don't understand from other people to fix problems you couldn't properly identify - you should want people to help you to understand the problem and create your own solution to it once you have learned where it went wrong - which is exactly what mac_gyver the other people of this board are going to be: those people who can help you learn and grow. If you just want someone to do it for you you want the freelance board where you can recruit someone to do the task for you.

 

Here is all code from file.php

<?php
session_start();
require ('login.php');
if ($user['status'] != '1' || $user['group'] != '2') {
header('Location: '. $s['url'] .'/enter.php');
exit();
}

$action = $_GET['select'];
if ($action == 'categories') 
{
	$categories = array ();
	$categories_query = mysql_query("SELECT * FROM categories ORDER BY name");
	while ($categories_row = mysql_fetch_array($categories_query)) {
		$categories[] = array (
			'id'				=>	$categories_row['catid'],
			'name'				=>	$categories_row['name'],
			'title'				=>	$categories_row['title'],
			'url'				=>	$categories_row['url'],
			'icon'				=>	$categories_row['icon'],
			'description'		=>	$categories_row['description']
		);
	}

  categories_admin();
} 

elseif ($action == 'edit_category') {
    $category_id = (int) $_GET['categ'];
    if (isset($_POST['submit_edit_category'])) {
    $edit_category_query = mysql_query("UPDATE categories SET name = '". $_POST['name'] ."', description = '". $_POST['description'] ."' WHERE catid = '". $category_id ."'");


// categories sidebar   
$delete_category_query = mysql_query("delete FROM map_category WHERE catid2 = '". $category_id ."'");

if (empty($_POST['check_list'])) {echo '';}
if (is_array($_POST['check_list']))
{
foreach ($_POST['check_list'] as $selected) {         
$insert_category_query = mysql_query("INSERT INTO map_category(catid1, catid2) VALUES ('". $category_id ."','". $selected ."')");
}
}

// categories footer    
$delete_category_query = mysql_query("delete FROM map_category_footer WHERE catid2 = '". $category_id ."'");

if (empty($_POST['check_list_footer'])) {echo '';}
if (is_array($_POST['check_list_footer']))
{
foreach ($_POST['check_list_footer'] as $selected) {          
$insert_category_query = mysql_query("INSERT INTO map_category_footer(catid1, catid2) VALUES ('". $category_id ."','". $selected ."')");
}
}

    redirect_page('/admin/file.php?select=edit_category&categ='. $category_id);
    }

    $category_query = mysql_query("SELECT * FROM categories WHERE catid = '". $category_id ."' LIMIT 1");   
    $category_row = mysql_fetch_assoc($category_query);
    $edit_category = array (
        'name'          =>  $category_row['name'],  
        'description'   =>  $category_row['description']
    );

// select categories sidebar
    $top_category_query = mysql_query("SELECT c.catid, c.name, (CASE WHEN mc.catid1 IS NULL THEN 0 ELSE 1 END) AS is_selected
                                    FROM categories c
                                    LEFT JOIN map_category mc 
                                        ON c.catid = mc.catid2
                                        and mc.catid1 = '". $category_id ."' 
                                    WHERE c.catid <> '". $category_id ."' 
                                    ORDER BY c.name ASC "); 

    while($top_category_row = mysql_fetch_assoc($top_category_query))
        {
            $top_edit_category[] = $top_category_row;
        }   


// select categories footer     
    $top_category_footer_query = mysql_query("SELECT c.catid, c.name, (CASE WHEN mc.catid1 IS NULL THEN 0 ELSE 1 END) AS is_selected
                                    FROM categories c
                                    LEFT JOIN map_category_footer mc 
                                        ON c.catid = mc.catid2
                                        and mc.catid1 = '". $category_id ."' 
                                    WHERE c.catid <> '". $category_id ."' 
                                    ORDER BY c.name ASC "); 

    while($top_category_footer_row = mysql_fetch_assoc($top_category_footer_query))
        {
            $top_edit_category_footer[] = $top_category_footer_row;
        }       

edit_category_admin();
}

?>

Share this post


Link to post
Share on other sites

there are three other serious problems with your code -

 

1) the mysql_ functions are obsolete and have been removed from the latest php version. if you or your web host updates to the latest php version, your code will stop running at all and will need to be rewritten. to avoid wasting time learning obsolete methods that will have to be redo in the near future, you should be using either the PDO or mysqli_ database functions. PDO is the best choice, especially if using prepared queries.

 

2) your code has little to no protection against sql injection or of special sql characters in the data causing sql errors. the easiest and most constant way of protecting against these problems are to use prepared queries.

 

3) don't use the GLOBAL keyword to being data into a function. this breaks encapsulation and you might as well not be using functions. you should pass any data into a function as call time parameters.

Share this post


Link to post
Share on other sites

there are three other serious problems with your code -

 

1) the mysql_ functions are obsolete and have been removed from the latest php version. if you or your web host updates to the latest php version, your code will stop running at all and will need to be rewritten. to avoid wasting time learning obsolete methods that will have to be redo in the near future, you should be using either the PDO or mysqli_ database functions. PDO is the best choice, especially if using prepared queries.

 

2) your code has little to no protection against sql injection or of special sql characters in the data causing sql errors. the easiest and most constant way of protecting against these problems are to use prepared queries.

 

3) don't use the GLOBAL keyword to being data into a function. this breaks encapsulation and you might as well not be using functions. you should pass any data into a function as call time parameters.

 

I resolve my problem.

 

Thank you for your time @mac_gyver !

Share this post


Link to post
Share on other sites

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.

Sign in to follow this  

×
×
  • 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.