1

I have a formText.php file that contains a form with the following code form code:

<form action="insert.php" method="post">
    <p>
        <label for="theNames">Name:</label>
        <input type="text" name="theName" id="theName">
    </p>
    <p>
        <label for="theCitys">City:</label>
        <input type="text" name="theCity" id="theCity">
    </p>
    <p>
        <label for="theAges">Are you over eighteen?(Y/N)</label>
        <input type="text" name="theAge" id="theAge">
    </p>
    <p>
    <label for="theDates">Date:</label>
    <input type="text" name="theDate" id="theDate">
    </p>
    <input type="submit" value="Submit">
</form>

Then I have an insert.php file with the following script:

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "root","phpteste");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security (EDITED)
$theName = mysqli_real_escape_string($link, $_POST['theName']);
$theCity = mysqli_real_escape_string($link, $_POST['theCity']);
$theAge = mysqli_real_escape_string($link, $_POST['theAge']);
$theDate = mysqli_real_escape_string($link, date("Y-m-d h:i:s",$_POST['theDate']));


// attempt insert query execution
$sql = "INSERT INTO tabelateste (id, name, city, overeighteen, date) VALUES (NULL, '$theName', '$theCity', '$theAge', '$theDate')";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);
?>

My database is called phpteste and my table name is tabelateste. What am I doing wrong here? Whenever I click Submit nothing comes up and nothing gets added to the database.

5
  • Did you get any error? Commented Jan 2, 2016 at 5:50
  • nop, nothing appears on screen simply. Commented Jan 2, 2016 at 5:51
  • You getting incorrect variable name in $_POST Commented Jan 2, 2016 at 5:55
  • First verify that the form generated by formText.php is sending the proper data to insert.php by replacing insert.php with a test script that simply dumps/renders the data POSTed. Then verify that insert.php can actually modify the database by hardcoding it to insert a dummy record in your database. Then get them to work together. Commented Jan 2, 2016 at 5:56
  • your column name is signup_date but in your insert it is date Commented Jan 2, 2016 at 7:03

4 Answers 4

2

Your post data name fields are wrong. SO you need to change below line:

// Escape user inputs for security
$theName = mysqli_real_escape_string($link, $_POST['theName']);
$theCity = mysqli_real_escape_string($link, $_POST['theCity']);
$theAge = mysqli_real_escape_string($link, $_POST['theAge']);
$theDate = mysqli_real_escape_string($link, date("Y-m-d h:i:s",$_POST['theDate']));

You need to change date to signup_date as per your database table structure.

$sql = "INSERT INTO tabelateste (name, city, overeighteen, signup_date) VALUES ('$theName', '$theCity', '$theAge', '$theDate')";
Sign up to request clarification or add additional context in comments.

9 Comments

Chanage sql to: $sql = "INSERT INTO tabelateste (name, city, overeighteen, date) VALUES ('$theName', '$theCity', '$theAge', '$theDate')";
Did you get any error while data inserted into database? Or echo your query and try to run it directly into database.
Still did not work. I had the NULL in there because the id was a NULL value. But yet, no luck. Have no idea what I might be doing wrong.
What is your database structure? Is id auto increment and primary key?
Yes it is. tabelateste (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20), city VARCHAR (20), overeighteen CHAR(1), signup_date DATE);
|
0
$sql = "INSERT INTO tabelateste (`name`, `city`, `overeighteen`, `date`) VALUES ('$theName', '$theCity', '$theAge', '$theDate')";

Use this code

2 Comments

Tried it and didn't fix it. Damn it!
print_r($_POST); use to debug.values are correctly post or not
0

I just tested your code (copied and pasted) and it works perfectly under my server configuration (Windows 10 - PHP 5.6) . My best guess is that you have a typo in either the table name or the MySQL configuration.

If you copied this code from another site. Please check that you created the database and the table , and that the MySQL configuration is correct.

A good to check for this kind of mistakes so is to read the PHP error logs

2 Comments

I'm running Xubuntu 14.04 with PHP 5.5.9 and I'm not getting this to work. What could be wrong?
Can you check the PHP error logs? They tend to be useful
0

Try it like this maybe

if(isset($_POST['submit']) && !empty($_POST) ){

$theName = $_POST['theName'];
$theCity  = $_POST['theCity'];
$theAge = $_POST['theAge'];
$theDate = $_POST['theDate'];

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

    // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);

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



        $sql = "INSERT INTO tabelateste (name, city, overeighteen, date)
        VALUES ('$theName ', '$theCity ', '$theAge ', '$theDate ')";

            if ($conn->query($sql) === TRUE) {
            $last_id = $conn->insert_id;

            } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }

4 Comments

Still returns an empty page. No error but no output nor entry on db.
Do you have an ID on your DB with auto increment?
Yes I do, first column.
I have just taken out id from the sql line. Try that.

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.