0

I'm still new to php and this is what I've come up with after looking at many tutorials online. I have a basic submit form and I just want to store the data in the database. When I submit it and I check my database, there's nothing there.

<form action="insert.php" method="post">     
Value1: <input type="text" name="field1-name" />

<input type="Submit" /></form>

And the php file:

<?php 
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

$field1-name =  mysqli_real_escape_string ( $conn , $_POST['field1-name'])

$sql = "INSERT INTO db (APP_NAME)
VALUES ('$field1-name')";

if ($conn->query($sql) === TRUE) {
    echo "OK";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
}
?>
1

2 Answers 2

2

There are a few things wrong here.

Firstly, this variable is invalid $field1-name.

PHP is interpreting that as doing math, such as "field1 MINUS name".

Use an underscore $field1_name and for all instances for it in your code.

and use error reporting.

Sidenote:

in order to be 100% sure of a successful query/insert and in conjunction with your error checking.

It's better than if ($conn->query($sql) === TRUE) but that's a matter of preference I guess.

You also have a missing semi-colon in

$field1-name =  mysqli_real_escape_string ( $conn , $_POST['field1-name'])
                                                                          ^ right there

which error reporting would have caught as a parse error.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.


Footnotes:

To ensure that your field isn't empty, use empty().

I.e.: if(!empty($_POST['field1-name'])){...}

The ! means "not", as in "if not empty".

Sign up to request clarification or add additional context in comments.

Comments

0

The Problem

The real reason this script is failing to record anything into the database is because at line 7, you have a line of code that tells the script to die if there is an error in the database connection:

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);

But what if the connection was successful? We look for the CLOSING BRACES for the if ($conn->connect_error) statement, which is found at line 22:

    }
?>

But instead of doing things that should happen if the connection is successful, this script ends immediately after the closing brace to the code block for the if ($conn->connect_error) statement. In other words, nothing happens.

The Fix

Remove the closing bracket at line 22, below the $conn->close(); statement; and place it instead immediately after the die(); statement.

Next, fix a few syntax errors:

  1. You need a semicolon after the mysqli_real_escape_string() statement.
  2. Don't use '-' (dashes or minus signs) in variable names. Remove them or replace them with the more conventional underscore character (_).
  3. Your SQL syntax is incorrect. Try something like this:

    $sql = "INSERT INTO table_name ('APP_NAME') VALUES ('".$field1."')";

    Because you have already connected to your desired database ($dbname = "db";), SQL only needs to know the table to use within that database. Replace table_name from my example as appropriate. It'll be the table with a column called APP_NAME where it appears you want to store the form's post data. Finally, for the VALUES(), it is best to use the ('".$field1."') format rather than ('$field1') because it is more explicit. The latter will cause you problems under certain circumstances.

My rewrite of your code looks like this:

<?php
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
    $field1 =  mysqli_real_escape_string ( $conn , $_POST['field1']);

$sql = "INSERT INTO table_name ('APP_NAME') VALUES ('".$field1."')";

if ($conn->query($sql) === TRUE) {
    echo "OK";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.