elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: ¿Eres nuevo? ¿Tienes dudas acerca del funcionamiento de la comunidad? Lee las Reglas Generales


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web
| | |-+  PHP (Moderador: #!drvy)
| | | |-+  Problemas con datos de formulario que no se graban en la base de datos
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Problemas con datos de formulario que no se graban en la base de datos  (Leído 1,932 veces)
Infodale

Desconectado Desconectado

Mensajes: 3


Ver Perfil
Problemas con datos de formulario que no se graban en la base de datos
« en: 27 Marzo 2018, 17:27 pm »

Hola amigos,

Tengo un pequeño problema que no puedo determinar qué lo produce. Ingreso al sistema (Paso 1) y me redirige a un formulario (Paso 2), ingreso los datos que me pide este formulario pero finalmente no graba en la base lo ingresado. El paso 1 trabaja con una base 1 y el formulario con una base 2. En la base_1 el id es la llave principal y en la base_2 el id es la llave foránea. Me sucede igual si en la base_1 asigno al “email” como llave principal y en la base_2 como llave foránea. Adjunto los scripts de ambos pasos así como el archivo de conexión (config.php). Les agradeceré mucho me den una mano, gracias desde ya.

ARCHIVO CONEXIÓN (config.php)

Código
  1. <meta charset="utf-8" />
  2. <?php
  3.  
  4. error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);
  5.  
  6. define('DB_DRIVER', 'mysql');
  7. define('DB_SERVER', 'localhost');
  8. define('DB_SERVER_USERNAME', 'root');
  9. define('DB_SERVER_PASSWORD', '');
  10. define('DB_DATABASE', 'cofaith');
  11.  
  12. define('SITE_URL', 'http://localhost/sitioPrueba/');
  13.  
  14. $dboptions = array(
  15.    PDO::ATTR_PERSISTENT => FALSE,
  16.    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  17.    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  18.    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
  19. );
  20. try {
  21.  $DB = new PDO(DB_DRIVER . ':host=' . DB_SERVER . ';dbname=' . DB_DATABASE, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, $dboptions);
  22. } catch (Exception $ex) {
  23.  echo $ex->getMessage();
  24.  die;
  25. }
  26.  
  27. ?>

PASO 1: INGRESO AL SISTEMA (inreg1_2.php)

Código
  1. <meta charset="utf-8" />
  2. <?php
  3.  
  4. require("config.php");
  5. if (isset($_SESSION["email"]) || $_SESSION["email"] != "") {
  6.    //if user already logged in
  7.    redirect("logout.php");
  8. }
  9.  
  10. $errMSG = "";
  11. if (isset($_POST["mode"]) && $_POST["mode"] == "login") {
  12.  
  13.    //valiadte uerinput for security checks
  14.    // add addslashes() function to prevent from sql injections
  15.    $uemail = trim($_POST["uemail"]);
  16.    $pass = trim($_POST["upass"]);
  17.    $rem = trim($_POST["remember_me"]);
  18.  
  19.    if ($uemail == "" || $pass == "") {
  20.        $errMSG = errorMessage("Ingrese sus Datos correctamente!");
  21.    } else {
  22.  
  23.        // check if username exist
  24.        $sql = "SELECT * FROM dabas where email = :uemail";
  25.        try {
  26.            $stmt = $DB->prepare($sql);
  27.            $stmt->bindValue(":uemail", $uemail);
  28.            $stmt->execute();
  29.            $emailRS = $stmt->fetchAll();
  30.        } catch (Exception $ex) {
  31.            echo errorMessage($ex->getMessage());
  32.        }
  33.  
  34.        if (count($emailRS) > 0) {
  35.            // user exist
  36.            $sql = "SELECT * FROM dabas where email = :uemail AND pass = :pass";
  37.            try {
  38.                $stmt = $DB->prepare($sql);
  39.                $stmt->bindValue(":uemail", $uemail);
  40.                $stmt->bindValue(":pass", md5($pass));
  41.  
  42.                $stmt->execute();
  43.                $userRS = $stmt->fetchAll();
  44.            } catch (Exception $ex) {
  45.                echo errorMessage($ex->getMessage());
  46.            }
  47.  
  48.            if (count($userRS) > 0 ) {
  49.                // user exist
  50.                // now check if the status of the user
  51.                if ($userRS[0]["status"] == 'approved') {
  52.  
  53.                    $_SESSION["email"] = $userRS[0]["email"];
  54.  
  55.                    if ($rem == 1) {
  56.                        // if user select to remember
  57.                        setcookie("email", $uemail, time() + 7200);
  58.                    } else {
  59.                        setcookie("email", $uemail, time() - 7200);
  60.                    }
  61.  
  62.                    redirect("cuesti2_1.php");
  63.                } else {
  64.                    // user exist but the status is inactive
  65.                    $errMSG = errorMessage("Lo sentimos!!! Tu cuenta está temporalmente suspendida.");
  66.                }
  67.            } else {
  68.                $errMSG = errorMessage("Los Datos ingresados son incorrectos.");
  69.            }
  70.        } else {
  71.            // no user exist with same name
  72.            $errMSG = errorMessage("El Correo@ ingresado no está registrado");
  73.        }
  74.    }
  75. }
  76. $userame = (isset($_COOKIE["userame"]) && $_COOKIE["userame"] != "") ? $_COOKIE["userame"] : "";
  77. ?>
  78. <!DOCTYPE html>
  79. <html>
  80.    <style type="text/css">
  81.    #apDiv3 {
  82. position: absolute;
  83. width: 200px;
  84. height: 115px;
  85. z-index: 1;
  86. left: 745px;
  87. top: 535px;
  88. }
  89.    </style>
  90. <head>
  91.       <link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />
  92.  
  93.        <style type="text/css">
  94.        .letraCampos {
  95. font-family: Arial, Helvetica, sans-serif;
  96. font-size: 16px;
  97. color: #06C;
  98. height: 35px;
  99. width: 300px;
  100. }
  101.        .letraBoton {
  102. font-family: Arial, Helvetica, sans-serif;
  103. font-size: 20px;
  104. color: #009;
  105. height: 40px;
  106. width: 200px;
  107. }
  108.        .letratituolsgrande {
  109. font-family: "Swis721 LtCn BT";
  110. font-size: 24px;
  111. font-weight: bold;
  112. color: #333;
  113. }
  114.        #apDiv1 {
  115. position: absolute;
  116. width: 200px;
  117. height: 115px;
  118. z-index: 1;
  119. }
  120.        #apDiv16 {
  121. position: absolute;
  122. width: 172px;
  123. height: 30px;
  124. z-index: 14;
  125. left: 548px;
  126. top: 506px;
  127. font-family: "Swis721 LtCn BT";
  128. font-size: 18px;
  129. font-weight: normal;
  130. color: #666;
  131. }
  132.        #apDiv18 {
  133. position: absolute;
  134. width: 200px;
  135. height: 26px;
  136. z-index: 25;
  137. left: 550px;
  138. top: 456px;
  139. font-family: "Swis721 LtCn BT";
  140. font-size: 12px;
  141. color: #F00;
  142. }
  143.        .linkLetras {
  144. font-family: "Swis721 LtCn BT";
  145. font-size: 14px;
  146. color: #999;
  147. }
  148.        #apDiv2 {
  149. position: absolute;
  150. width: 271px;
  151. height: 29px;
  152. z-index: 26;
  153. left: 550px;
  154. top: 505px;
  155. font-family: "Swis721 LtCn BT";
  156. font-size: 20px;
  157. font-weight: bold;
  158. color: #009;
  159. }
  160.        .letrasIngreso {
  161. font-family: "Swis721 LtCn BT";
  162. font-size: 18px;
  163. color: #009;
  164. }
  165.        </style>
  166.        <title></title>
  167.  
  168.    </head>
  169.  
  170.    <body>
  171.  
  172.        <div id="container">
  173.  
  174.  
  175.           <div class="container">
  176.  
  177.  
  178.             <header></header>
  179.                <article><br><br>
  180.                    <form method="post" action="" name="form1">
  181.                        <input type="hidden" value="login" name="mode" />
  182.                        <div class="loginbox">
  183.                       </style><div class="container">
  184.  <div class="row" style="display: block; margin-top: 120px; margin-left: 450px; vertical-align: middle; color: #0066CC; font-size: 16px; font-family: Arial, Helvetica, sans-serif; width: 500px; height: 40px; text-align: left;">
  185.  
  186.  <div style="height: 400px; clear: both; text-align: center; border-color: #999; border-bottom-width: thin; border-left-width: thin; border-right-width: thin; border-top-width: thin; border-style: solid; width: 500px; margin-top: -50px;">
  187.  
  188.  <div style="height: 20px;clear: both"></div>
  189.  
  190.                        <p class="letratituolsgrande">Inicio de sesión</p>
  191.  
  192.                        <div style="height: 10px;clear: both"></div>
  193.                          <div class="col_left"></div><div class="col_right"><span class="letraCampos">
  194.                          <input type="text" name="uemail" value="<?php echo $userame; ?>" placeholder="Ingresa tu correo@" maxlength="50" autocomplete="off" class="letraCampos" required />
  195.                          </span></div>
  196.  
  197.                          <div style="height: 20px;clear: both"></div>
  198.  
  199.                          <div class="col_left"></div><div class="col_right"><span class="letraCampos"><span class="letraCampos">
  200.                          <input type="password" name="upass" placeholder="Ingresa tu Contraseña" maxlength="40" value="" class="letraCampos" required />
  201.                            </span></span></div>
  202.                            <div class="height10"></div>
  203.                          <div class="col_left">&nbsp;</div><div class="letraCampos"><label for="remember_me"><input type="checkbox" value="1" name="remember_me" <?php echo ($userame != "") ? 'checked' : ''; ?>>
  204.                            Recordarme </label> </div>
  205.                            <div class="height10"></div>
  206.                          <div class="col_left">&nbsp;</div><div class="col_right"><span class="letraBoton"><span class="letraBoton">
  207.                          <input type="submit" name="sub" value="Ingresar" class="letraBoton" />
  208.  
  209.                          <div class="linkLetras" style="height: 40px;clear: both">
  210.                            <p>¿Olvidaste tu Contraseña?</p>
  211.                            </style>
  212.                          </div>
  213.                          </span></span></div>
  214.                            <div class="height10"></div>
  215.                        </div>
  216.  
  217.                    </form>
  218.                </article>
  219.    </div></div>
  220.        <div id="apDiv3"><a href="index.php">REGÍSTRATE GRATIS AQUÍ</a></div>
  221.    </body>
  222. </html>
  223.  
  224. [b]PASO 2:  INGRESO DATOS AL FORMULARIO (inreg1_2.php)[/b]
  225.  
  226.  
  227. <?php session_start(); ?>
  228.  
  229. <html>
  230. <head>
  231. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  232. <title>Datos form 1 de 4</title>
  233. </head>
  234.  
  235. <body>
  236. <?php
  237.  
  238. //including the database connection file
  239. include_once("config.php");
  240.  
  241. if(isset($_POST['sub'])) {
  242.  
  243. $email = trim($_POST['uemail']);
  244. $pp_yo = trim($_POST['upp_yo']);
  245. $pp_pi = trim($_POST['upp_pi']);
  246.  
  247. $sql = "INSERT INTO profile(email, pp_yo, pp_pi) VALUES(:email, :pp_yo, :pp_pi)";
  248. //$query = $dbConn->prepare($sql);
  249.  
  250.  $stmt = $DB->prepare($sql);
  251.  $stmt->bindValue(':email', $email, PDO::PARAM_STR);
  252.  $stmt->bindValue(':pp_yo', $pp_yo, PDO::PARAM_STR);
  253.      $stmt->bindValue(':pp_pi', $pp_pi, PDO::PARAM_STR);
  254.  $stmt->execute();
  255.  
  256.  echo "<font color='green'>Los Datos ingresaron correctamente";
  257.  echo "<br/><a href='bien_regis.html'></a>";
  258. }
  259.  
  260. ?>
  261.  
  262. <style type="text/css">
  263. .letraCampos {
  264. font-family: Arial, Helvetica, sans-serif;
  265. font-size: 16px;
  266. color: #06C;
  267. height: 35px;
  268. width: 400px;
  269. margin-top: 20px;
  270. text-align: left;
  271. }
  272. .letraBoton {
  273. font-family: Arial, Helvetica, sans-serif;
  274. font-size: 20px;
  275. color: #009;
  276. height: 40px;
  277. width: 300px;
  278. text-align: center;
  279. margin-left: 0px;
  280. }
  281. #apDiv1 {
  282. position: absolute;
  283. width: 200px;
  284. height: 115px;
  285. z-index: 1;
  286. left: 53px;
  287. top: 16px;
  288. }
  289. .ContenedorMargen {
  290. padding: 60px;
  291. }
  292. .letratituolsgrande { color: #333;
  293. font-family: "Swis721 LtCn BT";
  294. font-size: 24px;
  295. font-weight: bold;
  296. text-align: center;
  297. }
  298. .LetrasPequeñas {
  299. font-family: Arial, Helvetica, sans-serif;
  300. font-size: 14px;
  301. color: #666;
  302. }
  303. #apDiv2 {
  304. position: absolute;
  305. width: 200px;
  306. height: 115px;
  307. z-index: 1;
  308. left: 136px;
  309. top: 204px;
  310. font-family: "Swis721 LtCn BT";
  311. font-size: 24px;
  312. }
  313. </style><div class="container">
  314.  <div class="row" style="display: block; margin-top: 120px; margin-left: 450px; vertical-align: middle; color: #0066CC; font-size: 16px; font-family: Arial, Helvetica, sans-serif; width: 500px; height: 40px; text-align: left;">
  315.  
  316.    <div class="col-lg-6">
  317.      <div class="well contact-form-container">
  318.        <form class="form-horizontal contactform" action="cuesti2_1.php" method="post" name="f" onSubmit="return validateForm();">
  319.          <fieldset>
  320.            <div class="form-group">
  321.            <div style="height: 850px; clear: both; text-align: center;">
  322.              <p class="letratituolsgrande">Ingresa los Datos requeridos</p>
  323.  
  324.            <div class="row">
  325.   <div class="col-md-3 col-md-offset-3"></div>
  326.  
  327. <?php echo $_SESSION['email'];?>
  328.  
  329.              <span class="letraCampos">
  330.              <label class="col-lg-12 control-label" for="upp_yo">
  331.                <textarea name="upp_yo" cols="60" rows="7" class="letraCampos" id="upp_yo" placeholder="Cuéntanos sobre ti"></textarea>  
  332.              </label>
  333.             </span><span style="text-align: center"></span></div>
  334.  
  335.            <div class="form-group">
  336.              <label class="letraCampos" for="upp_pi">
  337.                <textarea name="upp_pi" cols="60" rows="7" class="letraCampos" id="upp_pi" placeholder="Describe a la persona que buscas"></textarea>
  338.              </label>
  339.            </div>
  340.  
  341.            <div style="height: 30px;clear: both"></div>
  342.  
  343.            <div class="form-group">
  344.              <div class="col-lg-10">
  345.                <button class="letraBoton" type="submit" name="sub">Graba tus datos</button>
  346.              </div>
  347.              <div style="height: 40px;clear: both"></div>
  348.            </div>
  349.          </fieldset>
  350.        </form>
  351.      </div>
  352.    </div>
  353.  </div>
  354. </div>
  355.  
  356.  
  357. <div id="apDiv2">
  358.  <p>Estás en el primer paso:</p>
  359.  <p>1 de 4</p>
  360. </div>
  361.  
  362.      </body>
  363. </html>[/c
  364. ode]
  365.  
  366.  
  367. [b]Mod: Los temas de PHP van al subforo de PHP.[/b]
  368.  
  369.  
  370.  


« Última modificación: 27 Marzo 2018, 21:18 pm por #!drvy » En línea

Infodale

Desconectado Desconectado

Mensajes: 3


Ver Perfil
Re: Problemas con datos de formulario que no se graban en la base de datos
« Respuesta #1 en: 27 Marzo 2018, 23:36 pm »

ÉSTE ES EL ERROR QUE ME DA:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`cofaith`.`profile`, CONSTRAINT `prof_dab` FOREIGN KEY (`id_prof`) REFERENCES `dabas` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)' in C:\wamp\www\sitioPrueba\cuesti2_1.php on line 28

( ! ) PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`cofaith`.`profile`, CONSTRAINT `prof_dab` FOREIGN KEY (`id_prof`) REFERENCES `dabas` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) in C:\wamp\www\sitioPrueba\cuesti2_1.php on line 28


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines