Problem to insert data in a database from PHP

0

Good morning everyone!

Well, my problem is the one mentioned in the title. From a form in php I'm trying to get the data I collect inserted in a bbdd, and I can not find the fault.

First HTML document called "index.html":

<doctype HTML>
<html>
	<head>
		<meta charset='utf-8'>
		<title>Tarefa UD03</title>
		<!--<link rel="stylesheet" type="text/css" href="estilos.css"> -->
		<style>

			body{

				margin: 0 auto;
			}

			table{

				padding-top: 10px;
				padding-bottom: 20px;
			}


			h1{

				margin-top: 100px;
				margin-left: 600px;
				font-family: "Tahoma", "Geneva", sans-serif;
				font-size: 30px;
				text-decoration: underline;
			}

			th{

					font-family: "Tahoma", "Geneva", sans-serif;
					font-size: 15px;
			}

			td{

				font-family: "Tahoma", "Geneva", sans-serif;
				font-size: 15px;
			 }

			table{

				margin: 0 auto;
			}

			#contenedor1{

				margin: 0 auto;
				width: 1200px;
				height: 105px;
				background-color: #F5F6CE;
				border: 2px solid black;
			}

			.caja_text{

				font-family: "Tahoma", "Geneva", sans-serif;
				font-size: 15px;
			}

			.anho{

				font-family: "Tahoma", "Geneva", sans-serif;
				font-size: 15px;
				width: 100%;
			}

			#enviar{

				margin-top: 50px;
				margin-left: 580px;
				font-family: "Tahoma", "Geneva", sans-serif;
				font-size: 15px;
				background-color: #FFF;
				border: 1px solid #000;
			}
			
		</style>
	</head>
	<body style='background-color: #E6E6E6;'>
		<h1>CICLOS</h1>
		<form name='formulario' id='formulario' method='post' action='conexion_bd.php'>
			<div id='contenedor1'>
				<table>
					<tr>
						<th>Nome:</th>
						<td><label for='textfield'></label>
						<input type='text' name='nombre' id='nombre' class='caja_text' placeholder='Ex: Xoan' maxlength='15'></td>
						<th>Apelido 1:</th>
						<td><label for='textfield'></label>
						<input type='text' name='apellido1' id='apellido1' class='caja_text' placeholder='Ex: Rodríguez' maxlength='15'></td>
						<th>Apelido 2:</th>
						<td><label for='textfield'></label>
						<input type='text' name='apellido2' id='apellido2' class='caja_text' placeholder='Ex: Pérez' maxlength='15'></td>
						<th>Ano Inicio:</th>
						<td><label for='textfield'></label>
						<input type='text' name='anho_in' id='anho_in' class='anho' min='' max='' placeholder='Ex: 2014'></td>
					</tr>
					<tr>
						<th>Ciclo:</th>
						<td>
							<select name='nom_ciclo' id='nom_ciclo'>
								<option>Desarrollo de Apps Web</option>
								<option>Desarrollo de Apps Multiplataforma</option>
								<option>Admin. de Sistemas Informáticos</option>
							</select>
						</td>
						<th>Curso:</th>
						<td><label for='textfield'></label>
						<input type='radio' name='curso' id='curso' class='caja_text' value='1'>1º
						<input type='radio' name='curso' id='curso' class='caja_text' value='2'>2º</td>
						<th>Grupo:</th>
						<td>
							<select name='grupo' id='grupo'>
								<option>A</option>
								<option>B</option>
								<option>C</option>
								<option>D</option>
							</select>
						</td>
					</tr>
				</table>
			</div>
			<input type='submit' id='enviar' name='enviar' value='Enviar este formulario'/>
		</form>
	</body>
</html>

Second document called "conexion_bd.php".

This is the HTML code:

    

<head>
    <meta charset='utf-8'>
    <title>Tarefa UD03</title>
    <!--<link rel="stylesheet" type="text/css" href="estilos.css"> -->
    <style>
        body {
            margin: 0 auto;
        }
        table {
            padding-top: 10px;
            padding-bottom: 20px;
        }
        h1 {
            margin-top: 100px;
            margin-left: 600px;
            font-family: "Tahoma", "Geneva", sans-serif;
            font-size: 30px;
            text-decoration: underline;
        }
        th {
            font-family: "Tahoma", "Geneva", sans-serif;
            font-size: 15px;
        }
        td {
            font-family: "Tahoma", "Geneva", sans-serif;
            font-size: 15px;
        }
        table {
            margin: 0 auto;
        }
        #contenedor1 {
            margin: 0 auto;
            width: 1200px;
            height: 105px;
            background-color: #F5F6CE;
            border: 2px solid black;
        }
        .caja_text {
            font-family: "Tahoma", "Geneva", sans-serif;
            font-size: 15px;
        }
        .anho {
            font-family: "Tahoma", "Geneva", sans-serif;
            font-size: 15px;
            width: 100%;
        }
        #enviar {
            margin-top: 50px;
            margin-left: 580px;
            font-family: "Tahoma", "Geneva", sans-serif;
            font-size: 15px;
            background-color: #FFF;
            border: 1px solid #000;
        }
        #atras {
            margin-top: 50px;
            margin-left: 640px;
            font-family: "Tahoma", "Geneva", sans-serif;
            font-size: 15px;
            background-color: #FFF;
            border: 1px solid #000;
        }
    </style>
</head>

<body style='background-color: #E6E6E6;'>
    <h1>CICLOS</h1>
    <form name='formulario' id='formulario' method='post' action=''>
        <div id='contenedor1'>
            <table>
                <tr>
                    <th>Nome:</th>
                    <td>
                        <label for='textfield'></label>
                        <input type='text' name='nombre' id='nombre' class='caja_text' placeholder='Ex: Xoan' maxlength='15'>
                    </td>
                    <th>Apelido 1:</th>
                    <td>
                        <label for='textfield'></label>
                        <input type='text' name='apellido1' id='apellido1' class='caja_text' placeholder='Ex: Rodríguez' maxlength='15'>
                    </td>
                    <th>Apelido 2:</th>
                    <td>
                        <label for='textfield'></label>
                        <input type='text' name='apellido2' id='apellido2' class='caja_text' placeholder='Ex: Pérez' maxlength='15'>
                    </td>
                    <th>Ano Inicio:</th>
                    <td>
                        <label for='textfield'></label>
                        <input type='text' name='anho_in' id='anho_in' class='anho' min='' max='' placeholder='Ex: 2014'>
                    </td>
                </tr>
                <tr>
                    <th>Ciclo:</th>
                    <td>
                        <select name='nom_ciclo' id='nom_ciclo'>
                            <option>Desarrollo de Apps Web</option>
                            <option>Desarrollo de Apps Multiplataforma</option>
                            <option>Admin. de Sistemas Informáticos</option>
                        </select>
                    </td>
                    <th>Curso:</th>
                    <td>
                        <label for='textfield'></label>
                        <input type='radio' name='curso' id='curso' class='caja_text' value='1'>1º
                        <input type='radio' name='curso' id='curso' class='caja_text' value='2'>2º</td>
                    <th>Grupo:</th>
                    <td>
                        <select name='grupo' id='grupo'>
                            <option>A</option>
                            <option>B</option>
                            <option>C</option>
                            <option>D</option>
                        </select>
                    </td>
                </tr>
            </table>
        </div>
        <input type='submit' id='atras' name='atras' value='Atrás' onclick='history.back()' />
    </form>
</body>

</html>

and this the php code:

<?php

session_start();

$db_conexion = "localhost";
$db_usuario  = "root";
$db_pass     = "";
$db_nombre   = "ciclos";

$conexion = mysqli_connect($db_conexion, $db_usuario, $db_pass, $db_nombre);

if (!$conexion) {

    die("Erro de conexión: " . mysqli_connect_error());
}

if (isset($_POST["enviar"])) {

    $nombre = $_POST["nombre"];
    $apel1  = $_POST["apellido1"];
    $apel2  = $_POST["apellido2"];
    $anho   = $_POST["anho_in"];
    $ciclo  = $_POST["nom_ciclo"];
    $curso  = $_POST["curso"];
    $grupo  = $_POST["grupo"];

    if ($nombre == "" || $apel1 == "" || $apel2 == "" || $anho == "" || $ciclo == "" || $curso == "" || $grupo == "") {

        echo ("<script type='text/javascript'>alert('Erro.')</script>");

    } else {

        mysqli_query($conexion, "INSERT INTO usuarios (nombre, apellido1, apellido2, anho_inicio, nom_ciclo, curso, grupo) VALUES ($nombre, $apel1, $apel2, $anho, $ciclo, $curso, $grupo)");

        //mysqli_query($query);
    }
}

?>
    
asked by Jopimar 24.11.2016 в 16:30
source

2 answers

1

It is likely that the error is that in the INSERT you are passing values string and you have not included comillas .

Try making this change:

mysqli_query($conexion, "INSERT INTO usuarios 
(nombre, apellido1, apellido2, anho_inicio, nom_ciclo, curso, grupo) VALUES 
('$nombre', '$apel1', '$apel2', '$anho', '$ciclo', '$curso', '$grupo')");

//

Keep in mind that:

  

mysqli_query : Returns FALSE in case of error. If a query of type SELECT, SHOW, DESCRIBE or EXPLAIN is successful, mysqli_query () will return an object mysqli_result . For other successful queries of mysqli_query () you will return TRUE .

To know if it gave an error and why, you should modify your script in the following way:

$resultado = mysqli_query($conexion, "INSERT INTO usuarios (nombre, apellido1, apellido2, anho_inicio, nom_ciclo, curso, grupo) VALUES ($nombre, $apel1, $apel2, $anho, $ciclo, $curso, $grupo)");
if ($resultado === false) {
  printf("Error: %s\n", mysqli_error($conexion));
  die();
}

Recommended reading:

answered by 24.11.2016 / 19:43
source
0

We go in parts.

  • I do not understand why you have put a call to a JavaScript method at submit .
  • You are waiting for a variable to arrive in $_POST called "send" that will never arrive because you do not send it.

Changing the value of the attribute name of submit to "send" will work (as long as your login credentials to the database are correct).

    
answered by 24.11.2016 в 17:23