Foros del Web » Programando para Internet » PHP »

Error al conectar PHP con BBDD

Estas en el tema de Error al conectar PHP con BBDD en el foro de PHP en Foros del Web. Buenas, primero que nada debo aclarar que jamás he tenido una clase de PHP, solo tuve programación en pascal :S. Pero ahora estoy en un ...
  #1 (permalink)  
Antiguo 09/06/2012, 13:27
 
Fecha de Ingreso: noviembre-2008
Mensajes: 23
Antigüedad: 15 años, 5 meses
Puntos: 0
Error al conectar PHP con BBDD

Buenas, primero que nada debo aclarar que jamás he tenido una clase de PHP, solo tuve programación en pascal :S. Pero ahora estoy en un curso de BBDD y he visto SQL, pero para el lunes me pidieron como trabajo hacer una página web en html con scripts en PHP (ni si quiera sé si estoy usando las palabras correctas) para hacer consultas a mi base de datos.

Bueno, les pongo el código PHP que armé primero:

Código HTML:
<!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=utf-8" />
<title>Untitled Document</title>
</head>



<body>

<form action="../../Users/Oliver/Desktop/intentos php/insert.php" method="post">
<b>Formulario de registro de Cliente</b> <br><br>

Primer nombre: <input type="text" name="nombre"><br>
Primer apellido: <input type="text" name="apellido"><br>
RUT: <input type="text" name="rut_c"><br>
Calle y número: <input type="text" name="calle_nro"><br>
Comuna: <input type="text" name="comuna"><br>
Región: <input type="text" name="region"><br>

<input type="submit" value="Registrar">
</form>



<?

$hostname="localhost";
$username="root";
$password="xxxxxxx";   //CLAVE PUESTA EN XAMPP EN LA PARTE DE CHEQUEO DE SEGURIDAD
$db="omohr4";

$nombre=$_POST['nombre'];
$apellido=$_POST['apellido'];
$rut_c=$_POST['rut_c'];
$calle_nro=$_POST['calle_nro'];
$comuna=$_POST['comuna'];
$region=$_POST['region'];

mysql_connect($host,$username,$password);
@mysql_select_db($db);
$query="SELECT rut_c, fecha, id_orden_pedido 
	   FROM orden_pedido";
$query2 = "INSERT INTO `empleado` (`rut_c`, `nombre`, `apellido`, `calle_nro`, `comuna`, `region`) VALUES ('$rut_c','$nombre','$apellido','$calle_nro','$comuna','$region')";
$registro=mysql_query($query2);

echo "Historial de ordenes de pedido hechas por clientes:";

$result=mysql_query($query);
 

?>

</body>
</html> 

Esto imprime bien hasta el botón registrar y luego me tira el siguiente error:


Notice: Undefined index: nombre in C:\xampp\htdocs\aers.php on line 34

Notice: Undefined index: apellido in C:\xampp\htdocs\aers.php on line 35

Notice: Undefined index: rut_c in C:\xampp\htdocs\aers.php on line 36

Notice: Undefined index: calle_nro in C:\xampp\htdocs\aers.php on line 37

Notice: Undefined index: comuna in C:\xampp\htdocs\aers.php on line 38

Notice: Undefined index: region in C:\xampp\htdocs\aers.php on line 39

Notice: Undefined variable: host in C:\xampp\htdocs\aers.php on line 41

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\aers.php on line 41
Historial de ordenes de pedido hechas por clientes:
  #2 (permalink)  
Antiguo 09/06/2012, 14:38
 
Fecha de Ingreso: noviembre-2008
Mensajes: 23
Antigüedad: 15 años, 5 meses
Puntos: 0
Respuesta: Error al conectar PHP con BBDD

Pongo el código SQL de la base por si acaso.

Código:
-- -----------------------------------------------------
-- Table `omohr4`.`cliente`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`cliente` (
  `rut_c` INT(9) NOT NULL ,
  `nombre` VARCHAR(35) NOT NULL ,
  `apellido` VARCHAR(35) NULL ,
  `calle_nro` VARCHAR(35) NOT NULL ,
  `comuna` VARCHAR(35) NOT NULL ,
  `region` VARCHAR(35) NOT NULL ,
  PRIMARY KEY (`rut_c`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `omohr4`.`agenda_telefonica_cliente`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`agenda_telefonica_cliente` (
  `rut_c` INT(9) NOT NULL ,
  `telefono` INT(13) NOT NULL ,
  INDEX `rut_c` (`rut_c` ASC) ,
  CONSTRAINT `rut_c`
    FOREIGN KEY (`rut_c` )
    REFERENCES `omohr4`.`cliente` (`rut_c` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `omohr4`.`orden_pedido`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`orden_pedido` (
  `id_orden_pedido` INT NOT NULL AUTO_INCREMENT ,
  `rut_c_fk` INT(9) NOT NULL ,
  `fecha` DATE NOT NULL ,
  PRIMARY KEY (`id_orden_pedido`) ,
  INDEX `rut_c_fk` (`rut_c_fk` ASC) ,
  CONSTRAINT `rut_c_fk`
    FOREIGN KEY (`rut_c_fk` )
    REFERENCES `omohr4`.`cliente` (`rut_c` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `omohr4`.`proveedor`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`proveedor` (
  `rut_p` INT(9) NOT NULL ,
  `nombre` VARCHAR(35) NOT NULL ,
  `apellido` VARCHAR(35) NULL ,
  `calle_nro` VARCHAR(35) NOT NULL ,
  `comuna` VARCHAR(35) NOT NULL ,
  `region` VARCHAR(35) NOT NULL ,
  PRIMARY KEY (`rut_p`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `omohr4`.`administrador`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`administrador` (
  `rut_a` INT(8) NOT NULL ,
  `titulo` VARCHAR(35) NOT NULL ,
  PRIMARY KEY (`rut_a`) ,
  INDEX `rut_a` (`rut_a` ASC) ,
  CONSTRAINT `rut_a`
    FOREIGN KEY (`rut_a` )
    REFERENCES `omohr4`.`empleado` (`rut_e` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `omohr4`.`empleado`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`empleado` 
(
  `rut_e` INT(8) NOT NULL ,
  `nombre` VARCHAR(35) NOT NULL ,
  `apellido` VARCHAR(35) NOT NULL ,
  `calle_nro` VARCHAR(35) NOT NULL ,
  `comuna` VARCHAR(35) NOT NULL ,
  `region` VARCHAR(35) NOT NULL ,
  `fecha_contrato` DATE NOT NULL ,
  `tipo_empleado` ENUM('a','v','j') NOT NULL ,
  `rut_a_fk` INT(8) NULL ,
  
  PRIMARY KEY (`rut_e`) ,
  INDEX `rut_a_fk` (`rut_a_fk` ASC) ,
  CONSTRAINT `rut_a_fk`
    FOREIGN KEY (`rut_a_fk` )
    REFERENCES `omohr4`.`administrador` (`rut_a` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION
)
  
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `omohr4`.`vendedor`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`vendedor` (
  `rut_v` INT(8) NOT NULL ,
  PRIMARY KEY (`rut_v`) ,
  INDEX `rut_v` (`rut_v` ASC) ,
  CONSTRAINT `rut_v`
    FOREIGN KEY (`rut_v` )
    REFERENCES `omohr4`.`empleado` (`rut_e` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `omohr4`.`entrega`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`entrega` (
  `id_entrega` INT NOT NULL AUTO_INCREMENT ,
  `rut_c_fk2` INT(9) NOT NULL ,
  `rut_v_fk` INT(8) NOT NULL ,
  `fecha` DATE NOT NULL ,
  PRIMARY KEY (`id_entrega`) ,
  INDEX `rut_c_fk2` (`rut_c_fk2` ASC) ,
    CONSTRAINT `rut_c_fk2`
    FOREIGN KEY (`rut_c_fk2` )
    REFERENCES `omohr4`.`cliente` (`rut_c` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `rut_v_fk`
    FOREIGN KEY (`rut_v_fk` )
    REFERENCES `omohr4`.`vendedor` (`rut_v` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `omohr4`.`vehiculo`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`vehiculo` (
  `vin` VARCHAR(17) NOT NULL ,
  `marca` VARCHAR(35) NOT NULL ,
  `modelo` VARCHAR(35) NOT NULL ,
  `anno` YEAR NOT NULL ,
  `precio` INT(9) NOT NULL ,
  `color` VARCHAR(35) NOT NULL ,
  `tipo` VARCHAR(35) NOT NULL ,
  `rut_p` INT(9) NOT NULL ,
  `id_entrega` INT NULL AUTO_INCREMENT ,
  PRIMARY KEY (`vin`) ,
  INDEX `id_entrega` (`id_entrega` ASC) ,
  CONSTRAINT `rut_p`
    FOREIGN KEY (`rut_p` )
    REFERENCES `omohr4`.`proveedor` (`rut_p` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `id_entrega`
    FOREIGN KEY (`id_entrega` )
    REFERENCES `omohr4`.`entrega` (`id_entrega` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `omohr4`.`linea_orden_pedido`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`linea_orden_pedido` (
  `id_linea_orden_pedido` INT NOT NULL AUTO_INCREMENT ,
  `id_orden_pedido` INT NOT NULL ,
  `vin` VARCHAR(17) NOT NULL ,
  PRIMARY KEY (`id_linea_orden_pedido`) ,
  INDEX `id_orden_pedido` (`id_orden_pedido` ASC) ,
  INDEX `vin` (`vin` ASC) ,
  CONSTRAINT `id_orden_pedido`
    FOREIGN KEY (`id_orden_pedido` )
    REFERENCES `omohr4`.`orden_pedido` (`id_orden_pedido` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `vin`
    FOREIGN KEY (`vin` )
    REFERENCES `omohr4`.`vehiculo` (`vin` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `omohr4`.`junior`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`junior` (
  `rut_j` INT(8) NOT NULL ,
  `cargo_mantencion` VARCHAR(35) NOT NULL ,
  PRIMARY KEY (`rut_j`) ,
  INDEX `rut_j` (`rut_j` ASC) ,
  CONSTRAINT `rut_j`
    FOREIGN KEY (`rut_j` )
    REFERENCES `omohr4`.`empleado` (`rut_e` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `omohr4`.`orden_recibo`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`orden_recibo` (
  `id_orden_recibo` INT NOT NULL AUTO_INCREMENT ,
  `rut_p_fk` INT(9) NOT NULL ,
  `fecha` DATE NOT NULL ,
  PRIMARY KEY (`id_orden_recibo`) ,
  INDEX `rut_p_fk` (`rut_p_fk` ASC) ,
  CONSTRAINT `rut_p_fk`
    FOREIGN KEY (`rut_p_fk` )
    REFERENCES `omohr4`.`proveedor` (`rut_p` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `omohr4`.`linea_orden_recibo`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`linea_orden_recibo` (
  `id_linea_orden_recibo` INT NOT NULL AUTO_INCREMENT ,
  `id_orden_recibo` INT NOT NULL ,
  `vin_fk` VARCHAR(17) NOT NULL ,
  PRIMARY KEY (`id_linea_orden_recibo`) ,
  INDEX `id_orden_recibo` (`id_orden_recibo` ASC) ,
  INDEX `vin_fk` (`vin_fk` ASC) ,
  CONSTRAINT `id_orden_recibo`
    FOREIGN KEY (`id_orden_recibo` )
    REFERENCES `omohr4`.`orden_recibo` (`id_orden_recibo` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `vin_fk`
    FOREIGN KEY (`vin_fk` )
    REFERENCES `omohr4`.`vehiculo` (`vin` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `omohr4`.`agenda_telefonica_proveedor`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`agenda_telefonica_proveedor` (
  `rut_p_fk2` INT(9) NOT NULL ,
  `telefono` INT(13) NOT NULL ,
  INDEX `rut_p_fk2` (`rut_p_fk2` ASC) ,
  CONSTRAINT `rut_p_fk2`
    FOREIGN KEY (`rut_p_fk2` )
    REFERENCES `omohr4`.`proveedor` (`rut_p` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `omohr4`.`camion`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`camion` (
  `vin_c` VARCHAR(17) NOT NULL ,
  `tipo_cabina` VARCHAR(35) NOT NULL ,
  `capacidad` INT(4) NOT NULL ,
  PRIMARY KEY (`vin_c`) ,
  INDEX `vin_c` (`vin_c` ASC) ,
  CONSTRAINT `vin_c`
    FOREIGN KEY (`vin_c` )
    REFERENCES `omohr4`.`vehiculo` (`vin` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `omohr4`.`auto`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `omohr4`.`auto` (
  `vin_a` VARCHAR(17) NOT NULL ,
  `nro_pasajeros` INT(1) NOT NULL ,
  PRIMARY KEY (`vin_a`) ,
  INDEX `vin_a` (`vin_a` ASC) ,
  CONSTRAINT `vin_a`
    FOREIGN KEY (`vin_a` )
    REFERENCES `omohr4`.`vehiculo` (`vin` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;
  #3 (permalink)  
Antiguo 09/06/2012, 15:16
webankenovi
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Error al conectar PHP con BBDD

en primer lugar undefined index logicamente es por que tienes los valores recogidos por post vacios debes verificar

y no coinciden las variables de $host y $hostname ambas deben llamarse igual por eso no conecta

Código PHP:
Ver original
  1. <?
  2.  
  3. if($_POST['nombre'])
  4.  
  5. $host="localhost";
  6. $username="root";
  7. $password="xxxxxxx";   //CLAVE PUESTA EN XAMPP EN LA PARTE DE CHEQUEO DE SEGURIDAD
  8. $db="omohr4";
  9.  
  10. $nombre=$_POST['nombre'];
  11. $apellido=$_POST['apellido'];
  12. $rut_c=$_POST['rut_c'];
  13. $calle_nro=$_POST['calle_nro'];
  14. $comuna=$_POST['comuna'];
  15. $region=$_POST['region'];
  16.  
  17. mysql_connect($host,$username,$password);
  18. $query="SELECT rut_c, fecha, id_orden_pedido
  19.        FROM orden_pedido";
  20. $query2 = "INSERT INTO `empleado` (`rut_c`, `nombre`, `apellido`, `calle_nro`, `comuna`, `region`) VALUES ('$rut_c','$nombre','$apellido','$calle_nro','$comuna','$region')";
  21. $registro=mysql_query($query2);
  22.  
  23. echo "Historial de ordenes de pedido hechas por clientes:";
  24.  
  25. $result=mysql_query($query);
  26.  
  27. }
  28.  
  29. ?>
  #4 (permalink)  
Antiguo 09/06/2012, 15:41
 
Fecha de Ingreso: noviembre-2008
Mensajes: 23
Antigüedad: 15 años, 5 meses
Puntos: 0
Respuesta: Error al conectar PHP con BBDD

Gracias, no me había fijado lo del $host.

Acerca de las variables POST... no sé como registrar en $nombre por ejemplo, lo registrado en el campo "Nombre:" para registar clientes y lo mismo con los otros campos.

Lo del POST, sólo lo vi en un tutorial, si está muy mal puesto es porque sé muy poco de php y html.
  #5 (permalink)  
Antiguo 09/06/2012, 15:46
webankenovi
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Error al conectar PHP con BBDD

no esta mal puesto solo te falta la verificacion ya que tu haces las consultas en la misma pagina que envias por eso en la parte de php

Código PHP:
Ver original
  1. if($_POST['nombre']){ esto sirve para verificar que existen las variables mandadas por el formulario y no te tire error
  2.  
  3.  
  4. aqui el codigo y si no as enviado nada , no entra en esta parte por lo cual no te dara fallo de variables indefinidas
  5.  
  6.  
  7.  
  8. }


probaste el codigo tal te lo di??? te tira algun error???
  #6 (permalink)  
Antiguo 09/06/2012, 15:59
 
Fecha de Ingreso: noviembre-2008
Mensajes: 23
Antigüedad: 15 años, 5 meses
Puntos: 0
Respuesta: Error al conectar PHP con BBDD

Cita:
Iniciado por webankenovi Ver Mensaje


probaste el codigo tal te lo di??? te tira algun error???
Sí, usé este código:
Código:
<!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=utf-8" />
<title>Untitled Document</title>
</head>



<body>

<form action="../../Users/Oliver/Desktop/intentos php/insert.php" method="post">
<b>Formulario de registro de Cliente</b> <br><br>

Primer nombre: <input type="text" name="nombre"><br>
Primer apellido: <input type="text" name="apellido"><br>
RUT: <input type="text" name="rut_c"><br>
Calle y número: <input type="text" name="calle_nro"><br>
Comuna: <input type="text" name="comuna"><br>
Región: <input type="text" name="region"><br>

<input type="submit" value="Registrar">
</form>



<?
if($_POST['nombre'])

$host="localhost";
$username="root";
$password="cocodrilo";
$db="omohr4";

$nombre=$_POST['nombre'];
$apellido=$_POST['apellido'];
$rut_c=$_POST['rut_c'];
$calle_nro=$_POST['calle_nro'];
$comuna=$_POST['comuna'];
$region=$_POST['region'];

mysql_connect($host,$username,$password);
@mysql_select_db($db);
$query="SELECT rut_c, fecha, id_orden_pedido 
	   FROM orden_pedido";
$query2 = "INSERT INTO `empleado` (`rut_c`, `nombre`, `apellido`, `calle_nro`, `comuna`, `region`) VALUES ('$rut_c','$nombre','$apellido','$calle_nro','$comuna','$region')";
$registro=mysql_query($query2);

echo "Historial de ordenes de pedido hechas por clientes:";

$result=mysql_query($query);
 

?>

</body>
</html>
Y denuevo imprime hasta el botón registrar, pero luego salen estas líneas de error

Notice: Undefined index: nombre in C:\xampp\htdocs\aers2.php on line 28

Notice: Undefined index: nombre in C:\xampp\htdocs\aers2.php on line 35

Notice: Undefined index: apellido in C:\xampp\htdocs\aers2.php on line 36

Notice: Undefined index: rut_c in C:\xampp\htdocs\aers2.php on line 37

Notice: Undefined index: calle_nro in C:\xampp\htdocs\aers2.php on line 38

Notice: Undefined index: comuna in C:\xampp\htdocs\aers2.php on line 39

Notice: Undefined index: region in C:\xampp\htdocs\aers2.php on line 40

Notice: Undefined variable: host in C:\xampp\htdocs\aers2.php on line 42

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\aers2.php on line 42
Historial de ordenes de pedido hechas por clientes:
  #7 (permalink)  
Antiguo 09/06/2012, 16:05
webankenovi
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Error al conectar PHP con BBDD

te falta cerrar el if con las llaves {}

Código PHP:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. </head>
  7.  
  8.  
  9.  
  10. <body>
  11.  
  12. <form action="../../Users/Oliver/Desktop/intentos php/insert.php" method="post">
  13. <b>Formulario de registro de Cliente</b> <br><br>
  14.  
  15. Primer nombre: <input type="text" name="nombre"><br>
  16. Primer apellido: <input type="text" name="apellido"><br>
  17. RUT: <input type="text" name="rut_c"><br>
  18. Calle y número: <input type="text" name="calle_nro"><br>
  19. Comuna: <input type="text" name="comuna"><br>
  20. Región: <input type="text" name="region"><br>
  21.  
  22. <input type="submit" value="Registrar">
  23. </form>
  24.  
  25.  
  26.  
  27. <?php
  28.  
  29. if(isset($_POST['nombre'])){
  30.  
  31. $host="localhost";
  32. $username="root";
  33. $password="cocodrilo";
  34. $db="omohr4";
  35.  
  36. $nombre=$_POST['nombre'];
  37. $apellido=$_POST['apellido'];
  38. $rut_c=$_POST['rut_c'];
  39. $calle_nro=$_POST['calle_nro'];
  40. $comuna=$_POST['comuna'];
  41. $region=$_POST['region'];
  42.  
  43. mysql_connect($host,$username,$password);
  44. $query="SELECT rut_c, fecha, id_orden_pedido
  45.        FROM orden_pedido";
  46. $query2 = "INSERT INTO `empleado` (`rut_c`, `nombre`, `apellido`, `calle_nro`, `comuna`, `region`) VALUES ('$rut_c','$nombre','$apellido','$calle_nro','$comuna','$region')";
  47. $registro=mysql_query($query2);
  48.  
  49. echo "Historial de ordenes de pedido hechas por clientes:";
  50.  
  51. $result=mysql_query($query);
  52.  
  53. }
  54. ?>
  #8 (permalink)  
Antiguo 09/06/2012, 16:10
 
Fecha de Ingreso: noviembre-2008
Mensajes: 23
Antigüedad: 15 años, 5 meses
Puntos: 0
Respuesta: Error al conectar PHP con BBDD

Perfecto, ahora no imprimió las líneas de error. Pero al apretar el botón registrar, luego de llenar los campos, me muestra el siguiente error:

Objecto no localizado!

El URL solicitado no ha sido localizado en este servidor. El URL de la página que lo refirió parece ser equivocado u obsoleto. Por favor comunique al autor de esa página acerca del error.

Por favor contacte con el webmaster en caso de que usted crea que existe un error en el servidor.

Error 404

localhost
06/09/12 18:06:15
Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
  #9 (permalink)  
Antiguo 09/06/2012, 16:15
webankenovi
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Error al conectar PHP con BBDD

la url del action del formulario parece que la tienes mal puesta , tu mandas los datos del formulario a la misma pagina no??

pues añadimos una linea y veamos que sucede modificamos la linea 12

Código PHP:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. </head>
  7.  
  8.  
  9.  
  10. <body>
  11.  
  12. <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
  13. <b>Formulario de registro de Cliente</b> <br><br>
  14.  
  15. Primer nombre: <input type="text" name="nombre"><br>
  16. Primer apellido: <input type="text" name="apellido"><br>
  17. RUT: <input type="text" name="rut_c"><br>
  18. Calle y número: <input type="text" name="calle_nro"><br>
  19. Comuna: <input type="text" name="comuna"><br>
  20. Región: <input type="text" name="region"><br>
  21.  
  22. <input type="submit" value="Registrar">
  23. </form>
  24.  
  25.  
  26.  
  27. <?php
  28.  
  29. if(isset($_POST['nombre'])){
  30.  
  31. $host="localhost";
  32. $username="root";
  33. $password="cocodrilo";
  34. $db="omohr4";
  35.  
  36. $nombre=$_POST['nombre'];
  37. $apellido=$_POST['apellido'];
  38. $rut_c=$_POST['rut_c'];
  39. $calle_nro=$_POST['calle_nro'];
  40. $comuna=$_POST['comuna'];
  41. $region=$_POST['region'];
  42.  
  43. mysql_connect($host,$username,$password);
  44. $query="SELECT rut_c, fecha, id_orden_pedido
  45.        FROM orden_pedido";
  46. $query2 = "INSERT INTO `empleado` (`rut_c`, `nombre`, `apellido`, `calle_nro`, `comuna`, `region`) VALUES ('$rut_c','$nombre','$apellido','$calle_nro','$comuna','$region')";
  47. $registro=mysql_query($query2);
  48.  
  49. echo "Historial de ordenes de pedido hechas por clientes:";
  50.  
  51. $result=mysql_query($query);
  52.  
  53. }
  54. ?>
  55.  
  56. </body>
  57. </html>
  #10 (permalink)  
Antiguo 09/06/2012, 16:22
 
Fecha de Ingreso: noviembre-2008
Mensajes: 23
Antigüedad: 15 años, 5 meses
Puntos: 0
Respuesta: Error al conectar PHP con BBDD

Cita:
Iniciado por webankenovi Ver Mensaje
la url del action del formulario parece que la tienes mal puesta , tu mandas los datos del formulario a la misma pagina no??

pues añadimos una linea y veamos que sucede modificamos la linea 12

Código PHP:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. </head>
  7.  
  8.  
  9.  
  10. <body>
  11.  
  12. <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
  13. <b>Formulario de registro de Cliente</b> <br><br>
  14.  
  15. Primer nombre: <input type="text" name="nombre"><br>
  16. Primer apellido: <input type="text" name="apellido"><br>
  17. RUT: <input type="text" name="rut_c"><br>
  18. Calle y número: <input type="text" name="calle_nro"><br>
  19. Comuna: <input type="text" name="comuna"><br>
  20. Región: <input type="text" name="region"><br>
  21.  
  22. <input type="submit" value="Registrar">
  23. </form>
  24.  
  25.  
  26.  
  27. <?php
  28.  
  29. if(isset($_POST['nombre'])){
  30.  
  31. $host="localhost";
  32. $username="root";
  33. $password="cocodrilo";
  34. $db="omohr4";
  35.  
  36. $nombre=$_POST['nombre'];
  37. $apellido=$_POST['apellido'];
  38. $rut_c=$_POST['rut_c'];
  39. $calle_nro=$_POST['calle_nro'];
  40. $comuna=$_POST['comuna'];
  41. $region=$_POST['region'];
  42.  
  43. mysql_connect($host,$username,$password);
  44. $query="SELECT rut_c, fecha, id_orden_pedido
  45.        FROM orden_pedido";
  46. $query2 = "INSERT INTO `empleado` (`rut_c`, `nombre`, `apellido`, `calle_nro`, `comuna`, `region`) VALUES ('$rut_c','$nombre','$apellido','$calle_nro','$comuna','$region')";
  47. $registro=mysql_query($query2);
  48.  
  49. echo "Historial de ordenes de pedido hechas por clientes:";
  50.  
  51. $result=mysql_query($query);
  52.  
  53. }
  54. ?>
  55.  
  56. </body>
  57. </html>
Lo que quiero es que al apretar registrar los datos se vallan a guardar a la tabla empleado de mi BBDD 'omohr4'. Por eso la línea de casi al final que dice mysql_query($query2)...

Creo que ese $registro está demás.... o no?
  #11 (permalink)  
Antiguo 09/06/2012, 16:27
webankenovi
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Error al conectar PHP con BBDD

segun como lo tienes los datos se guardan en la tabala empleado , la consulta que esta de mas es la de $query que es la que haces un select , pero que en realidad no te sirve de nada

prueba asi y me comentas los cambios que hecho son en la linea 46 he concatenado las variables

Código PHP:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. </head>
  7.  
  8.  
  9.  
  10. <body>
  11.  
  12. <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
  13. <b>Formulario de registro de Cliente</b> <br><br>
  14.  
  15. Primer nombre: <input type="text" name="nombre"><br>
  16. Primer apellido: <input type="text" name="apellido"><br>
  17. RUT: <input type="text" name="rut_c"><br>
  18. Calle y número: <input type="text" name="calle_nro"><br>
  19. Comuna: <input type="text" name="comuna"><br>
  20. Región: <input type="text" name="region"><br>
  21.  
  22. <input type="submit" value="Registrar">
  23. </form>
  24.  
  25.  
  26.  
  27. <?php
  28.  
  29. if(isset($_POST['nombre'])){
  30.  
  31. $host="localhost";
  32. $username="root";
  33. $password="cocodrilo";
  34. $db="omohr4";
  35.  
  36. $nombre=$_POST['nombre'];
  37. $apellido=$_POST['apellido'];
  38. $rut_c=$_POST['rut_c'];
  39. $calle_nro=$_POST['calle_nro'];
  40. $comuna=$_POST['comuna'];
  41. $region=$_POST['region'];
  42.  
  43. mysql_connect($host,$username,$password);
  44.  
  45. $query = "INSERT INTO `empleado` (`rut_c`, `nombre`, `apellido`, `calle_nro`, `comuna`, `region`) VALUES ('".$rut_c."','".$nombre."','".$apellido."','".$calle_nro."','".$comuna."','".$region."')";
  46. $registro=mysql_query($query);
  47.  
  48. echo "Historial de ordenes de pedido hechas por clientes:";
  49.  
  50.  
  51.  
  52. }
  53. ?>
  54.  
  55. </body>
  56. </html>
  #12 (permalink)  
Antiguo 09/06/2012, 16:32
 
Fecha de Ingreso: noviembre-2008
Mensajes: 23
Antigüedad: 15 años, 5 meses
Puntos: 0
Respuesta: Error al conectar PHP con BBDD

Con ese código, al apretar registar, sale

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\aers2.php on line 43
  #13 (permalink)  
Antiguo 09/06/2012, 16:37
webankenovi
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Error al conectar PHP con BBDD

vale entonces ya te llegaron los datos bien a la pagina solo que no te los ingresa por que existe en un error en el usuario de la bd na mas ese es el warning comprueba el nombre la contraseña todo que este correcto y que ese usuario tiene privilegios para acceder a esa tabla aunque root tiene que tener privilegios comprueba la contraseña

o crea un usuario nuevo para esa bd en concreto y prueba haber
  #14 (permalink)  
Antiguo 09/06/2012, 19:27
 
Fecha de Ingreso: noviembre-2008
Mensajes: 23
Antigüedad: 15 años, 5 meses
Puntos: 0
Respuesta: Error al conectar PHP con BBDD

Mmm, la clave estoy seguro que está bien.

Cómo hago otro usuario? tengo montada la base en XAMPP.
Y de seguro que ese usuario tiene los permisos, es el root.
  #15 (permalink)  
Antiguo 09/06/2012, 19:39
 
Fecha de Ingreso: noviembre-2008
Mensajes: 23
Antigüedad: 15 años, 5 meses
Puntos: 0
Respuesta: Error al conectar PHP con BBDD

A todo esto, le cambié la $password a "" y ahí no me tira error, pero los datos no llegan a la base.

También vi el código de fuente de la página y no sale el script php:

Código:
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

 

 

 

<body>

 

<form action="" method="post">

<b>Formulario de registro de Cliente</b> <br><br>

 

Primer nombre: <input type="text" name="nombre"><br>

Primer apellido: <input type="text" name="apellido"><br>

RUT: <input type="text" name="rut_c"><br>

Calle y número: <input type="text" name="calle_nro"><br>

Comuna: <input type="text" name="comuna"><br>

Región: <input type="text" name="region"><br>

 

<input type="submit" value="Registrar">

</form>

 

 

 

Historial de ordenes de pedido hechas por clientes: 

</body>

</html>
  #16 (permalink)  
Antiguo 10/06/2012, 00:11
 
Fecha de Ingreso: noviembre-2008
Mensajes: 23
Antigüedad: 15 años, 5 meses
Puntos: 0
Respuesta: Error al conectar PHP con BBDD

alguna ayuda?
:(
  #17 (permalink)  
Antiguo 10/06/2012, 08:53
webankenovi
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Error al conectar PHP con BBDD

el codigo php no se muestra en el codigo fuente ;)

prueba quitando las comillas

Código PHP:
Ver original
  1. $query = "INSERT INTO empleado (rut_c,nombre,apellido,calle_nro,comuna,region) VALUES ('".$rut_c."','".$nombre."','".$apellido."','".$calle_nro."','".$comuna."','".$region."')";
  2. $registro=mysql_query($query);

Etiquetas: bbdd, formulario, html, mysql, registro, sql, variables
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 19:23.