我的 PHP 文件中有2 isset()
。添加新值是可以的,但在更新值似乎有效但不在数据库中。
这是我的PHP代码。
<?php
if(isset($_POST['btnposins'])) {
mysql_connect("localhost", "root", "") or die(mysql_error());
//select database
mysql_select_db("brm_dbs") or die(mysql_error());
$posi = mysql_real_escape_string($_POST['inputposi']);
//insert data using insert into statement
$query = "Insert into admin_position(Position)Values('$posi')";
if (mysql_query($query)) {
//dislay a message box that the saving is successfully save
echo "<script type=\"text/javascript\">
alert(\"New Position has been posted.\");
window.location = \"new_admin.php\"
</script>";
} else {
echo("Failed: " . mysql_error());
}
}
else if(isset($_POST['btnposave'])) {
mysql_connect("localhost", "root", "") or die(mysql_error());
//select database
mysql_select_db("brm_dbs") or die(mysql_error());
$posid = $_POST['posid'];
$posname = $_POST['posname'];
//insert data using insert into statement
$query = "UPDATE admin_positon SET Position='$posname' WHERE uid='$posid'";
//execute the query
mysql_query($query);
//header("Location: new_admin.php");
if (mysql_affected_rows() != 0) {
echo "<script type=\"text/javascript\">
alert(\"Position Name Edited\");
window.location = \"new_admin.php\"
</script>";
} else {
echo "<script type=\"text/javascript\">
alert(\"Position Name Not Edited\");
window.location = \"new_admin.php\"
</script>";
}
} else {
echo 'No Tag'.mysql_error();
}
?>
我的代码中出现了什么问题?它始终返回&#39;位置名称已编辑&#39; ,即使我的值为空。
这是我的表格
<div id="position" class="modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Posting New Position</h4>
</div>
<div class="modal-body">
<form role="form" action="posi.php" method="post" enctype="multipart/form-data">
<fieldset>
<div class="form-group">
<label>Position Name</label>
<input id="inputposi" type="text" name="inputposi" class="form-control">
</div>
<div class="form-group">
<table id="posi" class="table table-bordered table-hover ">
<thead>
<tr>
<th>ID</th>
<th>Position's</th>
</tr>
</thead>
<tbody id="posi">
<?php
//set up mysql connection
mysql_connect("localhost", "root", "") or die(mysql_error());
//select database
mysql_select_db("brm_dbs") or die(mysql_error());
//select all records form tblmember table
$query = 'SELECT * FROM admin_position';
//execute the query using mysql_query
$result = mysql_query($query);
//then using while loop, it will display all the records inside the table
while ($row = mysql_fetch_array($result)) {
echo ' <tr> ';
echo ' <td> ';
echo $row['uid'];
echo ' <td> ';
echo $row['Position'];
}
?>
</tbody>
</table>
</div>
<div class="form-group">
<input type="number" id="posid" name="posid" placeholder="ID" class="form-control" >
<input type="text" id="posname" name="posname" placeholder="Desired Position Name" class="form-control" >
</div>
<div class="modal-footer">
<button type="submit" id="btnposave" name="btnposave" onclick="edit()" class="btn btn-success">Save</button>
<button type="submit" id="btnposins" name="btnposins" onclick="edit()" class="btn btn-success">ADD</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>
已更新
似乎$_POST['posid']
AND $_POST['posname']
没有传递值。什么造成这种情况?请帮助。
答案 0 :(得分:0)
改变这个:
if (mysql_affected_rows() != 0) // OR change to !== 0
到此:
if (mysql_affected_rows() == 1) {