Foros del Web » Programando para Internet » PHP »

formulario no guarda datos desde mysql

Estas en el tema de formulario no guarda datos desde mysql en el foro de PHP en Foros del Web. Estimados, tengo un formulario, el cual tiene datos en forma de select traidos desde una base de datos, el problema es que al momento de ...
  #1 (permalink)  
Antiguo 20/12/2011, 09:24
 
Fecha de Ingreso: noviembre-2011
Mensajes: 121
Antigüedad: 12 años, 5 meses
Puntos: 0
formulario no guarda datos desde mysql

Estimados, tengo un formulario, el cual tiene datos en forma de select traidos desde una base de datos, el problema es que al momento de escribir los datos en la nueva base de datos no me guarda los datos que tienen los combobox... todo lo que es texto plano ningun drama...

Código PHP:
<?php require_once('Connections/localhost.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  if (
PHP_VERSION 6) {
    
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}

$editFormAction $_SERVER['PHP_SELF'];
if (isset(
$_SERVER['QUERY_STRING'])) {
  
$editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset(
$_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  
$insertSQL sprintf("INSERT INTO proyectos (proy, noper, admpro, contacto, region, comuna, direccion, telefono, correo, pert) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       
GetSQLValueString(strtoupper($_POST['proy']), "text"),
                       
GetSQLValueString(strtoupper($_POST['noper']), "text"),
                       
GetSQLValueString(strtoupper($_POST['admpro']), "text"),
                       
GetSQLValueString(strtoupper($_POST['contacto']), "text"),
                       
GetSQLValueString($_POST['region'], "text"),
                       
GetSQLValueString($_POST['comuna'], "text"),
                       
GetSQLValueString(strtoupper($_POST['direccion']), "text"),
                       
GetSQLValueString($_POST['telefono'], "text"),
                       
GetSQLValueString($_POST['correo'], "text"),
                       
GetSQLValueString(strtoupper($_POST['pert']), "text"));

  
mysql_select_db($database_localhost$localhost);
  
$Result1 mysql_query($insertSQL$localhost) or die(mysql_error());
}

mysql_select_db($database_localhost$localhost);
$query_pert "SELECT * FROM tippro";
$pert mysql_query($query_pert$localhost) or die(mysql_error());
$row_pert mysql_fetch_assoc($pert);
$totalRows_pert mysql_num_rows($pert);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<style type="text/css">
<!--
.Estilo5 {font-family: Arial; font-size: 12; }
.Estilo6 {font-size: 12}
-->
</style>
</head>

<body>
<form action="<?php echo $editFormAction?>" method="POST" name="form1" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><span class="Estilo5">Nombre del Proyecto</span>:</td>
      <td><input type="text" name="proy" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><span class="Estilo5">N&uacute;mero de Operaci&oacute;n </span>:</td>
      <td><input type="text" name="noper" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><span class="Estilo5">Administrador del Proyecto</span>:</td>
      <td><input type="text" name="admpro" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><span class="Estilo5">Nombre del Contacto</span>:</td>
      <td><input type="text" name="contacto" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><span class="Estilo5">Regi&oacute;n</span>:</td>
      <td><input type="text" name="region" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><span class="Estilo5">Comuna</span>:</td>
      <td><input type="text" name="comuna" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><span class="Estilo5">Direcci&oacute;n</span>:</td>
      <td><input type="text" name="direccion" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><span class="Estilo5">Tel&eacute;lefono</span>:</td>
      <td><input type="text" name="telefono" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><span class="Estilo5">Correo</span>:</td>
      <td><input type="text" name="correo" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><span class="Estilo5">Pertenece a</span>:</td>
      <td><select>
  <?php
do {  
?>
  <option type="text" name="pert" value="<?php echo $row_pert['pert']?>"><?php echo $row_pert['pert']?></option>
  <?php
} while ($row_pert mysql_fetch_assoc($pert));
  
$rows mysql_num_rows($pert);
  if(
$rows 0) {
      
mysql_data_seek($pert0);
      
$row_pert mysql_fetch_assoc($pert);
  }
?>
</select></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Insertar registro" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
<script> 

<?php

 


        
for($z2=1;$z2 count($regiones);$z2++) {
                
$filenamemod="opttec/ciudades/".rtrim($regiones[$z2]).".txt";
                        
                
$textomod=file($filenamemod);
                
                
                echo 
'var comunas_'.$z2.'=new Array("-"';
                for(
$zmod=1;$zmod<count($textomod);$zmod++) {
                                echo 
',"'.rtrim($textomod[$zmod]).'"';    
            }    
            echo 
')'.chr(13).chr(10);
            }
?>

function cambia_region(){

var selregion
selregion = document.f1.selregion[document.f1.selregion.selectedIndex].value
    
if (selregion != 0) {
        //si estaba definido, entonces coloco las opciones de la comuna correspondiente.
        //selecciono el array de comuna adecuado
mis_comunas=eval("comunas_" + selregion)
        //calculo el numero de comunas
num_comunas = mis_comunas.length
        //marco el número de comunas en el select
document.f1.comuna.length = num_comunas
        //para cada comuna del array, la introduzco en el select
for(i=1;i < num_comunas;i++){
document.f1.comuna.options[i].value=mis_comunas[i]
document.f1.comuna.options[i].text=mis_comunas[i]
        }    
 }else{
        //si no había comuna seleccionada, elimino las comunas del select
    document.f1.comuna.length = 1
        //coloco un guión en la única opción que he dejado
document.f1.comuna.options[0].value = "-"
document.f1.comuna.options[0].text = "-"
    }
    //marco como seleccionada la opción primera de comuna
    document.f1.comuna.options[0].selected = true
}
</script>
</body>
</html>
<?php
mysql_free_result
($pert);
?>
actualmente el codigo solo tiene combobox para el campo "pertenece a", pero luego tendra uno dinamico para la region-comuna...
  #2 (permalink)  
Antiguo 20/12/2011, 12:45
Avatar de truman_truman  
Fecha de Ingreso: febrero-2010
Ubicación: /home/user
Mensajes: 1.341
Antigüedad: 14 años, 2 meses
Puntos: 177
Respuesta: formulario no guarda datos desde mysql

No te tira algún error?
si es así copialo y pegalo para que lo veamos
__________________
la la la
  #3 (permalink)  
Antiguo 20/12/2011, 13:02
 
Fecha de Ingreso: noviembre-2011
Mensajes: 121
Antigüedad: 12 años, 5 meses
Puntos: 0
Respuesta: formulario no guarda datos desde mysql

No, no arroja ningun error al guardar... solo guarda el texto "plano", los combobox desde mysql no... deja el campo vacio en la tabla final...

probe con select simples y si me guarda la data...
  #4 (permalink)  
Antiguo 20/12/2011, 13:08
Avatar de truman_truman  
Fecha de Ingreso: febrero-2010
Ubicación: /home/user
Mensajes: 1.341
Antigüedad: 14 años, 2 meses
Puntos: 177
Respuesta: formulario no guarda datos desde mysql

Ah ahora entiendo, el problema es que no le pusiste nombre al select (combobox)

<select>

en su lugar se lo pusiste a la etiqueta option, eso está mal

<option type="text" name="pert"


debería ser asi:

<select name="pert">



.
__________________
la la la
  #5 (permalink)  
Antiguo 20/12/2011, 13:52
 
Fecha de Ingreso: noviembre-2011
Mensajes: 121
Antigüedad: 12 años, 5 meses
Puntos: 0
Respuesta: formulario no guarda datos desde mysql

sere wn!!!!... muchas gracias... ¬¬
  #6 (permalink)  
Antiguo 20/12/2011, 15:14
 
Fecha de Ingreso: noviembre-2011
Mensajes: 121
Antigüedad: 12 años, 5 meses
Puntos: 0
Respuesta: formulario no guarda datos desde mysql

ahora tengo otra consulta, me guarda el valor de region_id y no de region_nombre y la comuna directamente no la guarda

Código PHP:
<?php require_once('Connections/localhost.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  if (
PHP_VERSION 6) {
    
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}

$editFormAction $_SERVER['PHP_SELF'];
if (isset(
$_SERVER['QUERY_STRING'])) {
  
$editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
}


if ((isset(
$_POST["MM_insert"])) && ($_POST["MM_insert"] == "f1")) {
  
$insertSQL sprintf("INSERT INTO proyectos (proy, noper, admpro, contacto, region, comuna, direccion, telefono, correo, pert) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       
GetSQLValueString(strtoupper($_POST['proy']), "text"),
                       
GetSQLValueString(strtoupper($_POST['noper']), "text"),
                       
GetSQLValueString(strtoupper($_POST['admpro']), "text"),
                       
GetSQLValueString(strtoupper($_POST['contacto']), "text"),
                       
GetSQLValueString(strtoupper($_POST['region']), "text"),
                       
GetSQLValueString(strtoupper($_POST['comuna']), "text"),
                       
GetSQLValueString(strtoupper($_POST['direccion']), "text"),
                       
GetSQLValueString($_POST['telefono'], "text"),
                       
GetSQLValueString($_POST['correo'], "text"),
                       
GetSQLValueString($_POST['pert'], "text"));
                       
  
mysql_select_db($database_localhost$localhost);
  
$Result1 mysql_query($insertSQL$localhost) or die(mysql_error());
}

mysql_select_db($database_localhost$localhost);
$query_Recordset1 "SELECT * FROM proyectos";
$Recordset1 mysql_query($query_Recordset1$localhost) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 mysql_num_rows($Recordset1);

mysql_select_db($database_localhost$localhost);
$query_pert "SELECT * FROM tippro";
$pert mysql_query($query_pert$localhost) or die(mysql_error());
$row_pert mysql_fetch_assoc($pert);
$totalRows_pert mysql_num_rows($pert);

$con mysql_connect("localhost","root","")
     or die (
"Error al conectar a la base de datos.");
mysql_select_db("optchile"$con)
     or die (
"Error al conectar a la base de datos.");
?>
<html>

<head>
<script type="text/javascript" language="javascript" src="js/funciones.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<style type="text/css">
<!--
.Estilo5 {font-family: Arial; font-size: 12; }
.Estilo6 {font-size: 12}
-->
</style>
</head>

<?php
$proy 
$_POST['proy'];
$noper $_POST['noper'];
$admpro $_POST['admpro'];
$contacto $_POST['contacto'];
$region $_POST['region'];
$comuna $_POST['comuna'];
$direccion $_POST['direccion'];
$telefono $_POST['telefono'];
$correo $_POST['correo'];
$pertenece $_POST['pertenece'];
?>


<body>


<form action="<?php echo $editFormAction?>" method="POST" name="f1" target="_self" id="f1">


<table width="72%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
      <table width="102%"  align="center" border="0" cellspacing="4" cellpadding="0">
      <tr>
        <td width="49%" ><div align="right"><span class="Estilo5">Nombre del Proyecto : </span></div></td>
        <td width="51%"><span class="Estilo5">
          <input name="proy" type="text" id="proy" size="50" maxlength="80" />
        </span></td>
      </tr>
         <tr>
        <td width="49%"><div align="right"><span class="Estilo5">N&uacute;mero de Operaci&oacute;n : </span></div></td>
        <td width="51%"><span class="Estilo6">
          <input name="noper" type="text" id="noper" size="6" maxlength="6"/>
          </span></td>
      </tr>
          <td><div align="right"><span class="Estilo5">Administrador del Proyecto : </span></div></td>
        <td><span class="Estilo6">
          <input name="admpro" type="text" id="admpro" size="50" maxlength="80"/>
          </span></td>
      </tr>
      <tr>
        <td><div align="right"><span class="Estilo5">Nombre del Contacto : </span></div></td>
        <td><span class="Estilo6">
          <input name="contacto" type="text" id="contacto" size="50" maxlength="80"/>
        </span></td>
      </tr>
      <tr>
        <td><div align="right"><span class="Estilo5">Región : </span></div></td>
<?php
$sql
="select region_id,region_nombre from regiones order by region_id asc";
$res=mysql_query($sql,$con);
?>       
<td>
<select name="region" onChange="from(document.f1.region.value,'comuna','comuna.php')">
<option value="0">Seleccione la regi&oacute;n</option>

<?php
while ($region=mysql_fetch_array($res))
{
?>
<option value="<?php echo $region["region_id"];?>"><?php echo $region["region_nombre"];?></option>
<?php
}
?>

</select> 
</td>
          </tr>            
         <tr>
            <td><div align="right"><span class="Estilo5">Comuna : </span></div></td>
<td>
<div id="comuna">
<select name="comuna">
<option value="0">Seleccione la comuna</option>
</select>
</div>
</td>
         </tr>
                 
      <tr>
        <td><div align="right"><span class="Estilo5">Direcci&oacute;n : </span></div></td>
        <td><span class="Estilo6">
          <input name="direccion" type="text" id="direccion" size="50" maxlength="200"/>
          </span></td>
      </tr>
      <tr>
        <td><div align="right"><span class="Estilo5">Tel&eacute;lefono : </span></div></td>
        <td><span class="Estilo6">
          <input name="telefono" type="text" id="telefono" size="50" maxlength="50"/>
          </span></td>
      </tr>
      <tr>
        <td width="49%"><div align="right"><span class="Estilo5">Correo : </span></div></td>
        <td width="51%"><span class="Estilo6">
          <input name="correo" type="text" id="correo" size="50" maxlength="50"/>
          </span></td>
      </tr>
      <tr>
        <td height="40"><div align="right"><span class="Estilo5">Pertenece a : </span></div></td>
        <td>
        <select name="pert">
          <option value="" <?php if (!(strcmp(""$row_pert['pert']))) {echo "selected=\"selected\"";} ?>>Seleccionar</option>
          <?php
do {  
?>
          <option value="<?php echo $row_pert['pert']?>"<?php if (!(strcmp($row_pert['pert'], $row_pert['pert']))) {echo "selected=\"selected\"";} ?>><?php echo $row_pert['pert']?></option>
          <?php
} while ($row_pert mysql_fetch_assoc($pert));
  
$rows mysql_num_rows($pert);
  if(
$rows 0) {
      
mysql_data_seek($pert0);
      
$row_pert mysql_fetch_assoc($pert);
  }
?>
        </select>
        </td>
      </tr>
      <tr>
        <td width="49%"><div align="right"></div></td>
        <td width="51%"><label>
          <input name="CREAR PROYECTO" type="submit" id="CREAR PROYECTO" onClick="MM_validateForm('nompro','','R','numope','','R','admpro','','R','nomcon','','R','direc','','R','telef','','R','correo','','RisEmail');return document.MM_returnValue" value="CREAR PROYECTO" />
          </label>
        <BR /></td>
      </tr>
    </table>
  
    </td>
     </tr>
</table></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>
<input type="hidden" name="MM_insert" value="f1">
</form>
<p>&nbsp;</p>
<script> 

<?php

 


        
for($z2=1;$z2 count($regiones);$z2++) {
                
$filenamemod="opttec/ciudades/".rtrim($regiones[$z2]).".txt";
                        
                
$textomod=file($filenamemod);
                
                
                echo 
'var comunas_'.$z2.'=new Array("-"';
                for(
$zmod=1;$zmod<count($textomod);$zmod++) {
                                echo 
',"'.rtrim($textomod[$zmod]).'"';    
            }    
            echo 
')'.chr(13).chr(10);
            }
?>

function cambia_region(){

var selregion
selregion = document.f1.selregion[document.f1.selregion.selectedIndex].value
    
if (selregion != 0) {
        //si estaba definido, entonces coloco las opciones de la comuna correspondiente.
        //selecciono el array de comuna adecuado
mis_comunas=eval("comunas_" + selregion)
        //calculo el numero de comunas
num_comunas = mis_comunas.length
        //marco el número de comunas en el select
document.f1.comuna.length = num_comunas
        //para cada comuna del array, la introduzco en el select
for(i=1;i < num_comunas;i++){
document.f1.comuna.options[i].value=mis_comunas[i]
document.f1.comuna.options[i].text=mis_comunas[i]
        }    
 }else{
        //si no había comuna seleccionada, elimino las comunas del select
    document.f1.comuna.length = 1
        //coloco un guión en la única opción que he dejado
document.f1.comuna.options[0].value = "-"
document.f1.comuna.options[0].text = "-"
    }
    //marco como seleccionada la opción primera de comuna
    document.f1.comuna.options[0].selected = true
}
</script>
</body>
</html>
<?php
mysql_free_result
($Recordset1);

mysql_free_result($pert);
?>

Etiquetas: formulario, html, mysql, registro, sql, guardar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 15:29.