www.webdeveloper.com
Results 1 to 2 of 2

Thread: Values not going in DB a Further Step

Threaded View

  1. #1
    Join Date
    Apr 2012
    Posts
    23

    Values not going in DB a Further Step

    Okay,

    With this issue I am having trouble get the script to update or add new records. It produces no error messages:

    My DB:

    Column Type Null Default
    id int(11) No Autoincrement
    page varchar(255) Yes NULL
    title varchar(255) Yes NULL
    description varchar(255) Yes NULL
    keywords varchar(255) Yes NULL

    The code:
    view.php
    PHP Code:
    <?php
            
    // connect to the database
            
    include("connect.php");
            
    // A function that is easily reusable
            
    function renderForm($page ''$title ''$description ''$keywords ''$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>Page: *</strong> <input type="text" name="page"
                                                    value="<?php echo $page?>"/><br/>
                                            <strong>Title: *</strong> <input type="text" name="title"
                                                    value="<?php echo $title?>"/><br/>
                                            <strong>Description: *</strong> <input type="text" name="description"
                                                    value="<?php echo $description?>"/><br/>
                                            <strong>Keywords: *</strong> <input type="text" name="keywords"
                                                    value="<?php echo $keywords?>"/>
                                            <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, 
            
    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'];
                                    
    $page htmlentities($_POST['page'], ENT_QUOTES);
                                    
    $title htmlentities($_POST['title'], ENT_QUOTES);
                                    
    $description htmlentities($_POST['description'], ENT_QUOTES);
                                    
    $keywords htmlentities($_POST['keywords'], ENT_QUOTES);
                                    
    // check that page and title are both not empty
                                    
    if ($page == '' || $title == '')
                                    {
                                            
    // if they are empty, show an error 
                                            
    $error 'ERROR: Please fill in all required fields!';
                                            
    renderForm($page$title$description$keywords$error$id);
                                    }
                                    else
                                    {
                                            
    // if everything is fine, update 
                                            
    if ($stmt $mysqli->prepare("UPDATE URL SET page = ?, title = ?, description = ?, keywords = ?
                                                    WHERE id=?"
    ))
                                            {
                                                    
    $stmt->bind_param("ssi"$page$title$description$keywords$id);
                                                    
    $stmt->execute();
                                                    
    $stmt->close();
                                            }
                                            
    // show an error message if the query has an error
                                            
    else
                                            {
                                                    echo 
    "ERROR: could not prepare SQL statement.";
                                            }
                                            
    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,
                    
    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 record from the database
                                    
    if($stmt $mysqli->prepare("SELECT * FROM URL WHERE id=?"))
                                    {
                                            
    $stmt->bind_param("i"$id);
                                            
    $stmt->execute();
                                            
                                            
    $stmt->bind_result($id$page$title$description$keywords);
                                            
    $stmt->fetch();
                                            
                                            
    // show the form
                                            
    renderForm($page$title$description$keywordsNULL$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,
            
    else
            {
                    
    // if the form's submit button is clicked, we need to process the form
                    
    if (isset($_POST['submit']))
                    {
                            
    // get the form data
                            
    $page htmlentities($_POST['page'], ENT_QUOTES);
                            
    $title htmlentities($_POST['title'], ENT_QUOTES);
                            
    $description htmlentities($_POST['description'], ENT_QUOTES);
                            
    $keywords htmlentities($_POST['keywords'], ENT_QUOTES);
                            
                            
    // check that page and title are both not empty
                            
    if ($page == '' || $title == '')
                            {
                                    
    // if they are empty, show an error message 
                                    
    $error 'ERROR: Please fill in all required fields!';
                                    
    renderForm($page$title,$description$keywords$error);
                            }
                            else
                            {
                                    
    // insert the new record into the database
                                    
    if ($stmt $mysqli->prepare("INSERT URL (page, title, description, keywords) VALUES (?, ?)"))
                                    {
                                            
    $stmt->bind_param("ss"$page$title$description$keywords);
                                            
    $stmt->execute();
                                            
    $stmt->close();
                                    }
                                    
    // show an error if the query has an error
                                    
    else
                                    {
                                            echo 
    "ERROR: Could not prepare SQL statement.";
                                    }
                                    
                                    
    // redirect the user
                                    
    header("Location: view.php");
                            }    
                    }
                    else
                    {
                            
    renderForm();
                    }
            }
            
    // close the mysqli connection
            
    $mysqli->close();
    ?>
    Last edited by chrisguk; 04-23-2012 at 02:24 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.40048 seconds
  • Memory Usage 3,241KB
  • Queries Executed 14 (?)
More Information
Template Usage (30):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (2)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (2)postbit
  • (2)postbit_onlinestatus
  • (2)postbit_wrapper
  • (1)showthread_list
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (27):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./includes/functions_threadedmode.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids_threaded
  • showthread_threaded_construct_link
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates