Jump to content

500 error php mysqli cannot update or edit simple table


webdevdea
Go to solution Solved by webdevdea,

Recommended Posts

http://dandewebwonders.com/view.php

Here is the link so you can try it yourself and seeĀ 

here is my model.php file


<?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($first = '', $address ='', $phone = '', $error = '', $id = '')
        { ?>
                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
                <html>
                        <head>  
                        	
                                <title>
                                        <?php if ($id != '') { 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 ($id != '') { 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 ($id != '') { ?>
                                                <input type="hidden" name="id" value="<?php echo $id; ?>" />
                                                <p>ID: <?php echo $id; ?></p>
                                        <?php } ?>
                                        
                                        
                                      <strong>Name: *</strong><input type="text" name="firstname"
                                                value="<?php echo $first; ?>"/><br/>
                                        <strong>Address: *</strong> <input type="text" name="addressname"
                                                value="<?php echo $address; ?>"/>
                                         <strong>Phone: </strong> <input type="text" name="phone"
                                                value="<?php echo $phone; ?>"/>
                                        <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['id']))
        {
                // 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['id']))
                        {
                                // get variables from the URL/form
                                //ENT_QUOTES - Decodes double and single quotes
                                $id = $_POST['id'];
                                $firstname = htmlentities($_POST['firstname'], ENT_QUOTES);
                                $addressname = htmlentities($_POST['addressname'], ENT_QUOTES);
                                $phone = htmlentities($_POST['phone'], ENT_QUOTES);
                                
                                // check that firstname and addressname are both not empty
                                if ($firstname == '' || $addressname == '')
                                {
                                        // if they are empty, show an error message and display the form
                                        $error = 'ERROR: Please fill in all required fields!';
                                        renderForm($firstname, $addressname, $phone, $error, $id);
                                }
                                else
                                {
                                        // if everything is fine, update the record in the database
                                        // bind statement string, string, string, integer
                                        if ($stmt = $mysqli->prepare("UPDATE players SET firstname = ?, addressname = ?, phone = ?
                                                WHERE id=?"))
                                        {
                                                $stmt->bind_param("sssi", $firstname, $addressname, $phone, $id);
                                                $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: dandewebwonders.com/view.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['id']) && $_GET['id'] > 0)
                        {
                                // get 'id' from URL
                                $id = $_GET['id'];
                                
                                // get the recod from the database
                                if($stmt = $mysqli->prepare("SELECT * FROM players WHERE id=?"))
                                {
                                        $stmt->bind_param("i", $id);
                                        $stmt->execute();
                                        
                                        $stmt->bind_result($id, $firstname, $addressname, $phone);
                                        $stmt->fetch();
                                        
                                        // show the form
                                        renderForm($firstname, $addressname, $phone, NULL, $id);
                                        
                                        $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: view.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
                        $firstname = htmlentities($_POST['firstname'], ENT_QUOTES);
                        $addressname = htmlentities($_POST['addressname'], ENT_QUOTES);
                        $phone = htmlentities($_POST['phone'], ENT_QUOTES);
                        
                        // check that firstname and addressname are both not empty
                        if ($firstname == '' || $addressname == '')
                        {
                                // if they are empty, show an error message and display the form
                                $error = 'ERROR: Please fill in all required fields!';
                                renderForm($firstname, $addressname, $phone, $error);
                        }
                        else
                        {
                                // insert the new record into the database
                                if ($stmt = $mysqli->prepare("INSERT players (firstname, addressname, phone) VALUES (?, ?, ?)"))
                                {
                                        $stmt->bind_param("sss", $firstname, $addressname, $phone);
                                        $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: view.php");
                        }
                        
                }
                // if the form hasn't been submitted yet, show the form
                else
                {
                        renderForm();
                }
        }
        
        // close the mysqli connection
        $mysqli->close();
?> 




Link to comment
Share on other sites

  • Solution

OK I checked the log, Its like a foreign language,Ā 

[05-Nov-2013 06:10:58] PHP Fatal error: Ā Call to a member function query() on a non-object in /home3/aundie/public_html/dandewebwonders.com/project/viewpatient.php on line 18

Ā 

Here is the code for viewpatient.phpā€¦ I got this to work on my school server but I cannot get it to work on my ownā€¦ SMDH

<!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='project/model.php?patientiId=" . $row->patientId . "'>Edit</a></td>";
                                                echo "<td><a href='project/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="project/model.php">Add New Patient Record</a>
        </body>
</html>
 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.