Search code examples
phpsqlmysqlsyntax-error

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


Here is my index page. Inserted all the data to the database and also shown on the same page but the main problem is that on the update.php page I can not retrieve the data.

//that main problem is here and I can't retrieve the data on this page and it always shows that:

Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in C:\wamp\www\phonebook\update.php on line 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>

Solution

  • Switch to using mysqli_fetch_array() (note the i) instead of mysql_fetch_array