[英]unable to save the value in form in mysql DB using PHP PDO
我有一種形式,其中一個字段具有預定義的值(使用php echo)。 我正在嘗試保存表單,以便所有值都存儲在DB中。
但是,預定義值顯示為空白值,因此無法保存。
我收到錯誤消息,內容為:
未定義的索引用戶ID
和
SQLSTATE [23000]:違反完整性約束:1048列“ vd_user_id”不能為空
碼:
<?php
$email = $_SESSION['email'];
$STM = $conn ->prepare("SELECT * FROM vendorusers WHERE vd_user_email = :email");
$STM->bindParam(':email',$email);
$STM->execute();
$rows = $STM->fetchAll();
foreach ($rows as $row) {
$username = $row['vd_user_name'];
$userid = $row['vd_user_id'];
?>
<form name="userForm" role="form" action="" method="post" novalidate>
<!-- text input -->
<div class="form-group">
<label>Restaurant Name</label>
<input type="text" class="form-control" id = "username" name = "username[]" value= "<?php echo $username; ?>" disabled>
</div>
<div class="form-group">
<label>User ID </label>
<input type="text" class="form-control" id = "userid" name = "userid" value= "<?php echo $userid; ?>" disabled>
<?php } ?>
</div>
<div class="box box-danger">
<div class="box-header">
<h3 class="box-title">Location</h3>
</div>
<div class="box-body">
<input class="form-control" type="text" placeholder="Address Line 1" id = "Add1" name = "Add1" required>
<br>
<input class="form-control" type="text" placeholder="Address Line 2" id = "Add2" name = "Add2">
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-4">
<select class="form-control" id = "City" name = "City">
<option>City</option>
<option>Chennai</option>
</select>
</div>
<!-- select -->
<div class="col-xs-4">
<select class="form-control" id = "State" name = "State">
<option>State</option>
<option>Tamil Nadu</option>
</select>
</div>
<div class="col-xs-3">
<input type="text" class="form-control" placeholder="Pin Code" maxlength="6" id = "PinCode" name = "PinCode">
</div>
</div>
</div>
<!-- /.box-body -->
</div><!-- /.box -->
<div class="modal-footer clearfix"">
<button type="submit" name="submit" id="submit"value = "Save" class="btn btn-primary pull-left" style="float: right>
<i class="fa fa-envelope" ></i>Save</button>
</div>
</form>
<?php
try {
if(isset($_POST['submit']) && isset($_POST["submit"])=="Save") {
// Define Variables
$userid = $_POST['userid'];
echo $userid; //giving blank values
$Logo = $_POST['logo'];
$Add1 = $_POST['Add1'];
$Add2 = $_POST['Add2'];
$City = $_POST['City'];
$State = $_POST['State'];
$PinCode = $_POST['PinCode'];
// Prepare the Query to save the data into the table
$PROFILE = $conn->prepare("INSERT INTO vendorprof (
vd_user_id,
vd_user_logo,
vd_loc_add1,
vd_loc_add2,
vd_loc_city,
vd_loc_state,
vd_loc_pin
)
VALUES (
:userid,
:Logo,
:Add1,
:Add2,
:City,
:State,
:PinCode
)");
// Bind the Parameters
$PROFILE->bindParam(':userid', $userid);
$PROFILE->bindParam(':Logo', $Logo);
$PROFILE->bindParam(':Logo', $Logo);
$PROFILE->bindParam(':Add1', $Add1);
$PROFILE->bindParam(':Add2', $Add2);
$PROFILE->bindParam(':City', $City);
$PROFILE->bindParam(':State', $State);
$PROFILE->bindParam(':PinCode', $PinCode);
// Execute the Query
$PROFILE->execute();
}
}
catch(PDOException $e)
{
echo "<br>" . $e->getMessage();
}
?>
對於userid標記,您使用了關鍵字disabled
。 禁用的輸入永遠不會過帳。
因此,請刪除已disabled
或直接在代碼中訪問$userid
。 似乎$userid
可在以下時間訪問: if(isset($_POST['submit']) && isset($_POST["submit"])=="Save") {
為了使輸入保存到發布中,您可以使用javascript或只做一個隱藏值,並在其后添加echo
,類似於:
<form name="userForm" role="form" action="" method="post" novalidate>
<div class="form-group">
<label>Restaurant Name</label>
<input type="hidden" class="form-control" id = "username" name = "username[]" value= "<?php echo $username; ?>" />
<?php echo $username; ?>
</div>
<div class="form-group">
<label>User ID </label>
<input type="hidden" class="form-control" id = "userid" name = "userid" value= "<?php echo $userid; ?>" />
<?php echo $userid; ?>
<?php } ?>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.