0

I want to insert some data from a form to a database table "sumation". But it's not working. I use PhpStorm IDE and it's shows no data sources are configured to run this sql and sql dialect is not configured. Where is the problem ?

<?php
    $db= new PDO('mysql:host=localhost;dbname=test;cahrset=utf8','root','');
    if(isset($_POST['submit'])){
        $id=$_POST['id'];
        $first=$_POST['first'];
        $second=$_POST['second'];
        $third=$_POST['third'];

        $sql="INSERT INTO sumation VALUES($id,'$first','$second','$third')";
        $db->query($sql);
        echo("<script>alert('Data Inserted Sucessfully !')</script>");
    }

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
        ID: <input type="text" name="id"><br>
        First: <input type="text" name="first"><br>
        Second: <input type="text" name="second"><br>
        Third: <input type="text" name="third"><br>
        <button type="submit" class="btn-primary" name="submit">Insert </button>
    </form>

</body>
</html>
6
  • 1
    getting any errors?? Commented Nov 26, 2015 at 15:02
  • I also suggest escaping dangerous characters in the user input or you will be vulnerable to SQL injection attacks. Commented Nov 26, 2015 at 15:03
  • No errors. Even the javascript code inside the echo is working. But it don't insert any data in database table. Commented Nov 26, 2015 at 15:05
  • 3
    cahrset, you probably meant charset, injection here are pretty at risk.. AND turn on error reporting to report error? Commented Nov 26, 2015 at 15:05
  • What can I do @Parsa Akbari Commented Nov 26, 2015 at 15:07

4 Answers 4

1

Your query is wrong, the syntax of INSERT is

INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)

So your query would look like

INSERT INTO sumation (id, first, second, third) VALUES ($id, '$first', '$second', '$third')

You also just assume that your query is executed. A PDO query would return an object on success, and boolean false on failure, meaning that you could wrap it into an if-statement.

You should also read up on How can I prevent SQL-injection in PHP?, which basically means that you should use prepared statements.

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

Comments

0

Please try

$sql="INSERT INTO sumation VALUES($id,'$first','$second','$third')";

Just replace

$sql="INSERT INTO sumation (id,first,second,third) VALUES ($id,'$first','$second','$third')";

Comments

0

This should work:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $id=$_POST['id'];
    $first=$_POST['first'];
    $second=$_POST['second'];
    $third=$_POST['third'];

    $conn = new mysqli('localhost', 'root', '', 'test');
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql="INSERT INTO sumation (id,first,second,third) VALUES ($id,'$first','$second','$third')";

    if ($conn->query($sql) === TRUE) {
        echo("<script>alert('Data Inserted Sucessfully !')</script>");
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<form method="post">
    ID: <input type="text" name="id"><br>
    First: <input type="text" name="first"><br>
    Second: <input type="text" name="second"><br>
    Third: <input type="text" name="third"><br>
    <button type="submit" class="btn-primary" name="submit">Insert </button>
</form>

</body>
</html>

Comments

0

To properly answer your question on how to protect your application from SQL injection attacks.

An SQL injection attack is where a user inserts SQL commands into their input string allowing them to run SQL queries on your database. This means they can drop the whole database or print out all the rows.

You can use the PDO quote function.

$id=$db->quote($_POST['id']);
$first=$db->quote($_POST['first']);
$second=$db->quote($_POST['second']);
$third=$db->quote($_POST['third']);

Alternatively I would recommend you use PDO prepare and execute functions read documentation here: http://php.net/manual/en/pdo.prepare.php

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.