Newbie
Newbie

Reputation: 72

Insert user input from textbox to Database (PHP to PHPMYADMIN using mysql) Code not working for some reason

For some reason I am getting "Notice: Undefined index: adata" when I try to input data and mysql data file gets a blank space input (no text input just an empty field.

So can you tell me guys what I did wrong, because I watched multiple tutorials, guide and read multiple posts regarding this matter yet still I could not resolve it.

<?php

if (isset($_POST['submit']))
{

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

// Create connection
$dbcon = mysqli_connect($servername, $username, $password, $dbname);
mysql_select_db($db);
// Check connection
    $adata = $_POST['test'];
    $sql = ("INSERT INTO data (data) VALUES ('$adata')");

    if (!mysqli_query($conn, $sqlinsert)) {
        die('error inserting new data');    
    } //end of second if

} //end of if statement


?>
<html>
<head>
<meta charset="UTF-8">
<title>Insert Data Into DB</title>
</head>
<body>
<h1>Insert new data into DB</h1>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Add Data:
<input type="text" name="test">
<br />
<input type="submit" value="add new data" />

</form>

</body>
</html>

Upvotes: 0

Views: 1930

Answers (1)

user3416706
user3416706

Reputation:

Be careful with your variables ;

$adata = $_POST['test'];
$sql = ("INSERT INTO data (data) VALUES ('$adata')");

if (!mysqli_query($conn, $sql)) { // it was $sqlinsert and must be $sql due your definition.
    die('error inserting new data');  

Upvotes: 2

Related Questions