dpz7935 2018-03-24 08:52
浏览 147
已采纳

无法使用表单在数据库中插入值

I have a form where users can register other accounts. It was working fine until I changed the data type of the column date to data type date (I was using varchar so I changed it to date). After changing the datatype, the registration stopped working. I don't get an error but I can't see the new account when I try to view the records.

Here's my form:

<div class="main">
  <div class="one">
    <div class="register">
      <center><h3>Add Account</h3></center>
      <form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
        <div>
          <label>ID</label>
          <input type="text" name="id" required>
        </div>
        <div>
          <label>First Name</label>
          <input type="text" name="firstname" required>
        </div>
        <div>
          <label>Last Name</label>
          <input type="text" name="lastname" required>
        </div>
        <div>
          <label>Email</label>
          <input type="text" name="email" placeholder="user@teamspan.com" required>
        </div>
        <div>
          <label>Username</label>
          <input type="text" name="username" required>
        </div>
        <div>
          <label>Password</label>
          <input type="password" name="password" required>
        </div>
        <div>
          <label>Street Address</label>
          <input type="text" name="street" required>
        </div>
        <div>
          <label>Town/Suburb</label>
          <input type="text" name="town" required>
        </div>
        <div>
          <label>City</label>
          <input type="text" name="city" required>
        </div>
        <div>
          <label>Contact</label>
          <input type="text" name="contact" required>
        </div>
        <div>
          <label>Gender</label>
            <select name="gender" required>
                <option disabled selected hidden>Select Gender</option>
                <option value="Male">Male</option>
                <option value="Female">Female</option>
            </select>
        </div>
        <div>
          <label>User Levels</label>
            <select name="user_levels" required>
                <option disabled selected hidden>Select Access Level</option>
                <option value="0">Employee</option>
                <option value="1">Administrator</option>
                <option value="2">Manager</option>
                <option value="1">HR</option>
            </select>
        </div>
        <div>
          <label>Date</label>
          <input type="text" readonly="readonly" name="date" value="<?php echo date("m/j/Y");?>" required>
        </div>
        <div>
          <label>Sick Leave</label>
          <input type="text" name="sickleave" required>
        </div>
        <div>
          <label>Vacation Leave</label>
          <input type="text" name="vacationleave" required>
        </div>
        <div>
          <label>Picture (Link)</label>
          <input type="text" name="picture" value="img/emp/" required>
        </div>
        <div>
          <label></label>
          <input type="submit" name="submit" value="Add Account" class="button" style="color: white;" />
          <a href="hr_panel.php"><input type="button" value="Back" class="button" style="color: white;" />
        </div>
      </form>
    </div>
  </div>

And here's code_exec.php

<?php

    session_start();
     
    include('connection.php');

    $id=$_POST['id']; 
    $username=$_POST['username'];
    $firstname=$_POST['firstname'];
    $lastname=$_POST['lastname'];
    $email=$_POST['email'];
    $street=$_POST['street'];
    $town=$_POST['town'];
    $city=$_POST['city'];
    $contact=$_POST['contact'];
    $gender=$_POST['gender'];
    $password=$_POST['password'];
    $user_levels=$_POST['user_levels'];
    $date=$_POST['date'];
    $picture=$_POST['picture'];
    $sickleave=$_POST['sickleave'];
    $vacationleave=$_POST['vacationleave'];
     
    mysqli_query($bd, "INSERT INTO employee(id, firstname, lastname, username, email, street, town, city, contact, gender, password, user_levels, date, picture, sickleave, vacationleave) 
                VALUES ('$id', '$firstname', '$lastname', '$username', '$email', '$street', '$town', '$city', '$contact', '$gender', '$password', '$user_levels', '$date', '$picture', '$sickleave', '$vacationleave')");
     
    echo "<script>alert('Successfully Added!'); window.location='register.php'</script>";
     
    mysqli_close($con);
    
?>

Database Schema:

DB Schema

</div>
  • 写回答

2条回答 默认 最新

  • duanhe1976 2018-03-24 09:03
    关注

    As others have already stated, your date format may not be correct. And you need to look at securing your queries against sql injection.

    In order to get you date issue fixed try replacing:

    $date=$_POST['date'];
    

    With:

    $date=date('Y-m-d', strtotime($_POST['date']));
    

    The Date format for sql is described as YYYY-MM-DD meaning a four digit year-two digit month - two digit day.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?