我创建了一个提交到mysql服务器的简单表单。当我按下提交按钮时,表单正在发送数据,但是当我检查sql数据库时,它显示没有数据。
这是php代码
<?php
require('db.php');
// If form submitted, insert values into the database.
if (isset($_POST['submit'])){
$user_id = $_POST['user_id'];
$type = $_POST['optionsRadiosInline'];
$qty = $_POST['qty'];
$de_add = $_POST['de_add'];
$re_add = $_POST['re_add'];
$sub = $_POST['sub'];
$time_deli = $_POST['time'];
$cost = $_POST['cost'];
$reg_date = date("Y-m-d H:i:s");
$query = "INSERT into `orders` (user_id, type, qty, de_add, re_add, sub, time_deli, cost, reg_date) VALUES ('$type','$qty','$de_add','$re_add','$sub','$time_deli','$cost', '$reg_date')";
$result = mysql_query($query);
if($result){
echo "<div class='form'><h3>You are registered successfully.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
}
}else{
?>
这是html表单部分
<form role="form" name="new_order" action="" method="post">
<div class="form-group">
<label>User ID</label>
<input class="form-control" name="user_id" value="<?php echo $_SESSION["username"]; ?>" readonly>
</div>
<div class="form-group">
<label>Type of Dabba : </label>
<label class="radio-inline">
<input type="radio" name="optionsRadiosInline" id="optionsRadiosInline1" value="xl" checked>X-Large
</label>
<label class="radio-inline">
<input type="radio" name="optionsRadiosInline" id="optionsRadiosInline2" value="l">Large
</label>
<label class="radio-inline">
<input type="radio" name="optionsRadiosInline" id="optionsRadiosInline3" value="m">Midiam
</label>
<label class="radio-inline">
<input type="radio" name="optionsRadiosInline" id="optionsRadiosInline3" value="s">Small
</label>
</div>
<div class="form-group">
<label>Quantity</label>
<input class="form-control" name="qty" placeholder="Enter text">
</div>
<div class="form-group">
<label>Destination Address</label>
<input class="form-control" name="de_add" placeholder="Enter text">
</div>
<div class="form-group">
<label>Reciving Address</label>
<input class="form-control" name="re_add" placeholder="Enter text">
</div>
<div class="form-group">
<label>Subscription</label>
<select class="form-control" name="sub">
<option>weekly</option>
<option>Monthly</option>
<option>Quterly</option>
<option>Yearly</option>
</select>
</div>
<div class="form-group">
<label>Time</label>
<select class="form-control" name="time">
<option value="8.00 AM">8.00 AM</option>
<option value="9.00 AM">9.00 AM</option>
<option value="10.00 AM">10.00 AM</option>
<option value="11.00 AM">11.00 AM</option>
<option value="12.00 AM">12.00 AM</option>
<option value="6.00 PM">6.00 PM</option>
<option value="7.00 PM">7.00 PM</option>
<option value="8.00 PM">8.00 PM</option>
<option value="9.00 PM">9.00 PM</option>
<option value="10.00 PM">10.00 PM</option>
</select>
</div>
<label>Cost</label>
<div class="form-group input-group">
<input type="text" name="cost" class="form-control">
<span class="input-group-addon">.00</span>
</div>
<button type="submit" name="submit" class="btn btn-default">Submit Button</button>
<button type="reset" class="btn btn-default">Reset Button</button>
</form>
请帮忙。感谢。
答案 0 :(得分:1)
首先,不要使用 mysql _ * 函数,它们在PHP 7中已被弃用并完全删除。
回到你的问题。
您的SQL查询有9个字段和8个值。这是不匹配的。
$query = "INSERT into `orders` (user_id, type, qty, de_add, re_add, sub, time_deli, cost, reg_date)
VALUES ('$type','$qty','$de_add','$re_add','$sub','$time_deli','$cost',
'$reg_date')";
缺少$user_id
答案 1 :(得分:1)
$query = "INSERT into `orders` (user_id, type, qty, de_add, re_add, sub, time_deli, cost, reg_date) VALUES ('$type','$qty','$de_add','$re_add','$sub','$time_deli','$cost', '$reg_date')";
我在此查询中看到9列但只有8个值。您在VALUES(或任何保存用户ID的变量)中缺少$ user_id,这导致查询失败(列和值必须在数量上相等)。
答案 2 :(得分:0)
嗨!
ini_set('display_errors', true);
error_reporting(E_ALL);
或
error_reporting(E_ALL ^ E_NOTICE);
后者不会显示通知
P.S我正在订阅这个答案,所以请随时告诉我它是怎么回事!