Jump to content

[SOLVED] Loop needed? but where!?


mcgonzalezp

Recommended Posts

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

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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. 

 

Link to comment
Share on other sites

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

        }

}

Link to comment
Share on other sites

  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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 >:(:D

Link to comment
Share on other sites

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;

 

}

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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