PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi,
I’m trying to update my database with the word ‘single’ if the checkbox is ticked in the Form of the coed below but unfortunately it’s not updating it for me and is setting all the values in the ‘menu’ column on the database to 0.
Can anyone help me to figure out what I’m missing?
Thanks very much
Thanks for the reply but unfortunately that made no difference. I do have only one checkbox in that form but it returns a good few checkboxes from the database.
One thing I have just noticed when the page loads is that when I un tick a checkbox that is set in the database and hit 'Update Record' it will set the value to 0 in the database but if I try to then go back a tick another box (or the same one) it won't pass that 'ticked' value to the database. So something is getting through with this code but not it all.
Also when the page loads and there is say one checkbox ticked if I leave this alone and hit update it stays ticked.
I'm very puzzled.
i'm quite puzzled with the flow of your code. Checked multiple checkbox that is inside the same form tag are placed in an array when the submit button is triggered. And when you want to access those values you have to use a loop to get all the values that is inside that array. I'm confused with this variable "$totalRows_GetMenuUpdate" which I think is not the size of the array of checkbox values. This might lead to an array index out of bounds(in some other programming languages) or misbehaving code (php).
I’m still struggling to learn all this stuff and appreciate your answer. I had another form that I took this from and I was using this to sort asc and desc the order of items listed in the database. It worked perfectly and the code I posted above will work in the same manner if I replace
The problem of course is that I have to do this by hand i.e. add ‘single’ to the text form field if I want to add the correct details to the database however it’s obvious that this is not acceptable as the client would run amuck with this. I thought it would be easy to add a checkbox but obviously this is proving more confusing to my knowledge of PHP but I know I’m close.
Thanks again for any help
i'm still confused.. can you tell me what this variable is for?
I thought this was " $totalRows_GetMenuUpdate" an integer cause you used it to compare in a for loop (condition)
for($i = 0; $i < $totalRows_GetMenuUpdate; $i++)
now it became an associative array?
if($totalRows_GetMenuUpdate['menu'] == 'single')
Thanks for the reply.
I thought I needed to run my first query to get the data from the database and pass this to the Form so that the checkboxes were populated correctly. Then if the client changes the values of the checkboxes I need to pass this back to the database when the Form is submitted. This is why I thought I needed to loop through the query using for($i=0; $i < $totalRows_GetMenuUpdate; $i++) { to pass and update the database.
This is the entire page of code that I’m using so maybe this will give a little more info.
As I say if I simply remove the "checked=\"checked from the Form below then it all works ok but then it would have to be manually changed.
I think you're doing it the hard way, you don't need to query all the products that have "0" menu and use loop to the queried products and one by one updating........ but instead you just use a single query like
/*
* I am assuming here that you only have one checkbox and one hidden input type that's why I used "ZERO"
* on the menu bf and id bf
*/
$menuBf = $_POST["menu_bf"][0];
$idBf = $_POST["id_bf"][0];
mysql_query("UPDATE prdts SET menu='".$menuBf."' WHERE cat_id='".$idBf."' AND archive=0") or die(mysql_error());