Ver Mensaje Individual
  #1 (permalink)  
Antiguo 25/05/2015, 18:28
thegaac
 
Fecha de Ingreso: octubre-2014
Mensajes: 2
Antigüedad: 9 años, 6 meses
Puntos: 0
Insertar Varios registros a mi Base de Datos MySql

Hola como estan?

Les consulto tengo que insertar varios registros de una sola vez a mi base de datos. Lo logre como se ve en el codigo que esta al final.
ahi elegis cuantos registros queres ingresar y luego completas el formulario y listo!.
Mi problema es que si opor ejemplo pones 2 jugadores, completas el formulario pero al darle guardar te acordas que son 3 jugadores, osea queres agrear uno mas, al poner 3 jugadores los datos ingresados se han borrado porque al ser php se refresaca la pagina, me gustaria saber como hacer esto sin que se borren los datos del formulario.
Alguien me puede ayudar?

Aca les dejo el codigo

Código PHP:
[CODE]<?php
include"head.php";
?>
<body>
<?php
if(isset($_POST["submit"]))
{
    
$dt $_POST[dt];    
    
$sql "SELECT * FROM club WHERE dt='".$dt."'";
    
$res mysql_query($sql);
    
$row mysql_fetch_array($res);
    if(
$row[dt] == $dt)
    {
        echo 
'Este Club ya te has registrado en CSVP<br/>';
    }
    else
    {
        
$telefono $_POST[dttelefono];
        
$email $_POST[dtemail];
        
$nombre $_POST[nombre];
        
$ssql "INSERT INTO club (dt, telefono, email, nombre) VALUES ('".$dt."','".$telefono."','".$email."','".$nombre."')";
        
$resu mysql_query($ssql);
        if(
$resu)
        {
            echo 
'El club '.$nombre.' se a registrado en <b>CSVP Uruguay</b><br/><br/>';
            echo 
'has registrado a los jugadores<br/>';
        }
        else
        {
            echo 
'Error';
        }
        for(
$x=0;$x<=$_POST['cantidad'];$x++)
        if(isset(
$_POST["num" $x]))
        {    
            
$psnid $_POST["psnid".$x];
            
$posicion $_POST["posicion".$x];
            
$nivel $_POST["nivel".$x];
            
$email $_POST["email".$x];
            
$telefono $_POST["telefono".$x];
            
$sql "INSERT INTO jugadores (psnid, posicion, nivel, email, telefono, club) VALUES ('".$psnid."','".$posicion."','".$nivel."','".$email."','".$telefono."','".$nombre."')";
            
$res mysql_query($sql);
            if(
$res)
            {
                echo 
$psnid.'<br/>';
            }
            else
            {
                echo 
'Error!!';
            }
        }
    }
}
else
{
    echo 
'<form id="form1" name="form1" method="post" >';
    echo 
'<tr><td>N. de jugadores a Registrar: </td><td><input size="5" name="cantidad" type="number" min="1" id="cantidad" value="1" /><input type="submit" name="Submit" value="Ok" /></td></tr>';
    echo 
'</form>';
    
$cantidad $_POST[cantidad];
    if(
$cantidad>0)
    {
        echo 
'<form method="post">';
        echo 
'<table>';
        echo 
'<tr><td>Nombre del club: </td><td><input name="nombre" type="text"/></td></tr>';
        echo 
'<tr><td colspan="2"><b>Datos del DT: </b></td></tr>';
        echo 
'<tr><td>PSN ID: </td><td><input name="dt" type="text"/></td></tr>';
        echo 
'<tr><td>Telefono: </td><td><input name="dttelefono" type="text"/></td></tr>';
        echo 
'<tr><td>E-Mail: </td><td><input name="dtemail" type="text"/></td></tr>';
        
$cantidad=1;
        While(
$cantidad<=$_POST['cantidad'])
        {
            echo 
'<tr><td colspan="2"><hr/></td></tr>';
            echo 
'<tr><td>Jugador N°'.$cantidad.'</td><td>&nbsp;</td>';
            echo 
'<tr><td>ID: </td><td><input name="psnid'.$cantidad.'" type="text" required="required"/></td></tr>';
            echo 
'<tr><td>Posicion: </td><td><input name="posicion'.$cantidad.'" type="text" required="required"/></td></tr>';
            echo 
'<tr><td>Nivel Pro: </td><td><input name="nivel'.$cantidad.'" type="text" required="required"/></td></tr>';
            echo 
'<tr><td>E-Mail: </td><td><input name="email'.$cantidad.'" type="text" required="required"/></td></tr>';
            echo 
'<tr><td>Telefono: </td><td><input name="telefono'.$cantidad.'" type="text" required="required"/></td></tr>';
            echo 
'<input name="num'.$cantidad.'" type="hidden">';
            echo 
'<input name="cantidad" type="hidden" id="cantidad" value="'.$_POST[cantidad].'" />';
            
$cantidad++;
        }
        echo 
'<tr><td colspan="2">&nbsp;</td></tr>';
        echo 
'<tr><td colspan="2" align="center"><button type ="submit" name="submit" >Guardar</button></td></tr>';
    }
    echo 
'</table>';
    echo 
'</form>';
}
?>
</body>
<?php
include"footer.php";
?>[/CODE]