Killersites Community: Basic PHP System: View/Edit/Delete/Add Records - Killersites Community

Jump to content

  • (6 Pages)
  • +
  • « First
  • 4
  • 5
  • 6
  • You cannot start a new topic
  • You cannot reply to this topic

Basic PHP System: View/Edit/Delete/Add Records

#151 User is offline   Silisav 

  • View blog
  • Group: New Members
  • Posts: 6
  • Joined: 23-September 12

Posted 23 September 2012 - 11:44 AM

View PostBen, on 23 September 2012 - 04:20 PM, said:

Actually after giving it more thought, I think I know what is going on -- "from" is a reserved word in MySQL (see http://dev.mysql.com...ed-words.html). Personally, I would change that column in the database to something else, or use backticks to escape it (see the first solution on http://serverfault.c...e-field-names).


hmmm, this makes more sense I will try to change it
0

#152 User is offline   Silisav 

  • View blog
  • Group: New Members
  • Posts: 6
  • Joined: 23-September 12

Posted 23 September 2012 - 12:14 PM

View PostBen, on 23 September 2012 - 04:20 PM, said:

Actually after giving it more thought, I think I know what is going on -- "from" is a reserved word in MySQL (see http://dev.mysql.com...ed-words.html). Personally, I would change that column in the database to something else, or use backticks to escape it (see the first solution on http://serverfault.c...e-field-names).


Thanks a lot Ben, it works!
It turns out that both 'from' and 'to' are reserved words in MySQL.
0

#153 User is offline   Silisav 

  • View blog
  • Group: New Members
  • Posts: 6
  • Joined: 23-September 12

Posted 02 October 2012 - 07:47 AM

Hi again,

I have been trying to do something for the past 2 days with no luck.
I would appreciate it if someone could help.

Let's say I have "id" "first name", "last name", "amount" as columns.
I have created a page (similar to view.php) where I have all the records of a specific first and last name.
For example 'first name':John and 'last name':Smith with different 'amount' for each row.
I want the "add new record" link to lead to a page where the first and last name are by default John and smith and you only have to insert the new amount.

I tried to do it in the same way as the edit.php, getting the first and last name from the URL (new.php?firstname=John&lastname=Smith) but it didn't really work.

Any ideas?
thanks
0

#154 User is offline   Ben 

  • View blog
  • Group: Administrators
  • Posts: 5,409
  • Joined: 19-December 08
  • LocationChico, CA

Posted 02 October 2012 - 09:50 AM

View PostSilisav, on 02 October 2012 - 05:47 AM, said:

I tried to do it in the same way as the edit.php, getting the first and last name from the URL (new.php?firstname=John&lastname=Smith) but it didn't really work.

How about you start a new topic, and include the code you are using? I can't help much without seeing the code. Assuming you are coding it correctly, what you want should be possible.
Benjamin Falk | Falken Creative : Twitter : KillerSites Screencast Blog
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
0

#155 User is offline   scottk 

  • View blog
  • Group: New Members
  • Posts: 1
  • Joined: 31-October 12

Posted 31 October 2012 - 01:15 AM

Thanks Ben for this great code, its helped me "learn by doing". Can you show what code would be required to sort the table that is echoed out so that the table is displayed with the rows sorted by record id? This way the rows will stay in order of creation and the newest record will always be at the end. Thanks for your help!
0

#156 User is offline   aksun 

  • View blog
  • Group: New Members
  • Posts: 5
  • Joined: 16-February 13
  • LocationOulu, Finland

Posted 16 February 2013 - 03:43 AM

Here's my problem. When somebody adds new records, it goes at the bottom of the table after submitting. How can I change that the record is going to top of the table?
0

#157 User is offline   aksun 

  • View blog
  • Group: New Members
  • Posts: 5
  • Joined: 16-February 13
  • LocationOulu, Finland

Posted 03 March 2013 - 06:19 AM

Sorry. I just look at it and the record was going top of the table.
0

#158 User is offline   administrator 

  • View blog
  • View gallery
  • Group: Administrators
  • Posts: 4,017
  • Joined: 19-December 08
  • LocationMontreal Canada

Posted 03 March 2013 - 12:11 PM

You could effect the sort order using simple SQL:

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC


So a specific example would be:

SELECT * FROM Persons
ORDER BY LastName DESC


DESC = descending
ASC = ascending

Stef
Web design training videos by subscription OR from our video tutorial store.
0

#159 User is offline   aksun 

  • View blog
  • Group: New Members
  • Posts: 5
  • Joined: 16-February 13
  • LocationOulu, Finland

Post icon  Posted 23 March 2013 - 05:23 AM

Thanks stef!
0

#160 User is offline   stephen 

  • View blog
  • Group: New Members
  • Posts: 1
  • Joined: 19-April 13

Posted 19 April 2013 - 11:56 PM

Hey, great code, i have been trying to add another column for the phone number and when i change the code it doesn't update or add records, it doesn't give any errors just nothing gets entered in the database. Head is wrecked trying to fix it. Can anybody see where i am going wrong.

<?php
        /*
                Allows the user to both create new records and edit existing records
        */

        // connect to the database
        include("connect-db.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 = '', $last ='', $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"/>
                        </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 red; color:red'>" . $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>First Name: *</strong> <input type="text" name="firstname"
                                                value="<?php echo $first; ?>"/><br/>
                                        <strong>Last Name: *</strong> <input type="text" name="lastname"
                                                value="<?php echo $last; ?>"/>
                                        <strong>Phone Number: *</strong> <input type="text" name="phonenumber"
                                                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
                                $id = $_POST['id'];
                                $firstname = htmlentities($_POST['firstname'], ENT_QUOTES);
                                $lastname = htmlentities($_POST['lastname'], ENT_QUOTES);
                                $phonenumber = htmlentities($_POST['phonenumber'], ENT_QUOTES);
                                
                                
                                // check that firstname and lastname are both not empty
                                if ($firstname == '' || $lastname == '' || $phonenumber == '')
                                {
                                        // if they are empty, show an error message and display the form
                                        $error = 'ERROR: Please fill in all required fields!';
                                        renderForm($firstname, $lastname, $phonenumber, $error, $id);
                                }
                                else
                                {
                                        // if everything is fine, update the record in the database
                                        if ($stmt = $mysqli->prepare("UPDATE players SET firstname = ?, lastname = ?, phonenumber = ? WHERE id= ?"))
                                        {
                                                $stmt->bind_param("ssi", $firstname, $lastname, $phonenumber, $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: 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, $lastname, $phonenumber);
                                        $stmt->fetch();
                                        
                                        // show the form
                                        renderForm($firstname, $lastname, $phonenumber, 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);
                        $lastname = htmlentities($_POST['lastname'], ENT_QUOTES);
                        $phonenumber = htmlentities($_POST['phonenumber'], ENT_QUOTES);
                        
                        
                        // check that firstname and lastname are both not empty
                        if ($firstname == '' || $lastname == ''  || $phonenumber == '')
                        {
                                // if they are empty, show an error message and display the form
                                $error = 'ERROR: Please fill in all required fields!';
                                renderForm($firstname, $lastname, $phonenumber, $error);
                        }
                        else
                        {
                                // insert the new record into the database
                                if ($stmt = $mysqli->prepare("INSERT players (firstname, lastname, phonenumber) VALUES (?, ?, ?)"))
                                {
                                        $stmt->bind_param("ss", $firstname, $lastname, $phonenumber);
                                        $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();
?>

0

Share this topic:


  • (6 Pages)
  • +
  • « First
  • 4
  • 5
  • 6
  • You cannot start a new topic
  • You cannot reply to this topic

3 User(s) are reading this topic
0 members, 3 guests, 0 anonymous users