PHP更新添加id为0的新记录

时间:2017-08-26 21:34:01

标签: php mysqli

这是我的第一次,所以请耐心等待我。 我尝试过但没有成功! 我的数据库有7个字段,最后一个是'id'作为键和自动增量 我试图仅更新1条记录中的一个或所有字段。 但是我只是获得了包含所有先前数据的新记录,除了id增加1。 这是我的代码:

<?php
$error = '';
ini_set('display_errors', 1);
session_start();
if(!isset($_SESSION['login_user'])){ // If session is not set then    redirect to Login Page
header("Location:Login.php");  
}

// grab connection code
$login_id = $_SESSION['login_id'];

include "connection.php";

$query = "SELECT * FROM bvoa_login WHERE id = '$login_id'";

// get result
$result = $conn->query($query);

//create array of result
$row = $result->fetch_assoc();
// set existing data into variables
$curr_name = $row["name"];
$curr_username = $row["username"];
$curr_email = $row["email"];
$curr_property_no = $row["property_no"];    
$curr_website = $row["website"];    

if($_SERVER["REQUEST_METHOD"] == "POST") {
    if (!empty($_POST['username']) && !empty($_POST['email'])
        && !empty($_POST['new_password']) && !empty($_POST[  'password_confirm']) && !empty($_POST['location']) && !empty($_POST['real_name'])){
        $username = $_POST['username'];
        $real_name = $_POST['real_name'];
        $email = $_POST['email'];
        $new_password = $_POST['new_password'];
        $password_confirm = $_POST['password_confirm'];
        $location = $_POST['location'];
        $website = $_POST['website'];

        // compare passwords...
        if (strcmp($new_password, $password_confirm) == 0){
            $password_hash = sha1($new_password);
            $query = "UPDATE `bvoa_login` SET `username`='$username' WHERE `id`='$login_id'";
            $result = $conn->query($query);
        }else{
            $error = "Passwords do not match.";     
    }
    }else{  
    $error = "You must enter all fields to continue.";
    }
$conn->close();
}
?>

好的我已经调整了脚本,所以它只有1个查询。数据库有1条id为1的记录。我仍然在id字段中添加另一条没有条目的记录。

<?php
$error = '';
ini_set('display_errors', 1);
session_start();
if(!isset($_SESSION['login_user'])){ // If session is not set then redirect to Login Page
    header("Location:Login.php");  
}

// grab connection code
$login_id = $_SESSION['login_id'];

include "connection.php";


if($_SERVER["REQUEST_METHOD"] == "POST") {
    if (!empty($_POST['username']) && !empty($_POST['email'])
        && !empty($_POST['new_password']) && !empty($_POST[  'password_confirm']) && !empty($_POST['location']) && !empty($_POST['real_name'])){
        $username = $_POST['username'];
    $real_name = $_POST['real_name'];
        $email = $_POST['email'];
        $new_password = $_POST['new_password'];
    $password_confirm = $_POST['password_confirm'];
        $location = $_POST['location'];
    $website = $_POST['website'];

    // compare passwords...
    if (strcmp($new_password, $password_confirm) == 0){
        $password_hash = sha1($new_password);
        $query = "UPDATE `bvoa_login` SET `username`='$username' WHERE `id`='$login_id'";
        $result = $conn->query($query);
    }else{
        $error = "Passwords do not match.";     
    }
}else{  
$error = "You must enter all fields to continue.";
}
$conn->close();
}
?>

1 个答案:

答案 0 :(得分:0)

解决了 - 非常愚蠢的错误 - 'profile.php'页面是'register.php'的副本,我忘了更改表单操作,所以它调用了错误的页面 - DUHHH !!!感谢所有花时间发表评论的人。