0

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

//that main problem is here and I can't be retrieved the data on this page and always sow 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>
bhargav Patel
  • 156
  • 12

2 Answers2

1

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

Scuzzy
  • 12,186
  • 1
  • 46
  • 46
1

try this:

$conn = mysqli_connect('localhost','root','','phonebook');
$result=mysqli_query($conn,"SELECT * FROM contacts WHERE id='$id'");
$fetch=mysqli_fetch_array($result);
  1. You must not use mysql_*, it's deprecated. Use PDO or MySQLi instead
  2. You shouldn't mix mysql_* and mysqli_*
  3. Just create ONE mysqli instance instead of creating it for every file you have.
  4. Maximize the use of variables too. This way you only have to change something once.
  5. Please sanitize/escape user input before passing it into your SQL query. Otherwise your application is vulnerable to SQL injection attacks.
Aiman Daniel
  • 131
  • 1
  • 9