Foros del Web » Programando para Internet » PHP »

Problemas con los apostrofes al guardar registro en mysql con php

Estas en el tema de Problemas con los apostrofes al guardar registro en mysql con php en el foro de PHP en Foros del Web. TENGO UN FORMULARIO CON MAS DE 30 CAMPOS. MI PROBLEMA ESTA EN QUE NO ME GUARDA EL REGISTRO CUANDO ALGUN CAMPO CONTENGA APOSTROFE { ' ...
  #1 (permalink)  
Antiguo 26/08/2011, 13:43
Avatar de PHPaniel  
Fecha de Ingreso: abril-2011
Mensajes: 16
Antigüedad: 13 años
Puntos: 2
Exclamación Problemas con los apostrofes al guardar registro en mysql con php

TENGO UN FORMULARIO CON MAS DE 30 CAMPOS. MI PROBLEMA ESTA EN QUE NO ME GUARDA EL REGISTRO CUANDO ALGUN CAMPO CONTENGA APOSTROFE { ' }. LES EXPLICO:

CUANDO PONGO EN UNO DE LOS CAMPOS:

CAMPO1 : Mc'Gregor
CAMPO2 : Salazar

PUES NO SOLO NO SE GUARDA CAMPO1 SINO TAMBIEN CAMPO2 NI MUCHO MENOS LOS 28 CAMPOS RESTANTES SE GUARDAB. Y SI PONGO:

CAMPO1 : McGregor
CAMPO2 : Salazar

RECIEN ME GUARDA TODOS LOS CAMPOS.

AYUDA POR FAVOR, DESDE YA SE LOS AGRADEZCO:

ESTE ES MI CODIGO PARA EL MOMENTO DE LLENADO:

Código PHP:
<tr>
<td valign="top">Mejor Pelicula :</td>
<td valign="top"><input name="mpelicula" type="text" class="input-rojo" id="mpelicula" value="<?php echo $row_rsT['mpelicula']; ?>" maxlength="500" /></td>
<td valign="top">Mejor Director :</td>
<td valign="top"><input name="mdirector" type="text" class="input-rojo" id="mdirector" value="<?php echo $row_rsT['mdirector']; ?>" maxlength="500" /></td>
</tr>
  #2 (permalink)  
Antiguo 26/08/2011, 13:45
Avatar de andresdzphp
Colaborador
 
Fecha de Ingreso: julio-2011
Ubicación: $this->Colombia;
Mensajes: 2.749
Antigüedad: 12 años, 9 meses
Puntos: 793
Respuesta: Problemas con los apostrofes al guardar registro en mysql con php

Usa la función addslashes y cuando vayas a mostrar los campos usas la función stripslashes

Código PHP:
Ver original
  1. <?php
  2. $str = "Is your name O'reilly?";
  3.  
  4. // Outputs: Is your name O\'reilly?
  5. echo addslashes($str);
  6. ?>

Código PHP:
Ver original
  1. <?php
  2. $str = "Is your name O\'reilly?";
  3.  
  4. // Salida: Is your name O'reilly?
  5. echo stripslashes($str);
  6. ?>
__________________
Si sabemos como leer e interpretar el manual será mucho más fácil aprender PHP. En lugar de confiar en ejemplos o copiar y pegar - PHP
  #3 (permalink)  
Antiguo 26/08/2011, 13:46
Avatar de PHPaniel  
Fecha de Ingreso: abril-2011
Mensajes: 16
Antigüedad: 13 años
Puntos: 2
Exclamación Respuesta: Problemas con los apostrofes al guardar registro en mysql con php

ME OLVIDE DECIRLES QUE CON EL CODIGO QUE LES MENCIONE ME ANDA BIEN DE LOCAL OZEA SI ME GUARDA CUANDO TENGO LA WEB Y LA BASE DE DATOS DE LOCAL PERO CUANDO LO SUBO A UN SERVIDOR ES AHI DONDE NO ME GUARDA LOS DATOS. ESTOY ALOJANDOLO EN UN SERVIDOR DE BASE DE DATOS - HOSTING DE "DREAMHOST", PROBE EN "GODADDY" Y ES LO MISMO NO ME GUARDA.
  #4 (permalink)  
Antiguo 26/08/2011, 13:58
 
Fecha de Ingreso: abril-2006
Mensajes: 105
Antigüedad: 18 años
Puntos: 11
Respuesta: Problemas con los apostrofes al guardar registro en mysql con php

Realmente no se si se puefe solucionar asi, pero te lo digo por si las moscas.

En el archivo que recoje los datos del formulario, ¿Como lo haces, con comillas dobles o apostrof?

Prueba a definir las variables con comillas dobles, es lo unico que se me ocurre.

Aunque dudo que te funcione lo que te he dicho, pero por intentarlo...
  #5 (permalink)  
Antiguo 26/08/2011, 14:04
Avatar de PHPaniel  
Fecha de Ingreso: abril-2011
Mensajes: 16
Antigüedad: 13 años
Puntos: 2
Mensaje Respuesta: Problemas con los apostrofes al guardar registro en mysql con php

Cita:
Iniciado por Malekith Ver Mensaje
Realmente no se si se puefe solucionar asi, pero te lo digo por si las moscas.

En el archivo que recoje los datos del formulario, ¿Como lo haces, con comillas dobles o apostrof?

Prueba a definir las variables con comillas dobles, es lo unico que se me ocurre.

Aunque dudo que te funcione lo que te he dicho, pero por intentarlo...

ESTE ES EL CODIGO CON EL QUE RECOJO MIS DATOS:

Código PHP:
<input name="mactors" type="text" class="input-rojo" id="mactors" value='<?php echo $row_rsT['mactors']; ?>' maxlength="500" />
TAMBIEN ESTE:

Código PHP:
<input name="mactors" type="text" class="input-rojo"  id="mactors" value="<?php echo $row_rsT['mactors']; ?>"  maxlength="500" />
PERO COMO LES DIGO: DE LOCAL ME FUNCIONA BIEN PERO CUANDO LO SUBO AL SERVIDOR DE DREAMHOST O GODADDY NO ME GRABA NADA DE NADA SI ES QUE CONTIENE ALGUN APOSTROFE.
  #6 (permalink)  
Antiguo 26/08/2011, 14:17
Avatar de PHPaniel  
Fecha de Ingreso: abril-2011
Mensajes: 16
Antigüedad: 13 años
Puntos: 2
Exclamación Respuesta: Problemas con los apostrofes al guardar registro en mysql con php

AMIGO ANDRES HICE COMO LO SUGERISTE DE ESTA FORMA (ESPERO ESTAR HACIENDOLO BIEN) :

Código PHP:
<input name="mactor" type="text" class="input-rojo" id="mactor" value='<?php echo addslashes($row_rsT['mactor']); ?>' maxlength="500" />
SI ES CORRESTO PUES TE DIGO QUE NADA IGUAL NO ME GUARDA NADA, REVISE EL PHPMYADMIN Y ESTA VACIO

PROBE TAMBIEN CON htmlentities y htmlspecialchars Y NADA TAMPOCO
  #7 (permalink)  
Antiguo 26/08/2011, 14:40
 
Fecha de Ingreso: abril-2006
Mensajes: 105
Antigüedad: 18 años
Puntos: 11
Respuesta: Problemas con los apostrofes al guardar registro en mysql con php

Pero no has hecho lo que te sugeri, quita las comillas simples y pon comillas dobles en su lugar.
  #8 (permalink)  
Antiguo 26/08/2011, 14:44
Avatar de darkasecas  
Fecha de Ingreso: marzo-2005
Ubicación: SantaCata, NL, Mexico
Mensajes: 1.553
Antigüedad: 19 años, 1 mes
Puntos: 77
Respuesta: Problemas con los apostrofes al guardar registro en mysql con php

1) Usa addslashes() al momento de guardar, no antes

2) Si te funciona en local lo mas probable es que tengas magic_quotes_gpc activado en tu php.ini
  #9 (permalink)  
Antiguo 26/08/2011, 14:58
Avatar de PHPaniel  
Fecha de Ingreso: abril-2011
Mensajes: 16
Antigüedad: 13 años
Puntos: 2
Información Respuesta: Problemas con los apostrofes al guardar registro en mysql con php

Cita:
Iniciado por Malekith Ver Mensaje
Pero no has hecho lo que te sugeri, quita las comillas simples y pon comillas dobles en su lugar.

SI TE REFIERES A LAS COMILLAS SIMPLES DEL VALUE:

Código PHP:
value='<?php echo $row_rsT['mpelicula']; ?>'
YA LOS CAMBIE POR ESTE:

Código PHP:
value="<?php echo $row_rsT['mpelicula']; ?>"
Y SI TE REFIERES AL QUE ESTA DENTRO DEL ROW:

Código PHP:
$row_rsT['mpelicula']; 
LO CAMBIE POR ESTE:


Código PHP:
$row_rsT["mpelicula"]; 
Y NADA!!!

PERO COMO LES DIGO, SIN NECESIDAD DE CAMBIAR/QUITAR LAS COMILLAS SIMPLRES O DOBLES Y SIN AGREGAR HTMLENTITIES, HTMLSPECIALSCHARS, ADDSLASH U OTRO ME FUNCIONA BIEN CUANDO LO TRABAJO DE LOCAL, PERO CUANDO LO SUBO AL SERVIDOR NO ME FUNCIONA. QUIZAS SEA EL SERVIDOR EL PROBLEMA O NOSE. HELP ME!
  #10 (permalink)  
Antiguo 26/08/2011, 15:00
Avatar de darkasecas  
Fecha de Ingreso: marzo-2005
Ubicación: SantaCata, NL, Mexico
Mensajes: 1.553
Antigüedad: 19 años, 1 mes
Puntos: 77
Respuesta: Problemas con los apostrofes al guardar registro en mysql con php

Leiste mi post (#8) ? -.-
  #11 (permalink)  
Antiguo 26/08/2011, 15:07
Avatar de PHPaniel  
Fecha de Ingreso: abril-2011
Mensajes: 16
Antigüedad: 13 años
Puntos: 2
Mensaje Respuesta: Problemas con los apostrofes al guardar registro en mysql con php

ESTE ES EL CODIGO QUE PROCESA CON LA QUE ESTOY TRABAJANDO, DONDE TENDRIA QUE MODIFICAR ESE addslashes() AMIGO darkasecas??? QUIZA DEBA MODIFICAR LA LINEA # 10 DE ESE MAGIC_QUOTES COMO TU DICES, DESDE YA LES AGRADEZCO SU AYUDA.

Código PHP:
<?php require_once('../../../Connections/dbcpanel.php'); ?>
<?php
mysql_select_db
($database_dbcpanel$dbcpanel);
require(
"../seguridad.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;
}
}

$colname_rsT "-1";
if (isset(
$_GET['idnoticia'])) {
  
$colname_rsT $_GET['idnoticia'];
}
mysql_select_db($database_dbcpanel$dbcpanel);
$query_rsT sprintf("SELECT * FROM osc_anios WHERE idnoticia = %s"GetSQLValueString($colname_rsT"int"));
$rsT mysql_query($query_rsT$dbcpanel) or die(mysql_error());
$row_rsT mysql_fetch_assoc($rsT);
$totalRows_rsT mysql_num_rows($rsT);
?>
  #12 (permalink)  
Antiguo 26/08/2011, 15:10
Avatar de PHPaniel  
Fecha de Ingreso: abril-2011
Mensajes: 16
Antigüedad: 13 años
Puntos: 2
Pregunta Respuesta: Problemas con los apostrofes al guardar registro en mysql con php

Y ESTE UNO DE LOS CAMPOS DEL CUAL DEBO REGISTRA Y GUARDARLO EN LA BASE DE DATOS

Código PHP:
<input name="mactor" type="text" class="input-rojo" id="mactor" value="<?php echo $row_rsT['mactor']; ?>" maxlength="500" />
<textarea name="cactor" rows="4" class="input-rojo" id="cactor"><?php echo $row_rsT['cactor']; ?></textarea>
  #13 (permalink)  
Antiguo 26/08/2011, 15:12
Avatar de darkasecas  
Fecha de Ingreso: marzo-2005
Ubicación: SantaCata, NL, Mexico
Mensajes: 1.553
Antigüedad: 19 años, 1 mes
Puntos: 77
Respuesta: Problemas con los apostrofes al guardar registro en mysql con php

Pues en ese codigo no tienes que modificarlo porque ahi no es donde insertas en la BD, postea el codigo que usas para guardar :s
  #14 (permalink)  
Antiguo 26/08/2011, 15:18
Avatar de PHPaniel  
Fecha de Ingreso: abril-2011
Mensajes: 16
Antigüedad: 13 años
Puntos: 2
Información Respuesta: Problemas con los apostrofes al guardar registro en mysql con php

Cita:
Iniciado por darkasecas Ver Mensaje
Pues en ese codigo no tienes que modificarlo porque ahi no es donde insertas en la BD, postea el codigo que usas para guardar :s
AQUI ESTA, CON ESTO GUARDO:

Código PHP:
<?php
require('../../../Connections/dbcpanel.php');
mysql_select_db($database_dbcpanel$dbcpanel);

if (
$_POST){
    
$xid $_POST['idnoticia'];
    
$xanio $_POST['anio'];    
    
$xfecha $_POST['fecha'];
    
$xmpelicula $_POST['mpelicula'];
    
$xcpelicula $_POST['cpelicula'];    
    
$xmdirector $_POST['mdirector'];
    
$xcdirector $_POST['cdirector'];    
    
$xmactor $_POST['mactor'];
    
$xcactor $_POST['cactor'];    
    
$xmactriz $_POST['mactriz'];
    
$xcactriz $_POST['cactriz'];    
    
$xmactors $_POST['mactors'];
    
$xcactors $_POST['cactors'];    
    
$xmactrizs $_POST['mactrizs'];
    
$xcactrizs $_POST['cactrizs'];    
    
$xmguiono $_POST['mguiono'];
    
$xmguiona $_POST['mguiona'];
    
$xmpeliculaex $_POST['mpeliculaex'];
    
$xmfotografia $_POST['mfotografia'];
    
$xmdireccion $_POST['mdireccion'];
    
$xmsonido $_POST['msonido'];
    
$xmedicion $_POST['medicion'];
    
$xmbanda $_POST['mbanda'];
    
$xmcancion $_POST['mcancion'];
    
$xmmaquillaje $_POST['mmaquillaje'];
    
$xmvestuario $_POST['mvestuario'];
    
$xmefectos $_POST['mefectos'];
    
$xmmontaje $_POST['mmontaje'];
    
$xmpeliculanim $_POST['mpeliculanim'];
    
$xmdocumentalc $_POST['mdocumentalc'];
    
$xmdocumentall $_POST['mdocumentall'];
    
$xmcortometrajef $_POST['mcortometrajef'];
    
$xmcortometrajea $_POST['mcortometrajea'];
    
$xmargumento $_POST['margumento'];
    
$xpjuvenil $_POST['pjuvenil'];
    
$xohonor $_POST['ohonor'];
    
$xpirving $_POST['pirving'];
    
$xphuman $_POST['phuman'];
    
$xcuerpo $_POST['cuerpo'];

    
$sql "UPDATE osc_anios SET anio='$xanio', fecha='$xfecha', mpelicula='$xmpelicula', cpelicula='$xcpelicula', mdirector='$xmdirector', cdirector='$xcdirector', mactor='$xmactor', cactor='$xcactor', mactriz='$xmactriz', cactriz='$xcactriz', mactors='$xmactors', cactors='$xcactors', mactrizs='$xmactrizs', cactrizs='$xcactrizs', mguiono='$xmguiono', mguiona='$xmguiona', mpeliculaex='$xmpeliculaex', mfotografia='$xmfotografia', mdireccion='$xmdireccion', msonido='$xmsonido', medicion='$xmedicion', mbanda='$xmbanda', mcancion='$xmcancion', mmaquillaje='$xmmaquillaje', mvestuario='$xmvestuario', mefectos='$xmefectos', mmontaje='$xmmontaje', mpeliculanim='$xmpeliculanim', mdocumentalc='$xmdocumentalc', mdocumentall='$xmdocumentall', mcortometrajef='$xmcortometrajef', mcortometrajea='$xmcortometrajea', margumento='$xmargumento', pjuvenil='$xpjuvenil', ohonor='$xohonor', pirving='$xpirving', phuman='$xphuman', cuerpo='$xcuerpo'";
    
$sql .= "WHERE idnoticia='$xid'";
    
$rs mysql_query($sql$dbcpanel);

    echo 
"<html><script>document.location='vista-general.php';</script></html>";
    } else {
        echo 
"<script language='javascript'>"
        echo 
"alert('E R R O R, intentar otra vez.');";
        echo 
"</script>";
        echo 
"<html><script>document.location='vista-general.php';</script></html>";
}
?>
  #15 (permalink)  
Antiguo 26/08/2011, 15:25
Avatar de darkasecas  
Fecha de Ingreso: marzo-2005
Ubicación: SantaCata, NL, Mexico
Mensajes: 1.553
Antigüedad: 19 años, 1 mes
Puntos: 77
Respuesta: Problemas con los apostrofes al guardar registro en mysql con php

Pues ahi es donde tienes que usar los addslashes(), cuando asignas a la variable el valor que viene en $_POST

$algo = addslashes($_POST['algo']);
  #16 (permalink)  
Antiguo 26/08/2011, 15:46
Avatar de PHPaniel  
Fecha de Ingreso: abril-2011
Mensajes: 16
Antigüedad: 13 años
Puntos: 2
Mensaje Respuesta: Problemas con los apostrofes al guardar registro en mysql con php

Cita:
Iniciado por darkasecas Ver Mensaje
Pues ahi es donde tienes que usar los addslashes(), cuando asignas a la variable el valor que viene en $_POST

$algo = addslashes($_POST['algo']);

GRACIAAAAAAAAAAAAAAAAAAAAAASSS DARKASECAS!!
+10 !!
ME SALIO !!!
Código PHP:
$xcdirector addslashes($_POST['cdirector']);    
    
$xmactor addslashes($_POST['mactor']);
    
$xcactor addslashes($_POST['cactor']);    
    
$xmactriz addslashes($_POST['mactriz']);
    
$xcactriz addslashes($_POST['cactriz']);    
    
$xmactors addslashes($_POST['mactors']); 
TAMBIEN GRACIAS A ANDRES Y A MALEKITH

Etiquetas: mysql, apostrofes
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 00:32.