将数据插入SQL表时出错

时间:2017-02-10 10:49:46

标签: php mysql sql-server

我在尝试从html表单插入数据时收到此错误。

Error: INSERT INTO uren (aantaluren, projectname, datum) VALUES ('6','dropdown','2017-02-10'
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 2

这是我尝试添加数据的页面代码:

<?php
session_start();
require_once(dirname(__FILE__)."/simpleusers/su.inc.php"); 

$mysqli = mysqli_connect("localhost","root","","urenregistratie"); 
$sqlSelect="SELECT name, projectId FROM projecten";
$result = $mysqli -> query ($sqlSelect);

while ($row = mysqli_fetch_array($result)) {
echo "option value='" . $row['name'] . "'>" . $row['projectId'] . "</option>";
} 

$SimpleUsers = new SimpleUsers();

// This is a simple way of validating if a user is logged in or not.
// If the user is logged in, the value is (bool)true - otherwise (bool)false.
if( !$SimpleUsers->logged_in )
{
    header("Location: login.php");
    exit;
}

// If the user is logged in, we can safely proceed.
$users = $SimpleUsers->getUsers();


if ($mysqli->connect_error) {
die("Connection failed: ". $mysqli->connect_error);
}

if(isset($_POST['new']) && $_POST['new']==1)
{

$aantaluren =$_REQUEST['aantaluren'];
$datum =$_REQUEST['datum'];
$projectname = $_REQUEST['projectname'];

$sql = "INSERT INTO uren (aantaluren, projectname, datum)
VALUES ('$aantaluren','$projectname','$datum'";

if ($mysqli->query($sql) === TRUE) {
    echo "Uren succesvol toegevoegd. <a href='overzicht.php'> Bekijk     overzicht</a>";
} else {
    echo "Error: " . $sql . "<br>" . $mysqli->error;
}}


?>


<!DOCTYPE html>
<html>
<head>
    <title>Toevoegen</title>
    <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div class="form">
            <p>
                <a href="dashboard.php">Dashboard</a> | <a href="view.php">Projecten inzien</a> | <a href="logout.php">Loguit</a>
            </p>

            <div>
                <h1>Voeg uren toe</h1>

                <form action="uren.php" method="post">  
                    <input type="hidden" name="new" value="1" />
                    <p>Aantal uren</p><p>
                        <input name="aantaluren" type="number" min=1 max=24> 
                        </p>
                        <select name="projectname"/>

                        <?php
                    $sql = mysqli_query($mysqli, "SELECT name FROM projecten");
                    while ($row = $sql->fetch_assoc()){
                        echo "<option value=\"dropdown\">" . $row['name'] . "</option>";
                        }
                    ?>

                    </select>
                    <p>Datum</p>
                    <p>
                        <input type="date" name="datum" placeholder="datum" required />
                    </p>
                    <p>
                        <input name="submit" type="submit" value="Voeg toe" />
                    </p>
                </form>

            </div>
        </div>
    </body>
</html>

这些是我的SQL表:

CREATE DATABASE IF NOT EXISTS urenregistratie;

CREATE TABLE IF NOT EXISTS urenregistratie.`users` (
  `userId` int(11) NOT NULL auto_increment,
  `uUsername` varchar(128) NOT NULL,
  `uPassword` varchar(40) NOT NULL,
  `uSalt` varchar(128) NOT NULL,
  `uActivity` datetime NOT NULL,
  `uCreated` datetime NOT NULL,
  PRIMARY KEY  (`userId`),
  UNIQUE KEY `uUsername` (`uUsername`)
    ) ENGINE=MyISAM AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS urenregistratie.`users_information` (
  `userId` int(11) NOT NULL,
  `infoKey` varchar(128) NOT NULL,
  `InfoValue` text NOT NULL,
  KEY `userId` (`userId`)
) ENGINE=MyISAM;

CREATE TABLE IF NOT EXISTS urenregistratie.`projecten`(
    `projectId` int(11) NOT NULL AUTO_INCREMENT, 
    `trn_date` datetime NOT NULL,
    `name` varchar(50) NOT NULL,
    `begindatum` datetime NOT NULL,
    `einddatum` datetime NOT NULL,
    PRIMARY KEY (`projectId`)
    ); 

CREATE TABLE IF NOT EXISTS urenregistratie.`uren`(
    projectname varchar(50),
    aantaluren int(11) NOT NULL,
    datum datetime NOT NULL,
    PRIMARY KEY (projectname)
    )

我现在已经坚持了大约2天,我似乎无法弄明白为什么它不起作用。请帮忙

2 个答案:

答案 0 :(得分:1)

您忘记在insert语句中关闭括号。完成。

格式正确:

INSERT INTO uren()VALUES()

答案 1 :(得分:0)

您没有关闭括号

$sql = "INSERT INTO uren (aantaluren, projectname, datum)
VALUES ('$aantaluren','$projectname','$datum')";

它在您的错误中声明,其语法错误