Jump to content

And again Update and Insert


webdevdea

Recommended Posts

I finally got some of the kinks out, obviously I had my paths crossed lol .. 

anyway now I need to figure out why all my fields are not showing values, and my update and insert are not working.. 

Here is the code

<? ob_start(); ?>
<?php
        /*
                Allows the user to both create new model and edit existing model
        */

        // connect to the database
        include("connectme.php");

        // creates the new/edit record form
        // since this form is used multiple times in this file, I have made it a function that is easily reusable
        function renderForm( $infantPatientid ='', $roomNumberId = '', $patientFirstname = '', $patientLastname = '', $patientAddress = '', $patientPhone = '', $patientEmergencycontact = '', $error = '', $patientId = '')
        { ?>
                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
                <html>
                        <head>  
                        	
                                <title>
                                        <?php if ($patientId != '') { echo "Edit Record"; } else { echo "New Record"; } ?>
                                </title>
                                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                                <link rel="stylesheet" type="text/css" href="Styles.css" />
                        </head>
                        <body>
                                <h1><?php if ($patientId != '') { echo "Edit Record"; } else { echo "New Record"; } ?></h1>
                                <?php if ($error != '') {
                                        echo "<div style='padding:4px; border:1px solid pink; color:pink'>" . $error
                                                . "</div>";
                                } ?>
                                
                                <form action="" method="post">
                                <div>
                                        <?php if ($patientId != '') { ?>
                                                <input type="hidden" name="patientId" value="<?php echo $patientId; ?>" />
                                                <p>ID: <?php echo $patientId; ?></p><br/>
                                        <?php } ?>
                                        
                                        
                                      <strong>InfantPatientid: </strong><input type="text" name="infantPatientid"
                                                value="<?php echo $infantPatientid; ?>"/><br/>
                                        <strong>roomNumberId: </strong> <input type="text" name="roomNumberId"
                                                value="<?php echo $roomNumberId; ?>"/><br/>
                                         <strong>patientFirstname: </strong> <input type="text" name="patientFirstname"
                                                value="<?php echo $patientFirstname; ?>"/><br/>
                                        <strong>patientLastname: </strong> <input type="text" name="patientLastname"
                                                value="<?php echo $patientLastname; ?>"/><br/>
                                        <strong>patientAddress: </strong> <input type="text" name="patientAddress"
                                                value="<?php echo $patientAddress; ?>"/><br/>
                                        <strong>patientPhone: </strong> <input type="text" name="patientPhone"
                                                value="<?php echo $patientPhone; ?>"/><br/>
                                        <strong>patientEmergencycontact: </strong> <input type="text" name="patientEmergencycontact"
                                                value="<?php echo $patientEmergencycontact; ?>"/><br/>
                                       
                                                
                                        <p>* required</p>
                                        <input type="submit" name="submit" value="Submit" />
                                </div>
                                </form>
                        </body>
                </html>
                
        <?php }



        /*

           EDIT RECORD

        */
        // if the 'id' variable is set in the URL, we know that we need to edit a record
        if (isset($_GET['patientId']))
        {
                // if the form's submit button is clicked, we need to process the form
                if (isset($_POST['submit']))
                {
                        // make sure the 'id' in the URL is valid
                        if (is_numeric($_POST['patientId']))
                        {
                                // get variables from the URL/form
                                //ENT_QUOTES - Decodes double and single quotes
                                $patientId = $_POST['patientId'];
                                $infantPatientid = htmlentities($_POST['infantPatientid'], ENT_QUOTES);
                                 $roomNumberId = htmlentities($_POST['roomNumberId'], ENT_QUOTES);
                                $patientFirstname = htmlentities($_POST['patientFirstname'], ENT_QUOTES);
                                  $patientLastname = htmlentities($_POST['patientLastname'], ENT_QUOTES);
                                    $patientAddress = htmlentities($_POST['patientAddress'], ENT_QUOTES);
                                      $patientPhone = htmlentities($_POST['patientPhone'], ENT_QUOTES);
                                        $patientEmergencycontact = htmlentities($_POST['patientEmergencycontact'], ENT_QUOTES);
                                         
                                          
                                
                                // check that firstname and addressname are both not empty
                                if ($patientFirstname == '' || $patientLastname == '')
                                {
                                        // if they are empty, show an error message and display the form
                                        $error = 'ERROR: Please fill in all required fields!';
                                        renderForm($infantPatientid, $roomNumberId, $patientFirstname, $patientLastname, $patientAddress, $patientPhone, $patientEmergencycontact, $error, $id);
                                }
                                else
                                {
                                        // if everything is fine, update the record in the database
                                        // bind statement string, string, string, integer
                                        if ($stmt = $mysqli->prepare("UPDATE patient SET infantPatientid = ?, roomNumberId = ?, patientFirstname = ?, patientLastname = ?, patientAddress = ?, patientPhone = ?, patientEmergencycontact = ?
                                                WHERE patientId=?"))
                                        {
                                                $stmt->bind_param("issssssi", $infantPatientid, $roomNumberId, $patientFirstname, $patientLastname, $patientAddress, $patientPhone, $patientEmergencycontact, $patientId);
                                                $stmt->execute();
                                                $stmt->close();
                                        }
                                        // show an error message if the query has an error
                                        else
                                        {
                                                echo "ERROR: could not prepare SQL statement.";
                                        }
                                        
                                        // redirect the user once the form is updated
                                        
                                       header("Location: viewpatient.php");
                                }
                        }
                        // if the 'id' variable is not valid, show an error message
                        else
                        {
                                echo "Error!";
                        }
                }
                // if the form hasn't been submitted yet, get the info from the database and show the form
                else
                {
                        // make sure the 'id' value is valid
                        if (is_numeric($_GET['patientId']) && $_GET['patientId'] > 0)
                        {
                                // get 'id' from URL
                                $patientId = $_GET['patientId'];
                                
                                // get the recod from the database
                                if($stmt = $mysqli->prepare("SELECT * FROM patient WHERE patientId=?"))
                                {
                                        $stmt->bind_param("i", $patientId);
                                        $stmt->execute();
                                        
                                        $stmt->bind_result($patientId, $infantPatientid, $roomNumberId, $patientFirstname, $patientLastname, $patientAddress, $patientPhone, $patientEmergencycontact);
                                        $stmt->fetch();
                                        
                                        // show the form
                                        renderForm($infantPatientid, $roomNumberId, $patientFirstname, $patientLastname, $patientAddress, $patientPhone, $patientEmergencycontact, NULL, $patientId);
                                        
                                        $stmt->close();
                                }
                                // show an error if the query has an error
                                else
                                {
                                        echo "Error: could not prepare SQL statement";
                                }
                        }
                        // if the 'id' value is not valid, redirect the user back to the view.php page
                        else
                        {
                                header("Location:viewpatient.php");
                        }
                }
        }



        /*

           NEW RECORD

        */
        // if the 'id' variable is not set in the URL, we must be creating a new record
        else
        {
                // if the form's submit button is clicked, we need to process the form
                if (isset($_POST['submit']))
                {
                        // get the form data
                        $infantPatientid = htmlentities($_POST['firstname'], ENT_QUOTES);
                        $roomNumberId = htmlentities($_POST['roomNumberid'], ENT_QUOTES);
                        $patientFirstname = htmlentities($_POST['roomNumberId'], ENT_QUOTES);
                          $patientLastname = htmlentities($_POST['patientLastname'], ENT_QUOTES);
                            $patientAddress = htmlentities($_POST['patientAddress'], ENT_QUOTES);
                              $patientPhone = htmlentities($_POST['patientPhone'], ENT_QUOTES);
                                $patientEmergencycontact = htmlentities($_POST['patientEmergencycontact'], ENT_QUOTES);
                        
                        // check that firstname and addressname are both not empty
                        if ($patientFirstname == '' || $patientLastname == '')
                        {
                                // if they are empty, show an error message and display the form
                                $error = 'ERROR: Please fill in all required fields!';
                                renderForm($infantPatientid, $roomNumberId, $patientFirstname, $patientLastname, $patientAddress, $patientPhone, $patientEmergencycontact, $error);
                        }
                        else
                        {
                                // insert the new record into the database
                                if ($stmt = $mysqli->prepare("INSERT patient (infantPatientid, roomNumberId, patientFirstname, patientLastname, patientAddress, patientPhone, patientEmergencycontact) VALUES (?, ?, ?, ?, ?, ?, ?)"))
                                {
                                        $stmt->bind_param("issssss", $infantPatientid, $roomNumberId, $patientFirstname, $patientLastname, $patientAddress,$patientPhone,$patientEmergencycontact);
                                        $stmt->execute();
                                        $stmt->close();
                                }
                                // show an error if the query has an error
                                else
                                {
                                        echo "ERROR: Could not prepare SQL statement.";
                                }
                                
                                // redirec the user
                                header("Location: viewpatient.php");
                        }
                        
                }
                // if the form hasn't been submitted yet, show the form
                else
                {
                        renderForm();
                }
        }
        
        // close the mysqli connection
        $mysqli->close();
?> 


<? ob_flush(); ?>

Here is the LInk

http://dandewebwonders.com/project/viewpatient.php

Link to comment
https://forums.phpfreaks.com/topic/283622-and-again-update-and-insert/
Share on other sites

I have changed the "s" to an "i" for the emergency contact, I am not sure if BIGINT is an "s" or an "i" also I am not sure if that is what I am supposed to use for phone numbers? I am new to this trying to learn.. anyway I have to do a lunch meeting, I will check back for more advice

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
        <head>  
                <title>My Patients</title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        </head>
        <body>
                
                <h1>My Patients</h1>
                
                <p><b>View  All Patients</b> </p>
                
                <?php
                        // connect to the database
                        include('connectme.php');
                        
                        // get the records from the database
                        if ($result = $mysqli->query("SELECT * FROM patient ORDER BY patientId"))
                        {
                                // display records if there are records to display
                                if ($result->num_rows > 0)
                                {
                                        // display records in a table
                                        echo "<table border='1' cellpadding='10'>";
                                        
                                        // set table headers
                                        echo "<tr><th>Patient ID</th> <th>InfantId</th> <th>RoomNumber</th> <th>FirstName</th> <th>LastName</th> <th>Address</th> <th>Phone</th> <th>Emergency Contact</th> <th></th> <th></th> </tr>";
                                        
                                        while ($row = $result->fetch_object())
                                        {
                                                // set up a row for each record
                                                echo "<tr>";
                                                echo "<td>" . $row->patientId . "</td>";
                                                echo "<td>" . $row->infantPatientid . "</td>";
                                                echo "<td>" . $row->roomNumberId . "</td>";
                                                echo "<td>" . $row->patientFirstname . "</td>";
                                                echo "<td>" . $row->patientLastname . "</td>";
                                                echo "<td>" . $row->patientAddress . "</td>";
                                                echo "<td>" . $row->patientPhone . "</td>";
                                                echo "<td>" . $row->patientEmergencycontact . "</td>";
                                                
                                                
                                                echo "<td><a href='model.php?patientId=" . $row->patientId . "'>Edit</a></td>";
                                                echo "<td><a href='deletepatient.php?patientId=" . $row->patientId . "'>Delete</a></td>";
                                                echo "</tr>";
                                        }
                                        
                                        echo "</table>";
                                }
                                // if there are no records in the database, display an alert message
                                else
                                {
                                        echo "No results to display!";
                                }
                        }
                        // show an error if there is an issue with the database query
                        else
                        {
                                echo "Error: " . $mysqli->error;
                        }
                        
                        // close database connection
                        $mysqli->close();
                
                ?>
                
                <a href="model.php">Add New Patient Record</a>
        </body>
</html>
 

 

I have changed the "s" to an "i" for the emergency contact, I am not sure if BIGINT is an "s" or an "i" also I am not sure if that is what I am supposed to use for phone numbers? I am new to this trying to learn.. anyway I have to do a lunch meeting, I will check back for more advice

s is for strings (char, varchar, text datatypes etc)

i is for integers (int, tinyint, bigint datatypes etc).

 

Int datatypes only allow positive/negative numbers and nothing else. if you are allowing spaces/dashes in your phone numbers (eg 1234 555 666 or 1334-555-666) then you need change the column type from an int to a varchar. Then you'd use s for bind value.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.