数据没有使用PHP插入MySQL数据库

时间:2015-11-28 22:36:22

标签: php mysql

嘿伙计们,我一直在拉我的头发试图找出为什么这个PHP代码不起作用。基本上我的数据库和表都设置正确,我试图让用户填写的数据转到mysql数据库。

我一直在谷歌搜索问题几个小时,还没有找到解决方案。当我在页面上单击“提交”时,页面将刷新,看起来数据已提交,但在查询数据库时没有显示任何内容。

<form action="#" method="post">
        <div class="row">
            <h4>Select your school</h4>
            <p>If you can't find it, contact your administrator about signing your school up!</p>

            <div class="dropdown">
                <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">
                    Choose your school from the dropdown menu
                    <span class="caret"></span>
                </button>
                <select title="Select your School" name="School" id="schools">
                    <option value="1">University of Central Florida</option>
                    <option value="2">Seminole State College</option>
                    <option value="3">School of Hard Knocks</option>
                </select>
            </div>
        </div>
        <div class="row">
            <h4>Name</h4>
            <input class="form-group col-lg-4" id="first" name="first" type="text" placeholder="First">
            <input class="form-group col-lg-4" id="last" name="last" type="text" placeholder="Last">
        </div>
        <div class="row">
            <h4>Email Address</h4>
            <input class="form-group col-lg-8" id="email" name="email" type="text" placeholder="ex. Flava.Flav@netscape.com">
        </div>
        <div class="row">
            <h4>Password</h4>
            <input class="form-group col-lg-8" id="password" name="password" type="text" placeholder="ex. Hunter2">
        </div>
        <div class="row">
            <input id="submit" name="submit" type="submit" value="submit" class="btn btn-primary">
        </div>
    </form>
</div> <!-- /container -->

  <?php
  error_reporting(E_ALL);
  if (isset($_POST['submit']))
  {
  $firstName = -1;
  $lastName = -1;
  $email = -1;
  $password = -1;
  $school = -1;
  $dropdown_val = -1;

  $connect=mysqli_connect('localhost','root','yanni123','eventmanager');

  if(mysqli_connect_errno($connect))
  {
  echo 'Failed to connect';
  }

  $firstName = $_POST["first"];
  $lastName = $_POST["last"];
  $email = $_POST["email"];
  $password = $_POST["password"];
  $dropdown_val = $_POST["School"];

  mysqli_query($connect, "INSERT INTO users (idusers, firstName, lastName, password, emailAddress, school)
  VALUES (1, '$firstName', '$lastName', '$password', '$email', '$dropdown_val')");

  mysqli_close($connect);
  }
  ?>

1 个答案:

答案 0 :(得分:2)

取自我的评论:

  

&#34;并且,如果您的idusers列是AUTO_INCREMENT,那将是一个问题。如果您对其他列有其他限制。&#34;

OP:

  

&#34; @ Fred-ii-我在代码中添加了错误处理,它返回了&#34;错误描述:重复输入&#39; 1&#39;关键&#39; PRIMARY&#39; &#34;事实证明我的桌子是错的,我使用自动增量为idusers。如何在查询中将idusers的值保留为空,以便它与现有条目不冲突?我虽然在db遇到冲突的id时应该增加AUTO_INCREMENT?&#34;

使用以下内容并删除第一个条目,或使用''作为idusers AI&#39 d列的值。

mysqli_query($connect, "INSERT INTO users (idusers, firstName, lastName, password, emailAddress, school)
            VALUES ('', '$firstName', '$lastName', '$password', '$email', '$dropdown_val')")
            or die(mysqli_error($connect));
人工智能将独自发生。不,它不会与冲突AI。不是你现在的方式。

奖金回答:

您目前的代码向SQL injection开放。使用mysqli_* with prepared statementsPDOprepared statements

<强>密码

我还注意到您可能以纯文本格式存储密码。不建议这样做。

使用以下其中一项:

其他链接: