mcgonzalezp Posted July 9, 2007 Share Posted July 9, 2007 Hello all! I am kind of a newbie in PHP issues, but I try to work with structural programming. I have a form, where I print some database information: try it here: http://190.78.37.17/k/index.php username: asuarez password: 123 So, the user logs in, goes to PREINSCRIPCIONES, and then you click on CONTINUAR (select either value in combo box, it doesn't matter) What I need, is the user to select All the subjects he/she wants to submit, and with one click, update them all. So far, with the code I am sending, I can update One of them, always the last one I select... I believe I need some kind of loop and array, but Can't seem to find where. Please, if anybody has an Idea, please please let me know <?php error_reporting(0); require_once('config.php'); if(!isset($_SESSION['usuario'])) $_SESSION['usuario'] = ''; if(!isset($_SESSION['enlinea'])) $_SESSION['enlinea'] = 'No'; $status=''; if(!isset($_GET['insertar'])) $_GET['insertar'] = ''; switch($_GET['insertar']) { case 'pre': error_reporting(0); { $querySQL= "UPDATE Notas set Preinscrita='".htmlentities($_POST['pi'])."' WHERE notas.usuario_n = '".$_SESSION['IDusuario']."' AND Notas.Materia_n = '".htmlentities($_POST['ID'])."'"; $resultado = mysql_query($querySQL); if(mysql_num_rows($resultado) == 1) { $status='OK'; } else { $status='Datos Incorrectos'; } }//la del case }//la del switch if (($_SESSION['enlinea'] != 'Si')) { echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL=index.php?accion=validar">'; } else { echo Quote Link to comment Share on other sites More sharing options...
no_one Posted July 9, 2007 Share Posted July 9, 2007 First, I didn't check everything, but I think in your html you'll need to change the name of the checkboxes from pi to pi[] That way the server will get all in an array. so: $pi = $_POST['pi'] $pi will now be an array, if you want a string: $pi_str = implode(',',$pi); or something.. Help at all? Quote Link to comment Share on other sites More sharing options...
mcgonzalezp Posted July 9, 2007 Author Share Posted July 9, 2007 Thank you for your prompt reply! I tried something like that already but didn't work... or maybe I did something wrong again. Quote Link to comment Share on other sites More sharing options...
grimmier Posted July 9, 2007 Share Posted July 9, 2007 Are you drawing this table with data from your DB? if so then i would change where you output check boxes to something like this <? $n =0; #set a counter do { # some do loop to create your table based on output ... ?> <input type="checkbox" <? echo " name='pi{$n}';?> value="checked" /> <? $n ++; } while( some statement); ?> this way your checkboxes all have unique names. Quote Link to comment Share on other sites More sharing options...
mcgonzalezp Posted July 10, 2007 Author Share Posted July 10, 2007 Well now I did this... Changed the PI name to MATERIA so I can read the code better... But still doesn't work. There's the for loop... Any ideas?! if (isset($_POST['submit'])) { $materia = $_POST["materia"]; $cuantas = count($materia); echo 'Materias Preinscritas: '.$cuantas.'<br><br>'; for ($i=0; $i<=$cuantas; $i++) { $subquery= "SELECT Count(*) FROM Notas"; $cuenta = mysql_query($subquery); $cuenta2= $cuenta +1; $querySQL= "INSERT INTO notas VALUES ($cuenta2,'".$_SESSION['IDusuario']."', $materia, 0,0)"; $resultado = mysql_query($querySQL); } } Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 10, 2007 Share Posted July 10, 2007 for ($i=0; $i<=$cuantas; $i++) { $subquery= "SELECT Count(*) FROM Notas"; $cuenta = mysql_query($subquery); $cuenta2= $cuenta +1; $querySQL= "INSERT INTO notas VALUES ($cuenta2,'".$_SESSION['IDusuario']."', $materia, 0,0)"; $resultado = mysql_query($querySQL); } } ^^^^??? can you tell me what do you want with that Quote Link to comment Share on other sites More sharing options...
mcgonzalezp Posted July 10, 2007 Author Share Posted July 10, 2007 $querySQL= "INSERT INTO notas VALUES ($cuenta2,'".$_SESSION['IDusuario']."', $materia, 0,0)"; Well, I have a form with an array of checkmarks, calling the Database like this: <input name="materia[]" type="checkbox" id="pi" value="'.$rand['ID_materia'].'"> What I want to do, is to pick all the values of that array, and add them to the database. Create one new record with each selection the user makes of the checkmarks... So the values of the NOTAS table are: 1. Primary key: I 'm using CUENTA (count) to get to the last record) 2. The user is logged, so the ID is being read from the Session 3. The current value of Materia, as Primary key from the array. 4 and 5, two fields that can be null the first time I enter the record. So, the loop isn't working. That's why I am asking for support Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 10, 2007 Share Posted July 10, 2007 look for the value of this $cuantas try to echo that and the other thing i notice is that even if the loop work you still wont get the expected output with that this is annoying $subquery= "SELECT Count(*) FROM Notas"; $cuenta = mysql_query($subquery); you put that in the loop it doesnt do anything $cuenta value is boolean so what is the use of that inside that loop Quote Link to comment Share on other sites More sharing options...
mcgonzalezp Posted July 10, 2007 Author Share Posted July 10, 2007 Ohh!! I see... alright, I'll try to fix that and get back. I hope it works! TY! Quote Link to comment Share on other sites More sharing options...
mcgonzalezp Posted July 10, 2007 Author Share Posted July 10, 2007 THANK YOU! that was a great help I've fixed most of what I need to fix so far, but still have a problem here. Look at my query at line 12. I need to insert those values in my Table NOTAS. The array is for: ID, user, materia (the other 2 values don't matter). Materia is the field I'm trying to get from the checkboxes array as you can see here... BUT, I can't get it to bring Materia value; I mean, the checkboxes array is right there, I see it when I test it outside the FOR loop, but when it is inside, the values dissapear, and Materia goes Null. Last help I need! Thank you ALL. 1 if (isset($_POST['Submit'])) 2 { 3 $cuantas=0; 4 $materia = $_POST["materia"]; 5 $cuantas = count($materia); 6 $cuenta = mysql_query("SELECT * FROM Notas"); 7 $cuenta2= $num_regs +1; 8 for ($i=0; $i<=$cuantas; $i++) 9 { 10 11 echo 'Registros FOR' .$cuenta2. '<br><br>'; 12 $querySQL= "INSERT INTO notas VALUES ($cuenta2,'".$_SESSION['IDusuario']."', $materia, 0,0)"; 13 $cuenta2 = $cuenta2 +1; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.