-3

I don't understand why my data is not inserting on the database. Please help me. i use a single page where html and php are on the same page. i tried it so many times but data is not inserting.

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Quickstart pack</title>
    </head>
    <body>

    <form action="index.php" method="post">
        Name: <input type="text" name="name"/></br>
        Email: <input type="text" name="email"/></br>
        Phone: <input type="text" name="phone"/></br>
        <input type="submit" value="submit"/>
    </form>

<?php
if(isset($_POST['submit'])) {
    $conn = mysql_connect("localhost", "root", "");
    if (!$conn) {
        die("mysql is not connected" . mysql_error());
    }
    $data = mysql_select_db("customerInfo", $conn);

    $sql = "INSERT INTO customer (Name, Email_id, Phone) VALUES ('$_POST[name]','$_POST[email]','$_POST[phone]')";
    mysql_query($sql, $conn);

    mysql_close($conn);
}
?>
    </body>
</html>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • Are you getting any php errors? – James Jones Jan 14 '16 at 13:37
  • 7
    So many things are wrong here.. Please stop using **mysql_*** functions as they are deprecated, start using **PDO** or **mysqli_***. You are also wide open to SQL injection, so please start using **prepared statements** – Naruto Jan 14 '16 at 13:37
  • @Naruto. Calm down bud. I'll bet you a tenner this isn't for a live site. – James Jones Jan 14 '16 at 13:41
  • 2
    @JamesJones Why would you even assume I'm not calm? And second of all: O hey it's just a test, let's just encourage people to keep using crap even in test?! It's double work to rewrite the code again to something descent.. – Naruto Jan 14 '16 at 13:43
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 14 '16 at 14:06
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Jan 14 '16 at 14:06
  • 2
    Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard Jan 14 '16 at 14:11

3 Answers3

2
<input type="submit" value="submit"/>

Replace with:

<input type="submit" name="submit" value="submit"/>  // missing name attribute.

You are checking if(isset($_POST['submit'])) but there is no any elemnt with name submit.

Shailesh Katarmal
  • 2,757
  • 1
  • 12
  • 15
  • 2
    To be fair, once the OP does this there are many other errors which need correction. This single change will not fix the problem. – Jay Blanchard Jan 14 '16 at 14:08
2

1) Missing Name Attribute in submit button.

2) Change $_POST[textfieldName] to $_POST['textfieldName']

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Quickstart pack</title>
    </head>
    <body>

    <form action="index.php" method="post">
        Name: <input type="text" name="name"/></br>
        Email: <input type="text" name="email"/></br>
        Phone: <input type="text" name="phone"/></br>
        <input type="submit" value="submit" name='submit'/>
    </form>

<?php
if(isset($_POST['submit'])) {
    $conn = mysql_connect("localhost", "root", "");
    if (!$conn) {
        die("mysql is not connected" . mysql_error());
    }
    $data = mysql_select_db("customerInfo", $conn);

    $sql = "INSERT INTO customer (Name, Email_id, Phone) VALUES ('".$_POST['name']."','".$_POST['email']."','".$_POST['phone']."')";
    mysql_query($sql, $conn);

    mysql_close($conn);
}
?>
    </body>
</html>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
1

Use

if ($_SERVER['REQUEST_METHOD'] == 'POST') { // fired after form post

instead of:

if(isset($_POST['submit'])) { // fired after submit hit

Further, you're using mysql_ functions. These are deprecated (php 5.x) / removed (php 7). Use mysqli_ or PDO instead.

schellingerht
  • 5,726
  • 2
  • 28
  • 56