Error with PHP "Notice: Undefined index:" and does not connect to DB

0

Good I am trying to save comments in BD but it returns me the error that appears in the title of this question.

I have the following FORM in an index.html

<html>
<head>
</head>
<body>
    <form id="form" action="guarda_comentario.php" method="post">
        <input name="txtalias" value=""/>
        <textarea name="txtcomentario" value=""></textarea>
        <input type="submit" name="Submit"/>
    </form>
</body>

and the next php that is the action of the previous form.

include 'conectar.php';  

$varalias = $_POST['txtalias']; 
$varcomentario = $_POST['txtcomentario'];   

$sql = "INSERT INTO tabla_comentarios (alias, comentario) VALUES ('$varalias', '$varcomentario')"; 

$result=mysqli_query($mysqli, $sql); 
if (!$result){ 
    echo "ERROR: No se pudo guardar la informacion, contacte al administrador."; 
}else{ 
    echo "<b>Información guardada correctamente.</b>"; 
} 

I have already tried changing the variables in which I receive the values by POST A:

$varalias = isset($_POST['txtalias']) ? $_POST['txtalias'] : ''; $varcomentario = isset($_POST['txtcomentario']) ? $_POST['txtcomentario'] : '';

This causes me to stop sending the error "Notice: Undefined index:" otherwise, it returns the line of error that I have codified and contemplated:

echo "ERROR: No se pudo guardar la informacion, contacte al administrador.";

which I can not elucidate because it is not saving the values in BBDD. the file contectar.php below.

<?php

$user='root';
$pass='';
$server='localhost';

$mysqli = new mysqli($server,$user,$pass)  or die ( "no se ha podido conectar a la base de datos" ); 

echo "conectado correctamente";

? >

as you will see, very simple. But if we see the error that returns to me by screen is

"connected correctlyERROR: Could not save the information, contact the administrator."

The first thing that comes up is "Connected correctly" this is the echo of the file conectar.php which this file is working correctly.

Therefore, why do not you capture the values of the form in index.html? Why do not you save them in BBDD?

I await your comments.

Greetings

    
asked by Macali 02.01.2019 в 20:00
source

1 answer

1

You lack the name of the database in your connection file.

<?php
  $user='root';
  $pass='';
  $server='localhost';
  $basedatos= 'nombre_de_tu_base_de_datos';

  $mysqli = new mysqli($server,$user,$pass, $basedatos)  or die ( "no se ha podido conectar a la base de datos" ); 

  echo "conectado correctamente";
?>

link

    
answered by 02.01.2019 / 20:19
source