使用带有直接插入语句的PDO将数据插入到mysql数据库

时间:2016-02-28 02:09:22

标签: php mysql pdo

好的大家好,我正在努力寻找和测试我的代码工作几天,但我总是遇到错误。问题是用户将插入他们的姓名,姓氏,电子邮件和密码,但是当它被提交时会出错并且错误是:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\register.php on line 9

这是我的register.php:

try {

    $connect = new PDO( "mysql: host = 'localhost';", 'root', '' );
    $connect->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    $sqlQuery = "INSERT INTO users_details.details ( firstname, lastname, email, password ) VALUES (
                 '$_POST['firstName']',
                 '$_POST['lastName']',
                 '$_POST['email']' 
                 '$_POST['password']' )";

    $connect->exec($sqlQuery);
        echo 'Data submitted successfully.';

}

catch(PDOException $e) {

    echo $e->getMessage();

}

然后这是我的数据库结构:http://prntscr.com/a8uc2b

我也可以告诉你我的表格:

<!DOCTYPE html>

    

  <!-- Form Name -->
  <legend>
    Data Submission
  </legend>

  <!-- First Name -->
  <div class="form-group has-feedback">
    <label class="col-md-4 control-label" for="firstName">
    </label>

    <div class="col-md-4">
      <input id="firstName" name="firstName" pattern="[A-z]+" maxlength="12" data-error="Enter valid first name only." placeholder="First Name" class="form-control input-md" required="" type="text">
      <span class="glyphicon form-control-feedback" aria-hidden="true">
      </span>
      <div class="help-block with-errors">
      </div>

    </div>

  </div>

  <!-- Last Name -->
  <div class="form-group has-feedback">
    <label class="col-md-4 control-label" for="lastName">
    </label>

    <div class="col-md-4">
      <input id="lastName" name="lastName" pattern="[A-z]+" maxlength="12" data-error="Enter valid last name only." placeholder="Last Name" class="form-control input-md" required="" type="text">
      <span class="glyphicon form-control-feedback" aria-hidden="true">
      </span>
      <div class="help-block with-errors">
      </div>

    </div>
  </div>

  <!-- E-Mail -->
  <div class="form-group has-feedback">
    <label class="col-md-4 control-label" for="email">
    </label>

    <div class="col-md-4">
      <input id="email" name="email" maxlength="16" placeholder="E-Mail" class="form-control input-md" required="" type="email">
      <span class="glyphicon form-control-feedback" aria-hidden="true">
      </span>
      <div class="help-block with-errors">
      </div>

    </div>
  </div>

  <!-- Password input-->
  <div class="form-group has-feedback">
    <label class="col-md-4 control-label" for="password">
    </label>
    <div class="col-md-4">
      <input id="password" name="password" pattern="[A-z0-9!@#$%^*]+" maxlength="25" placeholder="Password" class="form-control input-md" required="" type="password">
      <span class="glyphicon form-control-feedback" aria-hidden="true">
      </span>
      <div class="help-block with-errors">
      </div>

    </div>
  </div>

  <!-- Password confirm input-->
  <div class="form-group has-feedback">
    <label class="col-md-4 control-label" for="passwordConfirm">
    </label>
    <div class="col-md-4">
      <input id="passwordConfirm" name="passwordConfirm" data-match="#password" placeholder="Confirm Password" class="form-control input-md" required="" type="password">
      <span class="glyphicon form-control-feedback" aria-hidden="true">
      </span>
      <div class="help-block with-errors">
      </div>

    </div>
  </div>

  <!-- Button -->
  <div class="form-group">
    <label class="col-md-4 control-label" for="submit">
    </label>
    <div class="col-md-4">
      <button id="submit" name="submit" class="btn btn-primary" type="submit">
        Submit
      </button>
    </div>
  </div>

</fieldset>

     

是否有可能将数据库中的值作为用户在表单中输入的值?因为当我还需要申报其他事情并使我的代码更长时,我得到OC,是的,我接受建议和批评。我仍然是新人,但我真的很喜欢效率。很多爱&lt; 3

3 个答案:

答案 0 :(得分:2)

你可以试着逃避报价。

示例:

var time=prompt("What time is it? ","");
if(time >=6 && time <=9)

您可以在此Wildfly documentation

中了解有关转义字符的详情

答案 1 :(得分:1)

从错误消息中推断,您的问题出在以下一行:

    $sqlQuery = "INSERT INTO users_details.details ( firstname, lastname, email, password ) VALUES (
                 '$_POST['firstName']',
                 '$_POST['lastName']',
                 '$_POST['email']' 
                 '$_POST['password']' )";

这会导致T_ENCAPSED_AND_WHITESPACE错误,因为使用单引号,PHP会将代码设为'$_POST['']'

此外,您错过了,'$_POST['email']'之间的'$_POST['password']'

更简单,更简洁的方法是将$_POST分配给变量:

$firstname = $_POST['firstName'];
$lastname = $_POST['lastName'];
$email = $_POST['email'];
$password = $_POST['password'];

$sqlQuery = "INSERT INTO users_details.details ( firstname, lastname, email, password ) VALUES ('$firstName', '$lastName', '$email', '$password' )";

此方法不需要使用连接,因此可以最大限度地减少错误。

答案 2 :(得分:0)

我认为下面的解决方案应该有效

$sqlQuery = "INSERT INTO users_details.details ( firstname, lastname, email, password ) VALUES ('".
             $_POST['firstName'] ."','".
             $_POST['lastName'] ."','".
             $_POST['email'] ."','".
             $_POST['password'] ."')";