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

时间:2018-03-24 08:52:54

标签: php html mysql database phpmyadmin

我有一个用户可以注册其他帐户的表单。它工作正常,直到我将列date的数据类型更改为数据类型date(我使用varchar所以我将其更改为日期)。更改数据类型后,注册停止工作。我没有收到错误但在尝试查看记录时无法看到新帐户。

这是我的表格:



<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>
&#13;
&#13;
&#13;

这里是code_exec.php

&#13;
&#13;
<?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);
	
?>
&#13;
&#13;
&#13;

数据库架构:

DB Schema

2 个答案:

答案 0 :(得分:1)

正如其他人已经说过的那样,您的日期格式可能不正确。而且您需要考虑保护您的查询以防止SQL注入。

为了让您解决日期问题,请尝试替换:

$date=$_POST['date'];

使用:

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

sql的日期格式描述为YYYY-MM-DD,表示四位数的年份 - 两位数月份 - 两位数日。

答案 1 :(得分:0)

您需要从输入date转换收到的日期:

$dt = \DateTime::createFromFormat('m/j/Y', $_POST['date']);

请参阅converted

此外,正如@Syscall所说,您还应该注意您的SQL注入开放的查询。为了防止这种情况,您应该使用PDO语句,例如:

$stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name');

$stmt->execute(array('name' => $name));

取自this StackOverflow answer for more informations

的示例