duanmu5039 2014-10-13 12:43
浏览 33
已采纳

数据库字段默认为某个值

I have this form that allows me to add users to my database.

When I add a new record and enter each field with data and submit, it successfully adds data to the database

However when I attempt to update that specific record and press submit my memberID field is automatically set a value and im not sure where it may be coming from (See Here http://imgur.com/MK07epA)

I have looked all throughout my code and cant seem to find what's causing it.

Upon trying to update or delete the records after the first update, I get the following error messages: (http://imgur.com/Qs0IQKi)

Add Code

<!DOCTYPE html>
<html>
<head><title>Insert Users</title></head>
<body>
<h2>Insert User Confirmation</h2>

<form action="<?php $_SERVER['PHP_SELF']?>" method="post"/> <br> 
<?php


include('connection.php');

                    echo    "<label for='memberID' >Member ID:</label>";
                    echo    "<input type='text' name='memberID' id='memberID' />";
                    echo    "<br /><br />"; 

                    echo    "<label for='username' >Username:</label>";
                    echo    "<input type='text' name='username' id='username' />";
                    echo    "<br /><br />"; 

                    echo    "<label for='password' >Password:</label>";
                    echo    "<input type='password' name='password' id='password' />";
                    echo    "<br /><br />"; 

                    echo    "<label for='fName' >Firstname:</label>";
                    echo    "<input type='text' name='fName' id='fName' />";
                    echo    "<br /><br />"; 

                    echo    "<label for='lName' >Lastname:</label>";
                    echo    "<input type='text' name='lName' id='lName'  />";
                    echo    "<br /><br />"; 

                    echo    "<label for='address' >Address:</label>";
                    echo    "<input type='text' name='address' id='address'  />";
                    echo    "<br /><br />"; 

                    echo    "<label for='email' >Email:</label>";
                    echo    "<input type='text' name='email' id='email'  />";
                    echo    "<br /><br />";

                    echo    "<input type='submit' name='submit' value='Submit' />";
                    echo    "<input type='reset' value='Clear' />";
                    echo    "<br /><br />"; 
 ?>
</form>
</section>

<a href="home.php">Back Home</a><br/>

<?php
if(!isset($_POST['submit'])) {  
    echo 'Please Register';
}
else {

        $memberID = $_POST['memberID'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $fName = $_POST['fName'];
        $lName = $_POST['lName'];
        $address = $_POST['address'];
        $email = $_POST['email'];



        $query = "INSERT INTO `members` (memberID, username, password, firstname, lastName, address, email) 
        VALUES ('$memberID', '$username', '$password', '$fName', '$lName', '$address', '$email')";
        mysqli_query($connection, $query)
        or  die(mysqli_error($connection));

        $rc = mysqli_affected_rows($connection);
        if ($rc==1)   
                    {
                    echo '<h4>The database has been updated with the following details: </h4> ';
                    echo 'MemberID: '.$memberID.'<br />';
                    echo 'Username: '.$username.'<br />';
                    echo 'Password: '.$password.'<br />';
                    echo 'First Name: '.$fName.'<br />';
                    echo 'Last Name: '.$lName.'<br />';
                    echo 'Address: '.$address.'<br />';
                    echo 'Email: '.$email.'<br />';
                    }
        else
                    {   
                    echo '<p>The data was not entered into the database this time.</p>';   
                    }
    }
?>
</body>
</html>

View Code

<?php
                require_once('connection.php');
                $query = 'select * from members order by memberID';

                include('includes/query.php');

                echo '<table>';
                echo '<tr>';
                echo '  <th>MemberID</th>
                        <th>Username </th> 
                        <th>Password</th> 
                        <th>Firstname</th> 
                        <th>Lastname</th> 
                        <th>Email</th> 
                        <th>Address</th>';  
                echo '</tr>'; 

                foreach ($rows as $row)
                {
                echo '<tr>';
                echo '<td>' . $row['memberID'] . '</td>';
                echo '<td>' . $row['username'] . '</td>';
                echo '<td>' . $row['password'] . '</td>';
                echo '<td>' . $row['firstname'] . '</td>';
                echo '<td>' . $row['lastname'] . '</td>';
                echo '<td>' . $row['email'] . '</td>';
                echo '<td>' . $row['address'] . '</td>';



                echo '<td> <a href="confirm_delete.php?memberID=' . $row['memberID'] . '">Delete</a> </td>';
                echo '<td> <a href="update_member_form.php?memberID=' . $row['memberID'] . '">Update</a> </td>';
                echo '</tr>';
                }

                echo '</table>';

    ?>

Update Code

<?php

    require_once('connection.php');

        if(!isset($_POST['submit']))    {

        echo "Unauthorised access";

        }
        else  { 
                $memberID = $_POST['memberID'];     
                $username = $_POST['username'];
                $password = $_POST['password'];
                $firstname = $_POST['firstname'];
                $lastname = $_POST['lastname'];
                $email = $_POST['email'];
                $address = $_POST['address'];


                $query = "update members set memberID = '#memberID', username = '$username', password = '$password', 
                firstname = '$firstname', lastname = '$lastname', email = '$email', address = '$address' where memberID = '$memberID'";

                $result = mysqli_query($connection, $query)                     
                    or die (mysqli_error($connection)); 

                $rc = mysqli_affected_rows($connection);

                if($rc == 1)
                {
                    echo "<p>You have successfully updated memberID: {$memberID} account details</p>";
                    echo "<p><a href='view_records.php'>Back to User List</a> </p>";
                }
                else
                {
                    echo "<p>No changes made.</p>";
                    echo "<p><a href='view_records.php'>Back to User List</a> </p>";
                }
        }

?>

Delete Code

<?php
    require_once('connection.php');

        $memberID = $_GET['memberID'];

        $query = "delete from members where memberID = '$memberID';";

        $result = mysqli_query($connection, $query)                     
            or die (mysqli_error($connection)); 

        $rc = mysqli_affected_rows($connection);

        if($rc == 1)
        {
            echo "<p>You have successfully removed MemberID: {$memberID} from the database</p>";
            echo "<a href='list_delete_update_users.php'>Back to User List</a>";
        }
        else
        {

        echo "<p>No record deleted</p>";
        echo "<a href='view_records.php'>Back to User List</a>";
        }   
?>
  • 写回答

3条回答 默认 最新

  • douhanxujiuji6098 2014-10-13 12:53
    关注

    See this line in your update code:

     $query = "update members set memberID = '#memberID', username = '$username', password = '$password', 
                    firstname = '$firstname', lastname = '$lastname', email = '$email', address = '$address' where memberID = '$memberID'";
    

    solution: you got the variable via post in $memberID but while updating you are using #memberID. This might be one of your problems.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值