0

we have taken the data and put it into database using php and also done with searching data , the only problem we are facing here is when we hit update button after searching data , we are getting Notice as

Notice: Undefined index: FromD in C:\xampp\htdocs\bkc\update-search.php on line 130

Notice: Undefined index: ToD in C:\xampp\htdocs\bkc\update-search.php on line 131

Notice: Undefined index: Contact in C:\xampp\htdocs\bkc\update-search.php on line 132

Notice: Undefined index: Child in C:\xampp\htdocs\bkc\update-search.php on line 136

and we are losing our FromD , ToD , Contact , Child data from database and getting message "Updation Failed" after hitting update button

NOTE:

  1. FromD, ToD, Contact are text and Child is number/integer

  2. Other is info. is updating Successfully with message "Data Updated"

CODE:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Search and Update</title>

    <style>
        .align{
            align:center;
        }
        .grad1 {
            height: 600px;
            background-color: red; 
            background-image: linear-gradient(315deg,#F6BB45, #18AA99);
            border-radius:20px;
            color:white;
        }
        
        .container{
            margin: 10px 50px 50px 50px;
        }
      </style>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

</head>
<body>
    
    <div class="container grad1 text-center">
        <h1>Update Reservation Details</h1>
        <form action="" method="POST"> 
            <div class="form-group ">
                <label for="exampleInputEmail1"><h5>Email address</h5></label>
                <input type="email" class="form-control " name="email" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email to Search">
            </div>
            <!--
            <div class="form-group">
                <label for="exampleInputPassword1"><h5>Password</h5></label>
                <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
            </div>
            -->
            <button type="submit" class="btn btn-warning" name="search" value="Search Data"><h5>Search</h5></button>
          </form>

        <?php
          $conn = mysqli_connect("localhost","root","");
          $db = mysqli_select_db($conn,"booking");

          if(isset($_POST['search']))
          {
            $email = $_POST['email'];
            
            $query = "SELECT * FROM bookingt where email='$email' ";
            $query_run = mysqli_query($conn,$query);

            while($row = mysqli_fetch_array($query_run))
            {
                ?>
                    <form action="" method="POST">
                        <div class="form-group">
                            <br>
                            <h4>Your Details:</h4>
                            <p><b>Email:</b>
                            <input type ="hidden" name="Email" value="<?php echo $row['Email'] ?>"/>
                            <b>Name:</b>
                            <input type ="text" name="Name" value="<?php echo $row['Name'] ?>"/></p>

                            <p><b>Source:</b>
                            <input type ="text" name="Source" value="<?php echo $row['Source'] ?>"/>
                            <b>Destination:</b>
                            <input type ="text" name="Destn" value="<?php echo $row['Destn'] ?>"/></t>
                            <b>From:</b>
                            <input type ="text" name="From-D" value="<?php echo $row['FromD'] ?>"/>
                            <b>To:</b>
                            <input type ="text" name="To-D" value="<?php echo $row['ToD'] ?>"/></p>
                            
                            <p><b>Contact:</b>
                            <input type ="text" name="contact" value="<?php echo $row['Contact'] ?>"/>
                            <b>Bus:</b>
                            <input type ="text" name="Bus" value="<?php echo $row['Bus'] ?>"/>
                            <b>Class:</b>
                            <input type ="text" name="Class" value="<?php echo $row['Class'] ?>"/></p>

                            <p><b>No. of adults:</b>
                            <input type ="number" name="Adult" value="<?php echo $row['Adult'] ?>"/>
                            <b>No. of childs:</b>
                            <input type ="number" name="child" value="<?php echo $row['Child'] ?>"/>
                            <b>No. of Uchilds:</b>
                            <input type ="number" name="Uchild" value="<?php echo $row['Uchild'] ?>"/></p>
                        </div>
                        <button type="submit" class="btn btn-danger" name="update" value="Update Data"><h5>Update</h5></button>
                    </form>
                <?php
            }
          }

        $conn->close();
        ?>

    </div>

</body>
</html>
<?php

ini_set('display_errors',1);
error_reporting(E_ALL);

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

try{
    $conn = mysqli_connect("localhost","root","");
    $db = mysqli_select_db($conn,"booking");
}   catch(Exception $ex)    {
    echo "Error";
}

function getPosts()
{
    $posts = array();
    $posts [0] = $_POST['Name'];
    $posts [1] = $_POST['Source'];
    $posts [2] = $_POST['Destn'];
    $posts [3] = $_POST['FromD'];
    $posts [4] = $_POST['ToD'];
    $posts [5] = $_POST['Contact'];
    $posts [6] = $_POST['Bus'];
    $posts [7] = $_POST['Class'];
    $posts [8] = $_POST['Adult'];
    $posts [9] = $_POST['Child'];
    $posts [10] = $_POST['Uchild'];
    $posts [11] = $_POST['Email'];
    return $posts;
}

if(isset($_POST['update']))
{
    $data = getPosts();
    $update_Query = "UPDATE `bookingt` SET `Source`='$data[1]',`Destn`='$data[2]',`Bus`='$data[6]',`Class`='$data[7]',`Adult`='$data[8]',`Child`='$data[9]',`Uchild`='$data[10]',`FromD`='$data[3]',`ToD`='$data[4]',`Name`='$data[0]',`Contact`='$data[5]' WHERE `Email`='$data[11]' ";
    try{
        $update_Result = mysqli_query($conn, $update_Query);

        if($update_Result)
        {
            if(mysqli_affected_rows($conn) > 0)
            {
                echo "Data Updated !!!";
            }
            else
            {
                echo " Updation Failed";
            }
        }
    }   catch(Exception $ex){
        echo "Error Update ".$ex->getMessage();
    }

}

$conn->close();
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
RDS
  • 53
  • 4
  • 2
    Are you certain your posted data includes FromD, ToD, Contact and Child query parameters? What does var_dump($_POST) outputs? – smartdroid Oct 31 '20 at 06:28
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Dharman Oct 31 '20 at 13:27

2 Answers2

2

The field names in form and PHP code need to be identical. These names are also case sensitive. Correct the PHP code so that you are using the names as in html form. For example change ToD to To-D, Child to child etc

smartdroid
  • 312
  • 2
  • 10
1

Check the "FromD", "ToD", "Contact" and "Child" names are available in the form which you're submitting.

I think you're missed or misspelled the name attribute for that 4 fields in the form...

Are you using a form or doing an ajax call?