Error in PHP Post

0

I'm doing the final project about forms in my Web programming subject, but when I press the register button it shows me an error that says: Can not POST /register.php

Here is my HTML code

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
    body {
        color: darkblue;
        background: -webkit-linear-gradient(top,  rgba(125,185,232,1) 71%,rgba(125,185,232,1) 99%); 
        


        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='lightblue', endColorstr='lightblue',GradientType=0 );
        font-family: "Century Gothic";
        font-size: 16px;
        line-height: 1.6;
}
    
    .form-input {
	display: block;
	width: 200px;
	height: 15px;
	margin-bottom: 15px;
	color: #000;
	border: 1px solid #999;
	box-shadow: inset 0 0 1px rgba(0, 0, 0, 0.7), 0 1px 0 rgba(255, 255, 255, 0.1);
	-moz-transition: all 0.4s ease-in-out;
	-webkit-transition: all 0.4s ease-in-out;
	-o-transition: all 0.4s ease-in-out;
	-ms-transition: all 0.4s ease-in-out;
	transition: all 0.4s ease-in-out;
	behavior: url(PIE.htc);
	font-family: "Century Gothic";
	font-size: 14px;
	padding-top: 5px;
	padding-right: 10px;
	padding-bottom: 5px;
	padding-left: 10px;
}
</style>
<title></title>
</head>

<body>
<div align='center'>
  <form action="registro.php" method="POST">
  <h2><em>Formulario de Registro</em></h2>  
     <p>
      <label for="Id_Pro"> <span>Id Proyecto</span></label>
      <input type="number" name="Id_Pro" class="form-input" required/>  
         
      <label for="nombre">Nombre <span><em>(requerido)</em></span></label>
      <input type="text" name="nombre" class="form-input" required/>   
      
      <label for="departamento"> <span>Departamento</span></label>
      <input type="text" name="departamento" class="form-input" required/>  
         
      <label for="encargado"> <span>Encargado</span></label>
      <input type="text" name="encargado" class="form-input" required/>  
         
      <label for="costo"> <span>Costo</span></label>
      <input type="number" name="costo" class="form-input" required/>
         
     <center> <input class="form-btn" name="submit" type="submit" value="Registrar" /></center>
    </p>
  </form>
</div>
</body>
</html>

Here is my PHP code

<?php
$db_host="localhost";
$db_user="root";
$db_password="";
$db_name="proyectos";
$db_table_name="proyecto";
   $db_connection = mysql_connect($db_host, $db_user, $db_password);

if (!$db_connection) {
	die('No se ha podido conectar a la base de datos');
}
$subs_Id_Pro = utf8_decode($_POST['Id_Pro']);
$subs_nombre = utf8_decode($_POST['nombre']);
$subs_departamento = utf8_decode($_POST['departamento']);
$subs_encargado = utf8_decode($_POST['encargado']);
$subs_costo = utf8_decode($_POST['costo']);

$resultado=mysql_query("SELECT * FROM ".$db_table_name." WHERE Id_Pro = ".$subs_Id_Pro." and Nombre= '".$subs_nombre."'", $db_connection);

if (mysql_num_rows($resultado)>0)
{

header('Location: Fail.html');

} else {
	
	$insert_value = 'INSERT INTO '' . $db_name . ''.''.$db_table_name.'' ('Id_Pro' , 'Nombre' , 'Departamento' , 'Encargado' , 'Costo' ,) VALUES ("' . $subs_Id_Pro . '", "' . $subs_nombre . '", "' . $subs_departamento . ', "' . $subs_encargado . ', "' . $subs_costo . '")';

mysql_select_db($db_name, $db_connection);
$retry_value = mysql_query($insert_value, $db_connection);

if (!$retry_value) {
   die('Error: ' . mysql_error());
}
	
header('Location: Success.html');

}

mysql_close($db_connection);

		
?>
    
asked by Romeo 11.11.2018 в 05:29
source

1 answer

0

First of all, I want to tell you that I am also a student, so I hope I can solve the doubt. In principle, the error that gives you seems to be by the address you enter in the action="registro.php" , make sure you enter the route correctly. On the other hand, the mysql extension is almost no longer used, I think it will no longer be supported. You should use MySQLi .

<?php
$db_host="localhost";
$db_user="root";
$db_password="";
$db_name="proyectos";
$db_table_name="proyecto";

$db_connection = new mysqli($db_host, $db_user, $db_password, $db_name);
if (!$db_connection) {
    die('No se ha podido conectar a la base de datos');
}

$subs_Id_Pro = utf8_decode($_POST['Id_Pro']);
$subs_nombre = utf8_decode($_POST['nombre']);
$subs_departamento = utf8_decode($_POST['departamento']);
$subs_encargado = utf8_decode($_POST['encargado']);
$subs_costo = utf8_decode($_POST['costo']);


$resultado = $db_connection->query("SELECT * FROM $db_table_name WHERE Id_Pro = $subs_Id_Pro and Nombre = '$subs_nombre'");

if ($resultado->num_rows > 0 ) {
    header('Location: Fail.html');
} else {
  $insert_value=$db_connection->query("INSERT INTO $db_table_name(Id_Pro, 
  Nombre, Departamento, Encargado, Costo) VALUES($subs_Id_Pro, 
  '$subs_nombre', '$subs_departamento',                                              
  '$subs_encargado', $subs_costo)");

    if($insert_value){  
    header('Location: Success.html');
    }else{
      //CÓDIGO EN CASO QUE NO SE PUEDA HACER EL INSERT.
    }
}

$db_connection->close();

The part of utf8_decode(); I would omit it, because if you have correctly configured the database you should not have any problem.

I do not know how you will have the BBDD but I have created one, sensing by the data that you put the fields and it has worked perfectly for me. Above all, make sure of the route.

I hope I helped you. Greetings.

    
answered by 11.11.2018 в 06:57