Loop missing, but where? :(

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

[b]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.[/b]

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 ''; } else { echo

first of all u should change ur MySQL structure to store multiple values for Preinscrita.

i’d suggest a table called Preinscrita and containing usuario_n and Materia_n as keys (i don’t know what these mean but as u used both in the WHERE they are needed i guess)

and then i would call the checkboxes pi[] and use a loop:

mysql_query(“DELETE FROM Preinscrita WHERE Preinscrita.usuario_n = '”.$_SESSION[‘IDusuario’]."’ AND Preinscrita.Materia_n = ‘".htmlentities($_POST[‘ID’])."’");
foreach($_POST[‘pi’] as $one_pi)
$querySQL= “INSERT INTO Preinscrita set Preinscrita.Preinscrita=’”.htmlentities($_POST[‘pi’])."’, Preinscrita.usuario_n = ‘".$_SESSION[‘IDusuario’]."’, Preinscrita.Materia_n = ‘".htmlentities($_POST[‘ID’])."’";

i’m not a MySQL expert maybe there is a better statemant that only deletes the not needed.

hope this gives u a point to start at

This is my new code…
I decided to assign the Foreign key value to the checkmarks… and insert a new record on my database, but still not working.

Any other Idea? :frowning:

i realy have problems reading ur code (varablenames).

U said it’s not working. what is not working? do u get any errors? can u post ur new db-structure?

Well, I’m sorry my variables are in spanish…

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).
BUT, I can’t get it to bring Materia[i] 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[i] 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			{
11				echo 'Registros FOR' .$cuenta2. '<br><br>';
				$querySQL= "INSERT INTO notas VALUES ($cuenta2,'".$_SESSION['IDusuario']."', $materia[i], 0,0)";
				$cuenta2 = $cuenta2 +1;

Insert statements should be formatted as such:

INSERT INTO table (field1, field2, field3) VALUES (’$value1’, ‘$value2’, ‘$value3’);

It seems that some people don’t use the fields part of the insert statement and I had never seen that until on this form. I guess it can be done that way, never tested it, but anyway…

You need to have your single quotes around each of the values.

You also have your variable wrong - $materia[i]
Should probably be - $materia[$i]

I would recommend making your life easier and do something like the following…

$somevariable = $materia[$i];
$idusuario = $_SESSION[‘IDusuario’];

“INSERT INTO notas VALUES (’$cuenta2’,’$idusuario’,’$somevariable’, ‘0’,‘0’)”;

You can use whatever variables you want, but his can save some keystrokes if you run into problems and need to do some debugging and makes the code a lot more understand/readable.

Just my thoughts.

Excellent advice!

This was the query at the end, working perfectly

$querySQL= “INSERT INTO notas (ID_nota, Usuario_n, Materia_n, Nota) VALUES ($cuenta2,’”.$_SESSION[‘IDusuario’]."’, $materia[$i],0)";

I completed the insert, but tested both ways and it worked… And the variables didn’t need to have single quotes as I saw.

But THANK YOU very much for your ideas everyone!

Sponsor our Newsletter | Privacy Policy | Terms of Service