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

时间:2017-03-17 11:39:29

标签: php mysql sql syntax-error core

这是我的索引页面。将所有数据插入数据库并显示在同一页面上但主要问题是在update.php页面上我无法检索数据

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

                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>

2 个答案:

答案 0 :(得分:1)

切换为使用mysqli_fetch_array()(请注意 i )而不是mysql_fetch_array

答案 1 :(得分:1)

试试这个:

$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. 在将用户输入传递给SQL查询之前,请先清理/转义用户输入。否则,您的应用程序很容易受到SQL注入攻击。