提问人:Hamed 提问时间:6/9/2016 更新时间:6/9/2016 访问量:1076
使用模式 Bootstrap 获取未定义索引更新查询
Update query with modal Bootstrap get undefined index
问:
我正在尝试完成我的新个人 CMS(内容管理系统),我已经编写了一个部分,其中包括使用 PHP、MySql 和引导模式在一个页面中插入、选择和删除项目。 显然,我在更新查询方面遇到了麻烦。该过程是当我单击编辑按钮时,将显示模态 Bootstrap,然后关联数据将按值属性检索 Mysql。之后,我更改了它们,然后单击“提交”按钮。问题就在这里!当过程完成时,我的 $_POST['input_result'] 出现错误。var_dump得到 NULL,最后我有未定义的索引。 有没有人可以解决这个问题???谢谢大家。 这是我使用 PHP 的模态 Bootstrap 代码:
<?php
$selectForUpdate = "SELECT * FROM ring WHERE id='4'";
$resultSelectForUpdate = mysqli_query($connect_to_db, $selectForUpdate);
$first = $second = $third= '';
$first = $_POST['u_ringCode'];
$second = $_POST['u_ringWeight'];
$third = $_POST['u_ringComment'];
$updateQuery = "UPDATE ring SET ring_code='".$first."', ring_weight='".$second."', ring_comment='".$third."' WHERE id='4'";
$resultUpdateQuery = mysqli_query($connect_to_db, $updateQuery);
while ( $showUpdateRows = mysqli_fetch_assoc( $resultSelectForUpdate ) ) {
?>
<!-- Update Modal -->
<div class="modal fade bs-example-modal-sm" id="update-4" tabindex="-1" role="dialog" aria-labelledby="update-4-label" >
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h5 class="modal-title text-center" id="update-4-label" style="color:#ddd;" >
Edit Code
</h5>
</div>
<div class="modal-body">
<div class="col-md-12 col-sm-12 col-xs-12">
<form method="post" action="<?=htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="form-group">
<label for="">Ring Code :</label>
<input type="text" class="form-control" id="" name="u_ringCode" placeholder="" value="<?=$showUpdateRows['ring_code']?>">
</div>
<div class="form-group">
<label for="">Ring Weight :</label>
<input type="text" class="form-control" id="" name="u_ringWeight" placeholder="" value="<?=$showUpdateRows['ring_weight']?>">
</div>
<div class="form-group">
<label for="">Comment :</label>
<input type="text" class="form-control" id="" name="u_ringComment" placeholder="" value="<?=$showUpdateRows['ring_comment']?>">
</div>
</form>
</div>
</div>
<div class="modal-footer">
<a type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button>
<a type="submit" class="btn btn-default pull-left" name="ringDelete" style="color:black;" href="?update=4">Submit</a>
</div>
</div>
</div>
</div>
<?php
}
?>
答:
0赞
Yury
6/9/2016
#1
在提交表单之前,您的 $_POST 变量为空。你需要检查一下。此外,在不转义字符串的情况下进行数据库查询也不是安全的方法。您可以通过搜索 MySQL 注入来阅读有关此内容的更多信息。第三件事是您在更新查询之前获得的项目。在这种情况下,您将在页面上看到未更改的数据。只有在页面刷新后,它才会显示新值。 代码的第一部分可能如下所示:
<?php
if(!empty($_POST['u_ringCode'])&&!empty($_POST['u_ringWeight'])&&!empty($_POST['u_ringComment']))
{
$first = $connect_to_db->escape_string($_POST['u_ringCode']);
$second = $connect_to_db->escape_string($_POST['u_ringWeight']);
$third = $connect_to_db->escape_string($_POST['u_ringComment']);
$updateQuery = "UPDATE ring SET ring_code='$first', ring_weight='$second', ring_comment='$third' WHERE id='4'";
$resultUpdateQuery = mysqli_query($connect_to_db, $updateQuery);
}
$selectForUpdate = "SELECT * FROM ring WHERE id='4'";
$resultSelectForUpdate = mysqli_query($connect_to_db, $selectForUpdate);
while ( $showUpdateRows = mysqli_fetch_assoc( $resultSelectForUpdate ) ) {
?>
1赞
Kuldeep Singh
6/9/2016
#2
<?php
/*Code Start For Connnect Database*/
$connect_to_db=mysqli_connect("localhost", "root", "", "db_name" );
/*Code End For Connnect Database*/
/*Code Start For Update Modal Form*/
if (isset($_REQUEST['ringUpdate'])) {
$first = $_POST['u_ringCode'];
$second = $_POST['u_ringWeight'];
$third = $_POST['u_ringComment'];
$updateQuery = "UPDATE ring SET ring_code='".$first."', ring_weight='".$second."', ring_comment='".$third."' WHERE id='4'";
$resultUpdateQuery = mysqli_query($connect_to_db, $updateQuery);
}
/*Code End For Update Modal Form*/
$selectForUpdate = "SELECT * FROM ring WHERE id='4'";
$resultSelectForUpdate = mysqli_query($connect_to_db, $selectForUpdate);
while ( $showUpdateRows = mysqli_fetch_assoc( $resultSelectForUpdate ) ) {
?>
<!-- Update Modal -->
<div class="modal fade bs-example-modal-sm" id="update-4" tabindex="-1" role="dialog" aria-labelledby="update-4-label" >
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<form method="post" action="<?=htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h5 class="modal-title text-center" id="update-4-label" style="color:#ddd;" >
Edit Code
</h5>
</div>
<div class="modal-body">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="form-group">
<label for="">Ring Code :</label>
<input type="text" class="form-control" id="" name="u_ringCode" placeholder="" value="<?=$showUpdateRows['ring_code']?>">
</div>
<div class="form-group">
<label for="">Ring Weight :</label>
<input type="text" class="form-control" id="" name="u_ringWeight" placeholder="" value="<?=$showUpdateRows['ring_weight']?>">
</div>
<div class="form-group">
<label for="">Comment :</label>
<input type="text" class="form-control" id="" name="u_ringComment" placeholder="" value="<?=$showUpdateRows['ring_comment']?>">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button>
<input type="submit" class="btn btn-default pull-left" name="ringUpdate" style="color:black;" value="submit">
</div>
</form>
</div>
</div>
</div>
<?php
}
?>
评论
0赞
Hamed
6/9/2016
它有效,伙计!谢谢。我希望你能得到你想要的最好的想法:D
0赞
Kuldeep Singh
6/9/2016
谢谢,我很乐意帮助你
评论