Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] enviar funcion a un insert mysql

Estas en el tema de enviar funcion a un insert mysql en el foro de PHP en Foros del Web. Hola tengo una funcion: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código PHP: Ver original function generarCodigo ( $longitud ) {   $key = '' ;   $pattern = '1234567890abcdefghijklmnopqrstuvwxyz' ...
  #1 (permalink)  
Antiguo 25/01/2016, 12:26
 
Fecha de Ingreso: marzo-2013
Mensajes: 72
Antigüedad: 11 años
Puntos: 0
enviar funcion a un insert mysql

Hola tengo una funcion:

Código PHP:
Ver original
  1. function generarCodigo($longitud) {
  2.  $key = '';
  3.  $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';
  4.  $max = strlen($pattern)-1;
  5.  for($i=0;$i < $longitud;$i++) $key .= $pattern{mt_rand(0,$max)};
  6.  return $key;
  7.  
  8. }

Ahora necesito que este numero de registro lo inserte a mi tabla, yo lo hice asi pero no inserta el registro

Código MySQL:
Ver original
  1. $insertSQL = sprintf("INSERT INTO registro (nombre, num_registro) VALUES (%s, generarCodigo(6);)",
  2.                        GetSQLValueString($_POST['nombre'], "text"),
  3.                GetSQLValueString($_POST['num_registro'], "text"));
  #2 (permalink)  
Antiguo 25/01/2016, 12:38
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: enviar funcion a un insert mysql

No puedes pasar funciones de PHP a SQL, sólo debes pasar el valor.

Código PHP:
Ver original
  1. $insertSQL = sprintf("INSERT INTO registro (nombre, num_registro) VALUES (%s, %s)",
  2.                    GetSQLValueString($_POST['nombre'], "text"),
  3.                    GetSQLValueString(generarCodigo(6), "text"));
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #3 (permalink)  
Antiguo 25/01/2016, 12:46
Avatar de gnzsoloyo
Moderador criollo
 
Fecha de Ingreso: noviembre-2007
Ubicación: Actualmente en Buenos Aires (el enemigo ancestral)
Mensajes: 23.324
Antigüedad: 16 años, 4 meses
Puntos: 2658
Respuesta: enviar funcion a un insert mysql

Cita:
Iniciado por drummerick7 Ver Mensaje
Hola tengo una funcion:


Ahora necesito que este numero de registro lo inserte a mi tabla, yo lo hice asi pero no inserta el registro

Código MySQL:
Ver original
  1. $insertSQL = sprintf("INSERT INTO registro (nombre, num_registro) VALUES (%s, generarCodigo(6);)",
  2.                        GetSQLValueString($_POST['nombre'], "text"),
  3.                GetSQLValueString($_POST['num_registro'], "text"));
@drummerick7:
MySQL no entiende PHP... Ese INSERT debe estar disparando un error de "Función desconocida".
Como buena práctica, por lo menos deberías capturar el error de ejecución en la base de datos, y recordar que PHP y el SQL no existen en el mismo entorno. Son cosas totalmente separadas.
__________________
¿A quién le enseñan sus aciertos?, si yo aprendo de mis errores constantemente...
"El problema es la interfase silla-teclado." (Gillermo Luque)
  #4 (permalink)  
Antiguo 25/01/2016, 12:58
 
Fecha de Ingreso: marzo-2013
Mensajes: 72
Antigüedad: 11 años
Puntos: 0
Respuesta: enviar funcion a un insert mysql

Gracias, quedo bien

Etiquetas: funcion, insert, mysql, registro, sql, tabla
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 04:34.