0

I am trying to create PHP form data insert in SQL but getting error. Even when I write code same to same but I'm still getting an error.

<?php

    $un = $_POST['uname'];
    $em = $_POST['email1'];

//with or what out these bellow variables
    $host = "localhost";
   $username = "admin";
   $password = "admin";
   $database = "test1";

    $db = mysqli_connect('$host','$username','$password','$database');
    $query = "INSERT INTO users ('username','password') VALUES ('$un','$em')";
    $rs = mysqli_query($db,$query);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form Registration</title>
</head>
<body>
    <form action="server1.php" method="post">
        <label>Name</label>
        <input type="text" name="uname" required="required">
        <label>Email</label>
        <input type="email" name="email1" required="required">
        <input type="submit" name="submit" value="submit">
    </form>

</body>
</html>
5
  • 2
    What is the error you are getting? Commented May 22, 2018 at 0:02
  • What is "same to same"? Commented May 22, 2018 at 7:11
  • Data is not interring in database when i click on submit. Commented May 22, 2018 at 8:07
  • 1
    Your code is vulnerable to SQL injection attacks. You should use mysqli or PDO prepared statements with bound parameters as described in this post. Commented May 22, 2018 at 8:25
  • can you re-write my codes. Commented May 22, 2018 at 8:34

2 Answers 2

1

The error was "" just inverted comma's, now its works perfectly.

     <?php
        $un = $_POST['uname'];
        $em = $_POST['email1'];

        $host = "localhost";
        $username = "admin";
        $password = "admin";
        $database = "test1";

            $con = mysqli_connect ("$host","$username","$password","$database");
            $query = "insert into users (username,email) values ('$un','$em')";
            $run = mysqli_query ($con,$query);

            if ($run=TRUE){
                echo 'Data Submitted Successfuly';
            }
            else {
                echo 'Error';
            }

    ?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form Registration</title>
</head>
<body>
    <form action="server1.php" method="post">
        <label>Name</label>
        <input type="text" name="uname" required="required">
        <label>Email</label>
        <input type="email" name="email1" required="required">
        <input type="submit" name="submit" value="submit">
    </form>

</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

You can try this way.

$db = mysqli_connect($host, $username, $password) or die ('Unable to connect');
mysqli_select_db($db, $database) or die('Unable to select DB');

4 Comments

same problem cant enter data in database.
Ok then you should change column name within ' ' to ` ` sign within query
Ok i have slightly modify your code and run to me. Please check this link - ideone.com/qLAlX4
same problem no data inserting to database when click on submit even i edited action="" but no inserting any data.

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.