Php form update

I am having Trouble with the Update Section on my program that i am creating i am following Lynda videos and the New Boston,

when i hit update it is showing no errors but it is not updating on the front nor the back end.

   <?php
    // IF RESQUEST IS EQUAL TO SUBMUIT
        if (isset($_REQUEST['submit']))
                {   
                    $my_date = date("Y-m-d H:i:s");
                    $order = uniqid();
                    $FullName= $_REQUEST['fullname'];
                    //Take in full Name and Split it into first and last name.
                    list($fname, $lname ) = explode( ' ', $customerName, 2 );       
                    $address = $_REQUEST['address'];
                    $emailAddress = $_REQUEST['emailAddress'];
                    $phoneNo = $_REQUEST['phoneNo'];

Below is my Sticky Forum which is getting the Information from the Database and putting it into the Text Fields

        // STICKY FORM TO ALLOW USER TO UPDATE INFORMATION 
        if (isset($_REQUEST['up']))
            {
                $query_sticky = mysqli_query($connection,'SELECT * FROM orders WHERE id = "' . $_GET['id'] . '"');
                if(! $query_sticky )
        {
          die('Could not get data: ' . mysqli_error($connection)); // Could not find Order_id show Error
        }//end die error 
        else
            (isset($_REQUEST['update']));
            {
        while($row = mysqli_fetch_array($query_sticky, MYSQLI_ASSOC))
        {
            $row['id'];
            echo '<form action="" method="post">'
    
          Name:';
                echo'<input name="customerName" id="cname" type="text" required   value="'.$row['firstname']. " " .$row['lastname']. '" />';
               echo' <br/>
                <br/>
                Address:
               <textarea name="address" id = "caddress" type="text" rows="5" cols="30" required value="'.$row['address'].'" ></textarea>
                <br/>
                <br/>
                Email Address:
               <input name="emailAddress" type="email" required  value="'.$row['email']. '" />
                <br/>
                <br/>
                <br/>
                Phone Number:
                 <input name="phoneNo" id="phoneNumber" type="text" required  value="'.$row['phone']. '" />
                <br/>
                 <br/>
              <button type="submit" name="update" value="update" >update</button
          <div id="Submit">
            </form>
            <form action="order.php" method="delete">
            </form>';
        }//close if 
            }
        } // Close While 

here is my Update Section

        if (isset($_REQUEST['update']))
        {
                $updateDB = "UPDATE orders SET student ='$_POST[student]', 
                firstname='John', lastname='wallace',
                email = '$_POST[emailAddress]', address = '$_POST[address]',
                phone = '$_POST[phoneNo]'
                WHERE 
                order_id ='$_GET[order_id]'";
                mysqli_query($connection, $updateDB);
            }//end update..     
            }//end PHP 
        ?>

If you look at the code highlighting you can see some problems straight away…

This part here is a problem:

 echo '<form action="" method="post">'

Name:';

Which will throw the rest of your code out.

Also you have a couple of comments wrong; your //close if is the close for the while and the //close while is the close if.

From a glance it looks as thought the first if is never closed - he’s actually closing the while and the else, not the if at all; and then the giant encompassing first if statement just yawns open?
Will have to read more carefully in a minute if I get time, needs better indenting and such…

I can not understand why you are not getting any errors; try adding an error reporting setting: http://php.net/manual/en/function.error-reporting.php

it seems it cant get a defined information from

line 297
order_id =‘$_GET[order_id]’";

and line 152
$query = mysqli_query($connection,‘SELECT * FROM orders WHERE order_id = "’ . $_GET[‘order_id’]. ‘"’);

if (!isset($_REQUEST['submit']))
{ //Open If !isset 

$query = mysqli_query($connection,'SELECT * FROM orders WHERE order_id = "' . $_GET['order_id']. '"');

if(! $query )
{ //Open Query Error 
  echo('Could not get data: ' . mysqli_error($connection)); // Could not find Order_id show Error Message 
} // Close Query Error

 while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) // Run run fecth and display to user.
{ //open While 
    $row['id'];
    $row['order_id'];
    echo"<h2><center>Your Order Receipt</h2></center>".
    "<center>------------------------------------------------</center>".
    "<center>Order No : " .$row['order_id']. "  <br> </center>".
    "<center>Customer Name :".$row['firstname']. " " .$row['lastname']. "<br> </center>".
    "<center>Address :". $row['address']. " <br> </center>".
    "<center>Phone No :" .$row['phone'].  "<br> </center>".
    "<center>------------------------------------------------</center>".

echo '<form action=" " method=\'POST\'>';
echo'<button type="submit" name="up" value="up">Update order</button>';
    echo'<button type ="submit" name="delete" value="delete">delete</button>';
echo '</form>';
} // Close While


    // STICKY FORM TO ALLOW USER TO UPDATE INFORMATION 
if (isset($_REQUEST['up']))
    {
        $query_sticky = mysqli_query($connection,'SELECT * FROM orders WHERE order_id = "' . $_GET['order_id'] . '"');
        if(! $query_sticky )
{
  die('Could not get data: ' . mysqli_error($connection)); // Could not find Order_id show Error
}//end die error 
else
    (isset($_REQUEST['update']));
    {
while($row = mysqli_fetch_array($query_sticky, MYSQLI_ASSOC))
{
    $row['id'];
    
    echo '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
    echo "Your Order ID:"; echo $row['order_id'];
      <div id="yourDetails">
        <h3>Enter your  details</h3>
        Name:';
          echo'<input name="customerName" id="cname" type="text" required   value="'.$row['firstname']. " " .$row['lastname']. '" />';
       echo' <br/>
        <br/>
        Address:
       <textarea name="address" id = "caddress" type="text" rows="5" cols="30" required value="'.$row['address'].'" ></textarea>
        <br/>
        <br/>
        Email Address:
       <input name="emailAddress" type="email" required  value="'.$row['email']. '" />
        <br/>
        <br/>
        <br/>
        Phone Number:
         <input name="phoneNo" id="phoneNumber" type="text" required  value="'.$row['phone']. '" />
        <br/>
         <br/>
     
        </div>
      <button type="update" name="update" value="update" >update</button
  <div id="Submit">
    </form>
    <form action="order.php" method="delete">
    </form>';
}//close if 
    }
} // Close While 


//Delete User

if(isset($_POST['delete']))
    {
    $query_delete = mysqli_query($connection,'DELETE FROM orders WHERE order_id = "'.$_GET['order_id'].'"');
    mysqli_query($connection, $query_delete);
    if ($query_delete)
    {
        echo "Deleted Successfully";
    }
    }
    
    
/**
        UPDATE USER INFORMATION.. 
**/

if (isset($_REQUEST['update']))
{

     $updateDB = "UPDATE orders SET 
        order_id='$_GET[order_id]', 
        student ='$_POST[student]', 
        firstname='John', lastname='wallace',
        email = '$_POST[emailAddress]', address = '$_POST[address]',
        phone = '$_POST[phoneNo]';
        WHERE 
        order_id ='$_GET[order_id]'";
        mysqli_query($connection, $updateDB);
       if(! $updateDB )
       {
          die('Could not get data: ' . mysqli_error($connection)); // Could not find Order_id show Error 
       }
}//end die error     
    }//end PHP 
?>

What is the value of the “orderid” CONSTANT?

Did you add

/* development debugging */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);

to the beginning of the script?

That is at the Start of my PHP file

 if  (isset($_REQUEST['submit']))
            {    
                
                $my_date = date("Y-m-d H:i:s");
                $order_id = uniqid();
                $customerName = $_REQUEST['customerName'];
                //Take in Customer Name and Split it into first and last name.
                list($fname, $lname ) = explode( ' ', $customerName, 2 );        
                
                $address = $_REQUEST['address'];
                $emailAddress = $_REQUEST['emailAddress'];
                $phoneNo = $_REQUEST['phoneNo'];

For a start you still have a problem here:

<div id="yourDetails">
        <h3>Enter your  details</h3>
        Name:';

You are not echoing it; as I said I can see problems from the code highlighting. Here you can see your black echo above is now a red echo below these lines. You must have a similar effect in your code editor.

Hi Rubble,

my Code is fine, for the Echo, as that is giving me no problem…
its the Update that is the problem that i am facing
I also added a Hidden field thinking this would pass the information down

 <button type="update" name="update" value="update" >update</button
      <input type="hidden" name="id" value="'.$row['id'].'"/>

And what error messages are you getting?

( ! ) Notice: Undefined index: id in C:\wamp\www\vieworder.php on line 302
Call Stack
#TimeMemoryFunctionLocation
10.0100176688{main}( )…\vieworder.php:0

Line 303 is

 WHERE id ='$_POST[id]'";

And what is the value of the “id” CONSTANT?

The value of the ID is an Auto Index Number

That isn’t a CONSTANT so it should be
$_POST[‘id’]
similar to how you have for $row[‘id’]

It looks like most of the trouble you’re having is with the use of single and double quotes.

I admit, it can be messy and hard to keep everything right.

Looking at the syntax highlighting in your above code examples should help you spot some errors.
And note the PHP does not parse PHP code that is inside of single quotes, it considers it as a literal string eg.

<?php
$var = "something";
echo "$var";
echo '$var';

will output

something
$var

Below is the full code… it be easier to debug
when i have

echo '<form action=" "method="post">'; 

and click on update nothing happends no error or nothing…

but if i do this

echo '<form action="'.$_SERVER['PHP_SELF'].'" "method="post">';

i keep getting
Undefined index: addPepperoni in C:\wamp\www\vieworder.php on line 293
on all vaules

<?php require("connections.php"); ?>
    <?php include("head.php");?>
    <?php
    // IF RESQUEST IS EQUAL TO SUBMUIT
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    if (isset($_REQUEST['submit']))
            {    
                $my_date = date("Y-m-d H:i:s");
                $order_id = uniqid();
                $pizzaSize = $_REQUEST['pizzaSize'];
                $customerName = $_REQUEST['customerName'];
                //Take in Customer Name and Split it into first and last name.
                list($fname, $lname ) = explode( ' ', $customerName, 2 );        
                
                $address = $_REQUEST['address'];
                $emailAddress = $_REQUEST['emailAddress'];
                $phoneNo = $_REQUEST['phoneNo'];
                //$price = $_POST['price'];
                
                /**
                    The Following Code Below Checks if
                    Check box is Set or Not Set 
                    if not Set, then set String to No
                    if it is set leave Default value of Yes set
                    
                **/
                if(!isset($_REQUEST['addPeppers']))
                {
                    $addPeppers = 'no';
                }else{$addPeppers = $_REQUEST['addPeppers'];}
                
                if(!isset($_REQUEST['addOnion']))
                {
                    $addOnion = 'no';
                }else{$addOnion = $_REQUEST['addOnion'];}
        
                if(!isset($_REQUEST['addOlives']))
                {
                    $addOlives = 'no';
                }else{$addOlives = $_REQUEST['addOlives'];}
        
                if(!isset($_REQUEST['addPepperoni']))
                {
                    $addPepperoni = 'no';
                }else{$addPepperoni = $_REQUEST['addPepperoni'];}
                
                if(!isset($_REQUEST['addPineapple']))
                {
                    $addPineapple = 'no';
                }else{$addPineapple = $_REQUEST['addPineapple'];}
    
                if(!isset($_REQUEST['addAnchovies']))
                {
                    $addAnchovies = 'no';
                }else 
                {$addAnchovies = $_REQUEST['addAnchovies'];}
                
                //Do the Same to Student Discount
                if(!isset($_REQUEST['student']))
                {
                    $student = 'no';
                }else{$student = $_REQUEST['student'];}
        
    if(! $connection ) // Cant Connect Show Error 
    {
      die('Could not connect: ' . mysqli_error($connection));
    } 
    // Insert SQL Query 
    $sql ="INSERT INTO orders
            (
            id,
            order_id,
             student, 
             firstname, 
             lastname,
              email, 
              address,
               phone,
                price,
                 size,
                  anchovies,
                   pineapples,
                    pepperoni,
                     olives,
                      onions,
                      peppers,
                      createddatetime
                    )
            VALUES
            (
            '',
            '$order_id',
            '$student',
            '$fname',
            '$lname',
            '$emailAddress',
            '$address',
            '$phoneNo',
            '18',
            '$pizzaSize',
            '$addAnchovies',
            '$addPineapple',
            '$addPepperoni',
            '$addOlives',
            '$addOnion',
            '$addPeppers',
            '$my_date'
            '1'
            )";
            
    $retval = mysqli_query($connection, $sql); // Run Query
    if(! $retval) // if Php SQL cant run Query Show Error 
                {
                die('Could Not Place Order: ' . mysqli_error($connection));    
                }    
    echo"<html>";
    echo"<title>Order Receipt</title>"; 
    echo"<head></head>";
    echo"<body>";
    echo"<h2><center>Your Order Receipt</h2></center>";
    echo "<center><a href='vieworder.php?order_id=$order_id'>http://vieworder.php?order_id=$order_id</a></center><br>";
    echo"<center>------------------------------------------------</center>";
    echo"<b><center>Order No." . " " . $order_id. "</b></center>";
    echo"<br />";
    echo"<center>" .$customerName."</center><br />";
    echo "<center>" .$address."</center><br />" ;
    echo"<center>".$emailAddress."</center><br />" ;
    echo"<center>".$phoneNo."</center><br />";
    echo"<center>------------------------------------------------</center>";
    echo"<br />";
    echo"<center>Pizza Size" . " ". $pizzaSize."</center><br />" ;
    echo"<center><b>Toppings</center></b><br />";
    echo"<center>Anchovies" . " ".$addAnchovies."</center><br />";
    echo"<center>Olives" . " ".$addOlives."</center><br />";
    echo"<center>Onion" . " ".$addOnion."</center><br />";
    echo"<center>Pepperoni" . " ".$addPepperoni."</center><br />";
    echo"<center>Pineapple" . " ".$addPineapple."</center><br />";
    echo"<center>Peppers" . " ".$addPeppers."</center><br />";
    echo"<br />";
    echo"<center>------------------------------------------------</center>";
    echo"<br />";
    echo"<center>Price" . " ". "€".$price."</center><br />";
    echo"<center>Student Discount" . " " .$student ."</center><br />";
    echo"<center>Order Time". " ".$my_date."</center>";
    }    
    
    if (!isset($_REQUEST['submit']))
    { //Open If !isset 
    
    $query = mysqli_query($connection,'SELECT * FROM orders WHERE order_id = "' . $_GET['order_id']. '"');
    
    if(! $query )
    { //Open Query Error 
      echo('Could not get data: ' . mysqli_error($connection)); // Could not find Order_id show Error Message 
    } // Close Query Error
    
     while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) // Run run fecth and display to user.
    { //open While 
        $row['id'];
        $row['order_id'];
        echo"<h2><center>Your Order Receipt</h2></center>";
        echo"<center>------------------------------------------------</center>";
        echo"<center>Order No : " .$row['order_id']. "  <br> </center>";
        echo"<center>Customer Name :".$row['firstname']. " " .$row['lastname']. "<br> </center>";
        echo"<center>Address :". $row['address']. " <br> </center>";
        echo"<center>Phone No :" .$row['phone'].  "<br> </center>";
        echo"<center>------------------------------------------------</center>";
        echo"<center>Pizza Size :".$row['size']."  <br> </center>";
        echo"<center>Toppings</center>";
        echo"<center>Anchovies  :". $row['anchovies']."  <br> </center>";
        echo"<center>Onion  :". $row['onions'].  "<br> </center>";
        echo"<center>Pepperoni  :".$row['pepperoni']."  <br> </center>";
        echo"<center>Pineapple :".$row['pineapples']."  <br> </center>";
        echo"<center>Peppers :".$row['peppers']." <br> </center>";
        echo"<center>Price :".$row['price']."  <br></center> ";
        echo"<center>Student Discount :".$row['student']." <br></center> ";
        echo"<center>Order Time :".$row['createddatetime']."  <br></center>";
    
    echo'<form action=" " method="POST">';
    echo'<button type="submit" name="up" value="up">Update order</button>';
    echo'<button type ="submit" name="delete" value="delete">delete</button>';
    echo"</form>";
    } // Close While
    
    
        // STICKY FORM TO ALLOW USER TO UPDATE INFORMATION 
    if (isset($_REQUEST['up']))
        {
            $query_sticky = mysqli_query($connection,'SELECT * FROM orders WHERE order_id = "' . $_GET['order_id'] . '"');
            if(! $query_sticky )
    {
      die('Could not get data: ' . mysqli_error($connection)); // Could not find Order_id show Error
    }//end die error 
    else
        (isset($_REQUEST['update']));
        {
    while($row = mysqli_fetch_array($query_sticky, MYSQLI_ASSOC))
    {
        $row['id'];
        
        echo '<form action=" "method="post">';
        echo "Your Order ID:"; 
        echo $row['order_id'];
        echo'<div id="pizzaSize">';
          echo"<h3>What Size of Pizza Would You Like? </h3>";    
     echo "Small";
     echo '<input id="small" type="radio" name="pizzaSize" onChange="redraw()" value="' . $row['size'] . '"' . ($row['size'] == 'small' ? ' checked="checked"' : '') . ' />';
        echo "Medium";
        echo'<input id="medium" type="radio" name="pizzaSize"  onChange="redraw()" value="' . $row['size'] . '"' . ($row['size'] == 'medium' ? ' checked="checked"' : '') . ' />';
        echo"Large";
      echo'<input id="large" type="radio" name="pizzaSize" onChange="redraw()" value="' . $row['size'] . '"' . ($row['size'] == 'large' ? ' checked="checked"' : '') . ' />';
      echo'</div>';
       echo'<br>';
             echo'<div id="ExtraToppings">';
         echo" <h3>Add Extra Toppings</h3>";
           echo' Anchovies';
          echo' <input id="anchovies" type="checkbox" name="addAnchovies"  onChange="redraw()" value="' . $row['anchovies'] . '"' . ($row['anchovies'] == 'y' ? ' checked="checked"' : '') . ' />';
      
    echo'Pineapple';
     echo'<input id="pineapple" type="checkbox" name="addPineapple"  onChange="redraw()" value="' . $row['pineapples'] . '"' . ($row['pineapples'] == 'y' ? ' checked="checked"' : '') . ' />';
       
          echo' Pepperoni';
          echo' <input id="pepperoni" type="checkbox" name="addPepperoni" onChange="redraw()" value="' . $row['pepperoni'] . '"' . ($row['pepperoni'] == 'y' ? ' checked="checked"' : '') . ' />';    
           echo' Olives';
           echo'        <input id="olives" type="checkbox" name="addOlives" onChange="redraw()" value="' . $row['olives'] . '"' . ($row['olives'] == 'y' ? ' checked="checked"' : '') . ' />';     
            echo'Onion';
           echo' <input id="onion" type="checkbox" name="addOnion" onChange="redraw()" value="' . $row['onions'] . '"' . ($row['onions'] == 'y' ? ' checked="checked"' : '') . ' />';        
           echo' Peppers';
          echo'  <input id="peppers" type="checkbox" name="addPeppers"  onChange="redraw()" value="' . $row['peppers'] . '"' . ($row['peppers'] == 'y' ? ' checked="checked"' : '') . ' />';
          echo'         </div>';
       echo'<div id="totalPrice">';
         echo'<h3>Total Price is: €<span id="pricetext">18</span></h3>';
         echo'</div>';
         echo' <div id="yourDetails">';
          echo'  <h3>Enter your  details</h3>';
            echo'Name:';
              echo'<input name="customerName" id="cname" type="text" required   value="'.$row['firstname']. " " .$row['lastname']. '" />';
            echo' <br/>';
             echo' <br/>';
                echo' Address:';
    echo'<textarea name="address" id = "caddress" type="text" rows="5" cols="30" required value="'.$row['address'].'" ></textarea>';
            echo'<br/>';
           echo' <br/>';
           echo' Email Address:';
          echo' <input name="emailAddress" type="email" required  value="'.$row['email']. '" />';
           echo' <br/>';
           echo' <br/>';
           echo' <br/>'; 
           echo'Phone Number:';
          echo'   <input name="phoneNo" id="phoneNumber" type="text" required  value="'.$row['phone']. '" />';
        echo'    <br/>';
            echo' <br/>';
            echo'Tick here if you are student:';
            echo'<input type="checkbox" id="studentdiscount" name="student" onChange="redraw()"  value="' . $row['student'] . '"' . ($row['student'] == 'y' ? ' checked="checked"' : '') . ' />';
            echo'</div>';
          echo'<button type="update" name="update" value="update" >update</button>';
          echo'<input type="hidden" name="id" value="'.$row['order_id'].'"/>';
      echo'<div id="Submit">';
        echo'</form>';
        echo'<form action=" " method="delete">';
        echo'</form>';
    }//close if 
        }
    } // Close While 
    
    
    //Delete User
    
    if(isset($_POST['delete']))
        {
        $query_delete = mysqli_query($connection,'DELETE FROM orders WHERE order_id = "'.$_GET['order_id'].'"');
        mysqli_query($connection, $query_delete);
        if ($query_delete)
        {
            echo "Deleted Successfully";
        }
        }
        
        
    /**
            UPDATE USER INFORMATION.. 
    **/
    
    if (isset($_REQUEST['update']))
    {
         $updateDB = "UPDATE orders SET 
            student ='$_POST[student]', 
            firstname='John', lastname='wallace',
            email = '$_POST[emailAddress]', address = '$_POST[address]',
            phone = '$_POST[phoneNo]', size='$_POST[pizzaSize]', 
            anchovies = '$_POST[addAnchovies]', pineapples ='$_POST[addPineapple]',
            pepperoni = '$_POST[addPepperoni]', olvies='$_POST[addOlives]', 
            onions = '$_POST[addOnion]', peppers='$_POST[addOnion]'
            WHERE order_id ='$_REQUEST[order_id]'";
            mysqli_query($connection, $updateDB);
           if(! $updateDB )
           {
              die('Could not get data: ' . mysqli_error($connection)); // Could not find Order_id show Error 
           }
    }//end die error 
        }//end update..     
    
    ?>

EDIT
This post has been reformatted by enclosing the code block in 3 backticks
```
on their own lines.

You haven’t made the changes I pointed out to you yet ?

    if (isset($_REQUEST['update']))
    {
         $updateDB = "UPDATE orders SET 
            student ='$_POST[student]', 
            firstname='John', lastname='wallace',
            email = '$_POST[emailAddress]', address = '$_POST[address]',
            phone = '$_POST[phoneNo]', size='$_POST[pizzaSize]', 
            anchovies = '$_POST[addAnchovies]', pineapples ='$_POST[addPineapple]',
            pepperoni = '$_POST[addPepperoni]', olvies='$_POST[addOlives]', 
            onions = '$_POST[addOnion]', peppers='$_POST[addOnion]'
            WHERE order_id ='$_REQUEST[order_id]'";
            mysqli_query($connection, $updateDB);
           if(! $updateDB )
           {
              die('Could not get data: ' . mysqli_error($connection)); // Could not find Order_id show Error 
           }

Maybe it’s looking for an index “no”?

                if(!isset($_REQUEST['addPepperoni']))
                {
                    $addPepperoni = 'no';
                }else{$addPepperoni = $_REQUEST['addPepperoni'];}

That if Statment is Passing Y to the Database if its Clicked Else it Sends N to the Database.

what seems to be the problem is the
Line 151

$query = mysqli_query($connection,'SELECT * FROM orders WHERE order_id = "' . $_GET['order_id']. '"');

From my Index.php when i hit Submit it goes to the view page, i am giving a Hyper Link with
vieworder.php?order_id=55258704d5c26

So when you click on the Hyper Link you are giving the option to Update or Delete… when i Hit the Update as its a Sticky Form its droping the ?order_id=55258704d5c26 which is causing the
Line 151 gving out that.

Notice: Undefined index: order_id in C:\wamp\www\vieworder.php on line 151

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.