在插入代码的一部分上获取SQL语法错误

时间:2014-09-23 14:30:49

标签: php mysql sql

我目前正在制作一个PHP提交表单,允许将记录添加到phpmyadmin数据库中。我已经创建了这个代码并且整夜都在努力解决它的错误

当我输入表单中字段的名称(对于虚拟数据)时,我得到了这个:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近'密码','名字','姓氏','地址','电子邮件','卡   数字','CCV')'在第1行

<!DOCTYPE html>
<html>
<head><title>Insert Users</title></head>
<body>
<h2>Insert User Confirmation</h2>

<form action="<?php $_SERVER['PHP_SELF']?>" method="post"/> <br> 
<?php
    require_once('connection.php');

    echo    "<label for='memberID' >Member ID:</label>";
    echo    "<input type='text' name='memberID' id='memberID' />";
    echo    "<br /><br />"; 

    echo    "<label for='username' >Username:</label>";
    echo    "<input type='text' name='username' id='username' />";
    echo    "<br /><br />"; 

    echo    "<label for='password' >Password:</label>";
    echo    "<input type='password' name='password' id='password' />";
    echo    "<br /><br />"; 

    echo    "<label for='fName' >Firstname:</label>";
    echo    "<input type='text' name='fName' id='fName' />";
    echo    "<br /><br />";     

    echo    "<label for='lName' >Lastname:</label>";
    echo    "<input type='text' name='lName' id='lName'  />";
    echo    "<br /><br />"; 

    echo    "<label for='address' >Address:</label>";
    echo    "<input type='text' name='address' id='address'  />";
    echo    "<br /><br />"; 

    echo    "<label for='email' >Email:</label>";
    echo    "<input type='text' name='email' id='email'  />";
    echo    "<br /><br />"; 

    echo    "<label for='cardnumber' >Card Number:</label>";
    echo    "<input type='text' name='cardnumber' id='cardnumber'  />";
    echo    "<br /><br />"; 

    echo    "<label for='ccv' >CCV:</label>";
    echo    "<input type='text' name='ccv' id='ccv'  />";
    echo    "<br /><br />"; 

    echo    "<input type='submit' name='submit' value='Submit' />";
    echo    "<input type='reset' value='Clear' />";
    echo    "<br /><br />"; 
?>
</form>


<?php
    if(!isset($_POST['submit'])) {  
        echo 'Please Register';
    }
    else {
        $memberID = $_POST['memberID'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $fName = $_POST['fName'];
        $lName = $_POST['lName'];
        $address = $_POST['address'];
        $email = $_POST['email'];
        $cardnumber = $_POST['cardnumber'];
        $ccv = $_POST['ccv'];

        $query = "INSERT INTO `members` (MemberID, Username, Password, FirstName, LastName,     StreetAddress, Email, CardNumber, CCV) VALUES ('$memberID', '$username, '$password', '$fName', '$lName', '$address', '$email', '$cardnumber', '$ccv' )";
        mysqli_query($connection, $query)
        or  die(mysqli_error($connection));

        $rc = mysqli_affected_rows($connection);
        if ($rc==1)   
                    {
                    echo '<h4>The database has been updated with the following details: </h4> ';
                    echo 'MemberID: '.$memberID.'<br />';
                    echo 'Username: '.$username.'<br />';
                    echo 'Password: '.$password.'<br />';
                    echo 'First Name: '.$firstname.'<br />';
                    echo 'Last Name: '.$lastname.'<br />';
                    echo 'Address: '.$address.'<br />';
                    echo 'Email: '.$email.'<br />';
                    echo 'Card Number: '.$cardnumber.'<br />';
                    echo 'CCV: '.$ccv.'<br />';
                    }
        else
                    {   
                    echo '<p>The data was not entered into the database this time.</p>';   
                    }
    }

?>
</body>
</html>

2 个答案:

答案 0 :(得分:4)

由于声誉太低,我无法添加评论,所以我在这里回答:

你有没有注意到失踪的&#39;在&#39; $ username?

之后

尝试

$query = "INSERT INTO `members` (MemberID, Username, Password, FirstName, LastName,     StreetAddress, Email, CardNumber, CCV) VALUES ('$memberID', '$username', '$password', '$fName', '$lName', '$address', '$email', '$cardnumber', '$ccv' )";

答案 1 :(得分:-1)

更改插入内容,我认为你做错了

INSERT INTO membersMemberIDUsernamePasswordFirstNameLastNameStreetAddress,{{1 },EmailCardNumber)VALUES('$ memberID','$ username','$ password','$ fName','$ lName','$ address','$ email ','$ cardnumber','$ ccv')