Parse error: syntax error, unexpected (T_VARIABLE)

2

Help, I'm just learning PHP and HTML I can not find the error even if I already search like 1000 times

The mistake that marks me is on line 11 and it is this:

Parse error: syntax error, unexpected '$ Name' (T_VARIABLE) in C: \ AppServ \ www \ insertadatos.php on line 11

<head>
<title> Creacion de portal PHP y MySQL </title>
</head>
<h2> Formulario para insertar datos </h2>
<?php
$host = "127.0.0.1";
$user = "root";
$password = 1;
$db = "Farmacia";

$Nombre=$_POST['Nombre'];
$Accion = $_POST['Accion'];
$IDLab = $_POST['IDLab'];

$enlace = mysql_connect($host, $user, $password);
mysql_select_db($db, $enlace);
$result = mysql_query("insert into Medicamentos(Nombre, Accion, IDLab) value 
('$Nombre','$Accion','$IDLab')", $enlace);
echo "Ha insertado los siguientes datos";
echo "<br><br>";
echo "Nombre del medicamento.......................: $Nombre";
echo "<br>";
echo "Accion del medicamento.......................: $Accion";
echo "<br>";
echo "ID del Laboratorio...........................: $IDLab";
echo "<br>";
echo "<br><br>";
?>

<a href = "http://localhost/Formularioinserta.htm"> Volver

the name of the file where the variables are acquired is: Forminserted.htm and contain this code:              Creation of PHP and MySQL portal        

    <form name = "form" action = "insertadatos.php" methos "post">
    <h2> Formulario para insertar datos </h2>
    <h5> Introduce el nombre del medicamento...........:
    <input name = "Nombre" type = "text" size = "45">
    <br><br>
    Introduce la accion del medicamento................:
    <input name = "Accion" type = "text" size = "45">
    <br><br>
    Introduce el ID del laboratorio....................:
    <input name = "IDLab" type = "text" size = "45">
    <br><br>
    </h5>
    <input name = "Enviar" type = "submit" value = "Enviar">
 </form>
    
asked by Jonas Aja 15.12.2018 в 02:16
source

1 answer

1

I propose this code with all the corrected errors and some improvements. The POST variables must be controlled in some way. Here I use a ternary operator combined with empty .

PHP

<head>
<title> Creacion de portal PHP y MySQL </title>
</head>
<h2> Formulario para insertar datos </h2>
<?php
    $Nombre= ( empty($_POST['Nombre']) ) ? NULL : $_POST['Nombre'];
    $Accion= ( empty($_POST['Accion']) ) ? NULL : $_POST['Accion'];
    $IDLab=  ( empty($_POST['IDLab'])  ) ? NULL : $_POST['IDLab'];

    if ($Nombre && $Accion && $IDLab){
        $host = "127.0.0.1";
        $user = "root";
        $password = "";
        $db = "Farmacia";

        $enlace = mysql_connect($host, $user, $password);
        mysql_select_db($db, $enlace);
        $sql="insert into Medicamentos(Nombre, Accion, IDLab) values ('$Nombre','$Accion','$IDLab')";
        $result = mysql_query($sql, $enlace);
        $msg="Ha insertado los siguientes datos";
        $msg.="<br><br>";
        $msg.="Nombre del medicamento.......................: $Nombre";
        $msg.="<br>";
        $msg.="Accion del medicamento.......................: $Accion";
        $msg.="<br>";
        $msg.="ID del Laboratorio...........................: $IDLab";
        $msg.="<br>";
        $msg.="<br><br>";

    }else{

        $msg="Algunos de los datos POST están vacíos";  

    }
    echo $msg;
?>

HTML

You must correct the form like this:

<form name = "form" action = "insertadatos.php" method="post">
  

NOTE ABOUT SECURITY : The mysql_* extension you use has been declared obsolete. You should as soon as possible move to PDO or to   MySQLi. The insertion managed in this way is insecure, you can   sneak malicious code. For more details see the following   question: Why should not the mysql_ * API be used in PHP / MySQL?

    
answered by 15.12.2018 / 03:19
source