用表格编辑数据

时间:2013-10-11 13:25:19

标签: php html forms mysqli

我正在尝试编辑数据库中的数据,但我无法让它工作。

我已尝试将脚本分解为基础并对每个部分进行故障排除。

删除按钮工作正常,但编辑数据并不是。

我哪里出错?

我的结构 database = domains table = domains_info row = domain

<?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>

我得到的错误就是这个

Database Error: Unable to update record.

1 个答案:

答案 0 :(得分:0)

您的更新查询中有一个额外的','

 $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'])."'";