无法使用 PHP PDO 将表单中的值保存在 mysql 数据库中


unable to save the value in form in mysql DB using PHP PDO

我有一个表单,其中一个字段具有预定义的值(使用 php echo(。我正在尝试保存表单,以便所有值都存储在数据库中。

但是,预定义的值显示空白值,因此无法保存。

我收到错误消息,内容为:

未定义的索引用户标识

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();
}
?>

对于用户 id 标签,您使用了关键字 disabled 。禁用的输入永远不会发布。

因此,要么删除disabled,要么直接在代码中访问$userid。似乎$userid之后可以访问: if(isset($_POST['submit']) && isset($_POST["submit"])=="Save") {

要使输入保存到 post,您可以执行 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>