简体   繁体   English

MySQL查询不会将用户输入添加到表中

[英]MySQL Query Won't Add User Input to Table

I created a 'Create a Profile' form that includes a username and password entry for a user to sign up. 我创建了一个“创建个人资料”表单,其中包含用户名和密码条目供用户注册。 I've set my code to retrieve the value of all form fields. 我设置了代码以检索所有表单字段的值。 I am testing to see if the first name updates to my database, and it does not. 我正在测试以查看名字是否更新到我的数据库,而不会更新。 The message that I set for failed query execution shows up when I submit the form: 'Error querying request'. 提交以下表单时,将显示为查询执行失败设置的消息:“错误查询请求”。 I'm connected to the database properly and have tried reformatting my query string (with the variable with quotes and with out quotes): 我已正确连接到数据库,并尝试重新格式化查询字符串(使用带引号和不带引号的变量):

$query = "INSERT INTO employees(firstName) VALUES ('$first_name')";

I would appreciate any input as to why the query string is not executing properly. 我将不胜感激,为什么查询字符串不能正确执行任何输入。 Counld it be an issue with the formatting or variables? 难道格式或变量有问题吗?

Here is my code (purposely blanked out the values): 这是我的代码(故意使值空白):

<?php

    define('DB_LOCATION', 'x');
    define('DB_USERNAME', 'x');
    define('DB_PASS', 'x');
    define('DB_NAME', 'x');

    $dbc = 0;

    $dbc = mysqli_connect(DB_LOCATION, DB_USERNAME, DB_PASS, DB_NAME)
        or die('Error connecting to database');

if(isset($_POST['submit']))  { 

    $first_name = $_POST['first'];
    $last_name = $_POST['last'];
    $email_address = $_POST['email_address'];
    $user_name = $_POST['user'];
    $user_password = $_POST['pass'];

    $query = "INSERT INTO employees(firstName) VALUES ('$first_name')";

    $result = mysqli_query($dbc, $query)
        or die('Error querying request');

        echo '<p>Congrats, your name has been added to the database</p>';


    }

?>

<!DOCTYPE html>
<head>
    <title></title>
      <meta charset="utf-8">
      <link type="text/css" rel="stylesheet" href="5_Signup_CSS.css">
</head>

<body>

<div class="formFormat">
<form name="signupForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table class="tableFormat">
  <tr>Create an Account</tr>
    <tr>
        <td>First Name:</td><td><input type="text" id="first" name="first" value ="<?php echo $first_name ?>"/></td>
    </tr>

    <tr>
        <td>Last Name:</td><td><input type="text" id="last" name="last" value="<?php echo $last_name ?>" /></td>
    </tr>

    <tr>
        <td>Email:</td><td><input type="text" id="email_address" name="email_address" value="<?php echo $email_address ?>" /></td>
    </tr>

    <tr>
        <td>Username:</td><td><input type="text" id="user" name="user" value="<?php echo $user_name?>" /></td>
    </tr>

    <tr>
        <td>Password:</td><td><input type="text" id="pass" name="pass" value="<?php echo $user_password?>"/></td>
    </tr>
    <tr>
        <td><input type="submit" id="submit" name="submit" /></td>
    </tr>

</table>
</form>
</div>


<?php
    mysqli_close($dbc);
?>

</body>

</html>
One issue that I can see is that you are inserting the $firstname value as literally "$first_name". 我可以看到的一个问题是,您将$ firstname值插入为字面上的“ $ first_name”。 You will want to change it to `('".$first_name."')` `$query = "INSERT INTO employees(firstName) VALUES ('$first_name')";` 您将需要将其更改为`('.. $ first_name。“')``$ query =” INSERT INTO employee(firstName)VALUES('$ first_name')“;`

You may also want to check your table names depending on what exactly the error is that you are getting. 您可能还想检查表名,具体取决于您所得到的确切错误。

Also you do not need to define $dbc before calling the mysqli_connect. 另外,在调用mysqli_connect之前不需要定义$ dbc。

Also you may want to look at changing from mysqli to PDO. 另外,您可能想看看从mysqli更改为PDO。 From my experience PDO is much easier to use. 根据我的经验,PDO易于使用。

Edit: Errors You may want to wrap all of your slq queries in a try catch statement, that way you can catch all exceptions that are thrown and then decide how to display them 编辑:错误您可能希望将所有slq查询都包装在try catch语句中,这样就可以捕获所有抛出的异常,然后决定如何显示它们

PHP Try / Catch PHP尝试/捕获

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM