2

A HTML form has been created that should (when filled) send the data it's holding to a database inserting a new row so it can be used later on. However, I can't seem to get it to work, I'm getting the following error:

Notice: Use of undefined constant con - assumed 'con' in C:\xampp\htdocs\form\insert.php on line 4

Warning: mysql_query() expects parameter 1 to be string, object given in C:\xampp\htdocs\form\insert.php on line 17
Data not inserted


HTML Code

<!DOCTYPE html>
<html>
    <head>    
        <title>Form linked to database</title>
    </head>
    <body>
        <form action="insert.php" method="post">
            Name: <input type="text" name="username">
            <br>
            Email: <input type="text" name="email">
            <br>
            <input type="submit" value="insert">
        </form>
    </body>
</html>

PHP Code

<?php
$con = mysqli_connect('localhost','[retracted]','[retracted]');

if(!con) {
    echo 'Not connected to server!';
}

if(!mysqli_select_db($con,'tutorial')) {
    echo 'Database not selected!';
}

$Name = $_POST['username'];
$Email = $_POST['email'];

$sql = "INSERT INTO person (Name,Email) VALUES ('$Name','$Email')";
if(!mysql_query($con,$sql)) {
    echo 'Data not inserted';
} else {
    echo 'Data inserted';
}
//header("refresh:2; url=form.html");
?>

I'm new to PHP and followed the following YouTube tutorial.

I'm also using XAMPP for this, on a localhost. Any help is appreciated. Thank you.

Community
  • 1
  • 1
Asad Hussain
  • 119
  • 1
  • 2
  • 5

5 Answers5

3

You should change:

if(!con){
    echo 'Not connected to server!';
}

to:

if(!$con){
    echo 'Not connected to server!';
}

as you're missing a dollar sign there.

Additionally, you're using a mysql_ function here, on the mysqli_ object $con:

if(!mysql_query($con,$sql))

Change this to

if(!mysqli_query($con,$sql))

SQL injection

As your query is vulnerable to SQL injection, then I'd like to recommend you take a look at using prepared statements, or using mysqli_real_escape_string()-- though, this comes with a few gotcha's: https://stackoverflow.com/a/12118602/7374549

Community
  • 1
  • 1
1

You have done two small mistakes ie

1) forgot to add $ before the variable name ie changes is

if(!$con){
        echo 'Not connected to server!';
    }

2) you are connected with mysqli_connect but you are trying to use mysql_query functions in it. so please change and use mysqli_query

if(!mysqli_query($con,$sql)){ }

This is issue in your case. My suggestion is to use mysqli or PDO that is good practice.

Pranav MS
  • 2,235
  • 2
  • 23
  • 50
0

You are not using the correct mySQL query function, you have used:

mysql_query($con

You should use:

mysqli_query

instead. Let me know if you still have issues.

-1

Altough you have a lot of answers right now, I think none of those is the right one. I've written your code new, procedural as you did, but with prepared statements, so you're going to be save to SQL injections.

<?php
    $con = mysqli_connect('localhost','[retracted]','[retracted]');

    if(!$con){
        echo 'Not connected to server!';
    }

    if(!mysqli_select_db($con,'tutorial')){
        echo 'Database not selected!';
    }

    $Name = $_POST['username'];
    $Email = $_POST['email'];

   if ($stmt = mysqli_prepare($con, "INSERT INTO person (Name, Email) VALUES (?, ?"))) {
    mysqli_stmt_bind_param($stmt, "ss", $Name, $Email);
    mysqli_stmt_execute($stmt);
    echo "Data inserted";
   }
   else {
    echo "Error";
   }

mysqli_close($con);

    //header("refresh:2; url=form.html");
?>

I think it should work, if not let me know.

Twinfriends
  • 1,972
  • 1
  • 14
  • 34
-2

Try this :

<?php

// Create connection
$conn = new mysqli("localhost", "username", "password", "databasename");
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('test fname', 'test lname', 'test@example.com')";

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

$conn->close();
?>
Sujal Patel
  • 592
  • 2
  • 5
  • 14
  • 2
    Nice copy paste from W3. At least you editet de standard names. But it won't help the OP, since he's trying to insert data from a HTML form. If you copy paste, then please copy paste an example with prepared statements for a good example. – Twinfriends Jan 05 '17 at 12:03
  • This is just insert query example. i think he's not find in w3. – Sujal Patel Jan 05 '17 at 12:09