用PHP更新数据

时间:2015-05-17 11:14:36

标签: php mysql

我正在使用编辑文件来更新手机号码,电子邮件详细信息等,但它没有更新,它显示数据的结果,因此连接正在运行,但没有数据更新。代码:

<?php

    include('header.php');
    $msg='';

     ?> 


<div class="page-cont1">                        
<!--heading starts-->

<?php

    session_start(); //starts the session
    if($_SESSION['user']){ //checks if user is logged in
    }
    else{
        header("location:index.php"); // redirects if user is not logged in
    }
    $user = $_SESSION['user']; //assigns user value
    $id_exists = false;
    ?>
    <body>
        <h2>Home Page</h2>
        <p>Hello <?php Print "$user"?>!</p> <!--Displays user's name-->
        <a href="logout.php">Click here to logout</a><br/><br/>
        <a href="home.php">Return to Home page</a>
        <h2 align="center">Currently Selected</h2>
        <table border="1px" width="100%">
            <tr>
                <th>Id</th>
                <th>E-Mail</th>
                <th>Mobile No</th>
                <th>Details</th>
                <th>Extra Information</th>

            </tr>
            <?php

                if(!empty($_GET['id']))
                {
                    $id = $_GET['id'];
                    $_SESSION['id'] = $id;
                    $id_exists = true;

                    $query = mysql_query("Select * from doctor Where id='$id'"); // SQL Query
                    $count = mysql_num_rows($query);
                    if($count > 0)
                    {
                        while($row = mysql_fetch_array($query))
                        {
                            Print "<tr>";
                                Print '<td align="center">'. $row['id'] . "</td>";
                                Print '<td align="center">'. $row['your_email'] . "</td>";
                                Print '<td align="center">'. $row['mobile_no'] . "</td>";
                                Print '<td align="center">'. $row['detail'] . "</td>";
                                Print '<td align="center">'. $row['info'] . "</td>";

                            Print "</tr>";
                        }
                    }
                    else
                    {
                        $id_exists = false;
                    }
                }
            ?>
        </table>
        <br/>
        <?php

        if($id_exists)
        {
        Print '
        <form action="edit.php" method="POST">
            Enter new E-Mail: <input type="text" name="your_email"/><br/>
            Enter new Mobile: <input type="text" name="mobile_no"/><br/>
            Enter new detail: <input type="text" name="detail"/><br/>
            Enter new Extra Information: <input type="text" name="info"/><br/>

            <input type="submit" value="Update List"/>
        </form>
        ';
        }
        else
        {
            Print '<h2 align="center">There is no data to be edited.</h2>';
        }
        ?>


<?php

    if($_SERVER['REQUEST_METHOD'] == "POST")
    {

        $field_email = mysql_real_escape_string($_POST['your_email']);
        $field_phone = mysql_real_escape_string($_POST['mobile_no']);
        $detail = mysql_real_escape_string($_POST['detail']);
        $field_message = mysql_real_escape_string($_POST['info']);

        mysql_query("UPDATE doctor SET your_email='$field_email', mobile_no='$field_phone', detail='$detail', info='$field_message' WHERE id='$id'") ;
        header("location: home.php");
    }
?>
<?php
    include('footer.php');
    $msg='';

     ?> 

</body>

更多的头文件包含连接文件,一个查询表单存在头文件或编辑文件中的问题。

3 个答案:

答案 0 :(得分:1)

首先在更新查询中将医生替换为医生(正如Utharsh所说)。

第二:

您必须在要发布的表单中包含该ID。

    Print '<form action="edit.php" method="POST">
        Enter new E-Mail: <input type="text" name="your_email"/><br/>
        Enter new Mobile: <input type="text" name="mobile_no"/><br/>
        Enter new detail: <input type="text" name="detail"/><br/>
        Enter new Extra Information: <input type="text" name="info"/><br/>
        <input type="hidden" name="id" value="'.$id.'">
        <input type="submit" value="Update List"/>
    </form>';

答案 1 :(得分:0)

在更新查询中将DOCTOR替换为doctor

如果问题仍然存在,请尝试对查询进行硬编码并在phpmyadmin中执行。

在表单中添加一个隐藏字段,并将id作为jeff声明

答案 2 :(得分:0)

收到ans后,我在ans中研究了我的代码,我读到了ID,所以我注意到在更新时没有ID引用所以我现在改变了我的代码问题解决了我的工作代码是:

<?php

    include('header.php');
    $msg='';

     ?> 


<div class="page-cont1">                        
<!--heading starts-->

<?php

    session_start(); //starts the session
    if($_SESSION['user']){ //checks if user is logged in
    }
    else{
        header("location:index.php"); // redirects if user is not logged in
    }
    $user = $_SESSION['user']; //assigns user value
    $id_exists = false;
    ?>
    <body>
        <h2>Home Page</h2>
        <p>Hello <?php Print "$user"?>!</p> <!--Displays user's name-->
        <a href="logout.php">Click here to logout</a><br/><br/>
        <a href="home.php">Return to Home page</a>
        <h2 align="center">Currently Selected</h2>
        <table border="1px" width="100%">
            <tr>
                <th>Id</th>
                <th>E-Mail</th>
                <th>Mobile No</th>
                <th>Details</th>
                <th>Extra Information</th>

            </tr>
            <?php

                if(!empty($_GET['id']))
                {
                    $id = $_GET['id'];
                    $_SESSION['id'] = $id;
                    $id_exists = true;

                    $query = mysql_query("Select * from doctor Where id='$id'"); // SQL Query
                    $count = mysql_num_rows($query);
                    if($count > 0)
                    {
                        while($row = mysql_fetch_array($query))
                        {
                            Print "<tr>";
                                Print '<td align="center">'. $row['id'] . "</td>";
                                Print '<td align="center">'. $row['your_email'] . "</td>";
                                Print '<td align="center">'. $row['mobile_no'] . "</td>";
                                Print '<td align="center">'. $row['detail'] . "</td>";
                                Print '<td align="center">'. $row['info'] . "</td>";

                            Print "</tr>";
                        }
                    }
                    else
                    {
                        $id_exists = false;
                    }
                }
            ?>
        </table>
        <br/>
        <?php

        if($id_exists)
        {
        Print '
        <form action="edit.php" method="POST">
            Enter new E-Mail: <input type="text" name="your_email"/><br/><br/>
            Enter new Mobile: <input type="text" name="mobile_no"/><br/><br/>
            Enter new detail: <textarea name="detail" rows="6" id="detail" style="width:200px;"></textarea><br/><br/>
            Enter new Extra Information: <textarea name="info" rows="4" id="info" style="width:200px;"></textarea><br/><br/>
            <input type="hidden" name="id" value="'.$id.'">

            <input type="submit" value="Update List"/>
        </form>
        ';
        }
        else
        {
            Print '<h2 align="center">There is no data to be edited.</h2>';
        }
        ?>


<?php

    if($_SERVER['REQUEST_METHOD'] == "POST")
    {

        $field_email = mysql_real_escape_string($_POST['your_email']);
        $field_phone = mysql_real_escape_string($_POST['mobile_no']);
        $detail = mysql_real_escape_string($_POST['detail']);
        $field_message = mysql_real_escape_string($_POST['info']);
        $id = $_SESSION['id'];
        mysql_query("UPDATE doctor SET your_email='$field_email', mobile_no='$field_phone', detail='$detail', info='$field_message' WHERE id='$id'") ;
        header("location: home.php");
    }
?>
<?php
    include('footer.php');
    $msg='';

     ?> 

</body>