Php 使用表单编辑数据
我正在尝试编辑数据库中的数据,但无法使其正常工作 我尝试将脚本分解为基本部分,并对每个部分进行故障排除 “删除”按钮工作正常,但编辑数据却不行 我哪里出错了 我的结构 数据库=域表=域\u信息行=域Php 使用表单编辑数据,php,html,forms,mysqli,Php,Html,Forms,Mysqli,我正在尝试编辑数据库中的数据,但无法使其正常工作 我尝试将脚本分解为基本部分,并对每个部分进行故障排除 “删除”按钮工作正常,但编辑数据却不行 我哪里出错了 我的结构 数据库=域表=域\u信息行=域 <?php include("header.php"); //include database connection include 'db_connect.php'; $action = isset( $_POST['action'] ) ? $_POST['action'] : "";
<?php include("header.php");
//include database connection
include 'db_connect.php';
$action = isset( $_POST['action'] ) ? $_POST['action'] : "";
if($action == "update"){
//write query
$query = "update domains_info
set
domain = '".$mysqli->real_escape_string($_POST['domain'])."',
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'";
if( $mysqli->query($query) ) {
echo "User was updated.";
}else{
echo "Database Error: Unable to update record.";
}
}
if($action=='delete'){ //if the user clicked ok, run our delete query
$query = "DELETE FROM domains_info WHERE id='".$mysqli->real_escape_string($_GET['id'])."'";
if( $mysqli->query($query) ){
echo "User was deleted.";
}else{
echo "Database Error: Unable to delete record.";
}}
$query = "select id, domain
from domains_info
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'
limit 0,1";
$result = $mysqli->query( $query );
$row = $result->fetch_assoc();
$id = $row['id'];
$domain = $row['domain'];?>
<form action='#' method='post' border='0' class="well form-horizontal">
<fieldset>
<label class="control-label" for="name">Domain</label>
<div class="controls">
<input id="name" type="text" name="name" value="<?php echo$domain; ?>">
<input type='hidden' name='id' value='<?php echo $id ?>' />
<!-- we will set the action to edit -->
<input type='hidden' name='action' value='update' />
<input type='submit' value='Edit' />
</fieldset>
</form>
<?php echo "<a href='#' onclick='delete_user( {$id} );'>Delete</a>"; ?>
<script type='text/javascript'>
function delete_user( id ){
//prompt the user
var answer = confirm('Are you sure you want to delete <?php echo$name; ?>?');
if ( answer ){ //if user clicked ok
//redirect to url with action as delete and id of the record to be deleted
window.location = 'deletecontact.php?id=' + id;
}
}
</script>
更新查询中有一个额外的“,”
$query = "update domains_info
set
domain = '".$mysqli->real_escape_string($_POST['domain'])."',
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'";
与
它会给出一个MySQL错误吗?尝试添加
printf(“错误消息:%s\n”,$mysqli->error)代码>下面echo“数据库错误:无法更新记录。”代码>在UPDATE语句中,domain=
行的末尾有一个额外的逗号。如果您使用$mysqli->error
,它将准确地告诉您问题所在;检查与您的MySQL服务器版本对应的手册,了解第4行“where id='2”附近使用的正确语法。如果您使用准备好的语句,您不必为真正的_escape_字符串而烦恼。如果我要在此添加另一行,会是什么?如果我要在此添加另一行,会是什么?
$query = "update domains_info
set
domain = '".$mysqli->real_escape_string($_POST['domain'])."',
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'";
$query = "update domains_info
set
domain = '".$mysqli->real_escape_string($_POST['domain'])."'
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'";