0

I have a problem with adding infromatiion to databse using SQL query. I cheked many times query, Tried to change something, but nothing shanged.... Please help me, I will be very grateful.

My HTML form:

<form action="functions.php" method="post">
        <div id="form" class="fleft">
            <div class="field">
                <label for="fname">Имя: </label>
                <input type="text" name="fname"/>
            </div>
            <div class="field">
                <label for="sname">Фамилия: </label>
                <input type="text" name="sname"/>
            </div>
            <div class="field">
                <label for="email">Ваш E-mail: </label>
                <input type="email" name="email"/>
            </div>
            <div class="field">
                <label for="message">Сообщение: </label>
                <input type="text" name="message">
            </div>
            <div class="field">
                <input type="submit" name="submit"/>
            </div>
        </div>
</form>

Functions.php:

<?php
include "db_connect.php";
if(isset($_POST['submit']))
{

    $connection = db_connect();
    if($connection) echo "connect <br>";
    else echo "no connect";

    $fname = $_POST['fname'];
    $sname = $_POST['sname'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    $query = "INSERT INTO `mail` (`id`,`fname`,`sname`,`email`,`message`) VALUES ('',$fname,$sname,$email,$message)";

    $result = mysql_query($query);

    if($result) echo "success!";
    else echo mysql_error();
}
?>

db_connect.php :

<?php
    function db_connect()
        {
            $host = 'localhost';
            $user = 'root';
            $password = '';
            $db = 'web';

            $connection = mysql_connect($host, $user, $password);
            mysql_query("SET NAMES utf8");
            if(!$connection || !mysql_select_db($db)){
                return false;
            }
                return $connection;
        }
?>

DataBase: structure

Error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com,vsjo OK)' at line 1

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
vladys.bo
  • 730
  • 2
  • 11
  • 34
  • 1
    `('','$fname','$sname','$email','$message')` and since `id` is auto_increment, take out the first `(`fname`,`sname`,`email`,`message`)` `('$fname','$sname','$email','$message')` – Funk Forty Niner Feb 11 '14 at 19:15
  • 1
    This is just a tip that may help you a lot. Here you can echo the $querry. Then try to execute it manually or in phpmyadmin sql tab to find the error. `$query = "INSERT INTO mail (fname,sname,email,message) VALUES ('$fname','$sname','$email','$message')";` – knightrider Feb 11 '14 at 19:19

1 Answers1

2

Your VALUES are not escaped, meaning you have no quotes around your VALUES

Use the following:

$query = "INSERT INTO `mail` (`id`,`fname`,`sname`,`email`,`message`) VALUES ('','$fname','$sname','$email','$message')";

However, since your id column is set to AUTO_INCREMENT take out the first values and use the following in its place:

$query = "INSERT INTO `mail` (`fname`,`sname`,`email`,`message`) VALUES ('$fname','$sname','$email','$message')";

The id column will take care of itself.

You should also consider moving over to mysqli_* functions and prepared statements. mysql_* functions are deprecated and will be removed from future releases.

Read the following: How can I prevent SQL injection in PHP?


Should you decide to continue using mysql_* functions:

(Borrowed from https://stackoverflow.com/a/60442/)

$unsafe_variable = $_POST["user-input"];
$safe_variable = mysql_real_escape_string($unsafe_variable);
mysql_query("INSERT INTO table (column) VALUES ('" . $safe_variable . "')");

MySQLi method: Should you decide to use mysqli_* functions later on: (highly recommended)

$fname = mysqli_real_escape_string($connection,$_POST['fname']);
$sname = mysqli_real_escape_string($connection,$_POST['sname']);
$email = mysqli_real_escape_string($connection,$_POST['email']);
$message = mysqli_real_escape_string($connection,$_POST['message']);

Yet, using MySQLi with prepared statement or PDO are also recommended.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • none of those `` are actully needed (but the '' are) –  Feb 11 '14 at 19:34
  • True. Since the OP already had them, I figured I would leave them in place. And it doesn't hurt for the OP to use that convention with the backticks around the table and column names, should the OP ever chooses a reserved word (down the road). @Dagon – Funk Forty Niner Feb 11 '14 at 19:37