我正在尝试插入我的SQL数据库。出于某种原因,我收到Incorrect integer value: '' for column 'user_id' at row 1
错误。
这是我的表架构:
如您所见,user_id
是一个引用另一个表中的列的外键。我不确定你是否需要这些信息,但是。我从插入php中收到错误。
这是我的插入php:
<?php
require_once ('database.php');
if (isset($_POST['send'])) {
$user_id = $_POST['user_id'];
$full_name = $_POST['full_name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$fromdate = ($_POST['fromdate']);
$todate = ($_POST['todate']);
$reason = ($_POST['reason']);
$status = ($_POST['status']);
{
$database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$insert_query = "INSERT INTO leaves (user_id, full_name, phone, email, fromdate, todate, reason, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$insert = $database->prepare($insert_query);
$insert->execute(array($user_id, $full_name, $phone, $email, $fromdate, $todate, $reason, $status));
echo "<script>alert('Successfully sent!'); window.location='leaveform.php'</script>";
}
}
?>
形式:
<div class="container">
<div class="card">
<div class="form__name" style="font-family: Questrial; ">
Leave Application Form
</div>
<div class="time__container">
<div class="section">
<div class="box">
1
</div><span>Date</span>
</div>
<form action="insert-leave.php" class="form__time" method="post">
<div class="date">
<label for="date">From</label> <input id="date" type="date" name="fromdate" required>
</div>
<div class="date">
<label for="date">To</label> <input id="date" type="date" name="todate" required>
</div>
<div class="timezone">
<label for="timezone"></label>
</select>
</div>
</div>
<div class="message__container">
<div class="section">
<div class="box">
2
</div><span>Reason</span>
</div>
<textarea cols="50" rows="5" name="reason" required></textarea>
</div>
<div class="contact__container">
<div class="section">
<div class="box">
3
</div><span>Contact Information</span>
</div>
<div class="form__contact">
<div class="cname">
<label for="cname">Name</label> <input name="full_name" type="text" readonly="readonly" value="<?php echo $fullname; ?>" required>
</div>
<div class="cnum">
<label for="cnum">Phone Number</label> <input id="cnum" type="text" readonly="readonly" name="phone" value="<?php echo $contact; ?>" required>
</div>
<div class="email">
<label for="cemail">Email</label> <input id="cemail" name="email" readonly="readonly" value="<?php echo $email; ?>" required>
</div>
<div class="email">
<label for="cemail"></label> <input type="hidden" name="status" required>
</div>
<div class="id">
<label for="id"></label> <input type="hidden" name="user_id" required>
</div>
</div>
</div>
答案 0 :(得分:1)
您永远不会将值设置为user_id
<input type="hidden" name="user_id" required>
所以$_POST['user_id'];
为空
答案 1 :(得分:0)
已添加
<div class="id">
<label for="id"></label> <input type="text" readonly="readonly" value="<?
php echo $user_id; ?>" name="user_id" required>
</div>