我想尝试在其他页面上获取数据而不是更新,但总是显示错误

i want to try fetch data on other page and than update but always show me an error

这是我的索引 page.inserted 数据库中的所有数据并显示在同一页面上,但主要问题是在 update.php 页面上我无法检索数据

//主要问题在这里,我无法检索此页面上的数据并始终播种:警告:mysql_fetch_array() 期望参数 1 是资源,C 中给出的对象: \wamp\www\phonebook\update.php 第 12 行

                index.php


                <?php require_once('dbconnect.php'); ?>

                <html>
                <head>
                <title> </title>
                </head>
                <body>
                <h1> phone book </h1>

                <form method="post">
                <table>
                <tr> 
                <td>fname </td><td> <input type="text" name="firstname" required  /> </td>
                </tr>
                <tr> 
                <td>lname </td><td> <input type="text" name="lastname" required  /> </td>
                </tr>
                <tr> 
                <td>mobile </td><td> <input type="text" name="mobile" required  /> </td>
                </tr>
                </table>    
                <input type="submit" name="submit" value="submit" >

                </form> 

                <!-- $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ show  $$$$$$$$$$$$$$$$$$$$$$$$$$ -->

                <br> data </br>
                <table border="1">
                <tr>
                <th>id</th>   <th>firstname</th>      <th>lastname</th> <th>mobile</th><th>update</th><th>delete</th>
                </tr>
                <?php
                $conn = mysqli_connect('localhost','root','','phonebook');
                $show = mysqli_query($conn,"SELECT * FROM contacts");

                while($row = mysqli_fetch_array($show)) 
                {
                ?>

                <tr> 
                <td><?php echo $row['id']; ?></td>
                <td><?php echo $row['firstname']; ?></td>
                <td><?php echo $row['lastname']; ?></td>
                <td><?php echo $row['mobile']; ?></td>
                <td><a href="update.php?id=<?php echo $row['id']; ?>">update</a></td>
                <td><a href="delete.php?id=<?php echo $row['id']; ?>" onclick="return confirm('sure want to delete')" >delete</a></td>
                </tr>


                <?php }  ?>
                </table>
                </body>
                </html>

                <?php
                //require_once("function.php");
                //$obj = new data();


                if(isset($_POST{"submit"}))
                {

                //echo "<pre>";print_r($_POST);die;
                $fname = $_POST['firstname'];
                $lname = $_POST['lastname'];
                $mobile = $_POST['mobile'];
                //$obj->insert($fname,$lname,$mobile);
                $connect = mysqli_connect('localhost','root','','phonebook');
                $insert = mysqli_query($connect,"insert into contacts(firstname,lastname,mobile) values('".$fname."','".$lname."','".$mobile."')");

                if ($insert)
                {  ?>

                <script> alert('record inserted'); </script>
                <?php 
                }
                else
                { ?>
                <script> alert('record not inserted'); </script>
                <?php

                }

                header('Location:index.php');
                }

                ?>

                update.php  
//check the code here 

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

                if(isset($_GET['id']) && is_numeric($_GET['id']) ) 
                {
                $id=$_GET['id'];
                }

                ?>
                <?php
                $conn = mysqli_connect('localhost','root','','phonebook');
                $result=mysqli_query($conn,"SELECT * FROM contacts WHERE id='$id'");
                $fetch=mysql_fetch_array($result);
                //$conn = mysqli_connect('localhost','root','','phonebook');
                //$show = mysqli_query($conn,"SELECT * FROM contacts");

                //while($row = mysqli_fetch_array($show)) 


                ?>


                <html>
                <head>
                <title>update page</title>
                </head>
                <body>
                <form method="post"  name="update" action="update.php">
                <table>

                <tr> 
                <td>fname </td><td> <input type="text" name="firstname" value= "<?php echo $fetch['firstname']; ?>" required  /> </td>
                </tr>
                <tr> 
                <td>lname </td><td> <input type="text" name="lastname" value="<?php echo $fetch['lastname']; ?>" required  /> </td>
                </tr>
                <tr> 
                <td>mobile </td><td> <input type="text" name="mobile" value= "<?php echo $fetch['mobile']; ?>" required  /> </td>
                </tr>
                </table>    
                <input type="submit" name="submit" value="submit" >

                </form> 

                </body>
                </html>

切换到使用 mysqli_fetch_array() (note the i) instead of mysql_fetch_array

试试这个:

$conn = mysqli_connect('localhost','root','','phonebook');
$result=mysqli_query($conn,"SELECT * FROM contacts WHERE id='$id'");
$fetch=mysqli_fetch_array($result);
  1. 您不能使用 mysql_*,它已被弃用。使用 PDO 或 MySQLi 代替
  2. 你不应该混用 mysql_* 和 mysqli_*
  3. 只需创建一个 mysqli 实例,而不是为您拥有的每个文件都创建它。
  4. 也最大限度地利用变量。这样你只需要改变一次。
  5. 请 sanitize/escape 用户输入,然后再将其传递到您的 SQL 查询中。否则您的应用程序容易受到 SQL 注入攻击。