www.webdeveloper.com
Results 1 to 11 of 11

Thread: Can't update sql row with my php code

  1. #1
    Join Date
    Apr 2009
    Posts
    107

    Can't update sql row with my php code

    So I am trying to edit a guestbook on a wedding site. I have a page that shows all of my guests who have registered for the wedding. Next to each row I have an edit and delete button. The delete button works, but the entire edit process does not. When I click edit, I then go to the edit.php page, see below:
    PHP Code:
    <?php
    session_start
    ();
    if (@
    $_SESSION['auth'] !="yes")
    {
        
    header("Location: login_form.php");
        exit();
    }
    ?>
    <?php 
    $host
    ="localhost"
    $username="username"
    $password="password"
    $db_name="database"

    // Connect to server and select database.
    mysql_connect("$host""$username""$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");

    $id $_GET['id']; 
    $results mysql_query("SELECT * FROM guests WHERE id = '$id'"); 
    $rows mysql_fetch_assoc($results); 

    ?>
    <html>
    <head>
        <link href="style.css" type="text/css" rel="stylesheet">
    </head>
    <body>

    <div align="center">
    <form action="edit_process.php" method="post">
    <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
    <table width="70%" cellpadding="0" cellspacing="0" border="0">
        <tr>
            <td width="30%"><table width="100%" cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td>Guest 1 <input type="text" value="<? echo $rows['guest_1']; ?>" name="guest_1"></td>
                </tr>
                <tr>
                    <td>Guest 2 <input type="text" value="<? echo $rows['guest_2']; ?>" name="guest_2"></td>
                </tr>
                <tr>
                    <td>Guest 3 <input type="text" value="<? echo $rows['guest_3']; ?>" name="guest_3"></td>
                </tr>
                <tr>
                    <td>Guest 4 <input type="text" value="<? echo $rows['guest_4']; ?>" name="guest_4"></td>
                </tr>
                <tr>
                    <td>Guest 5 <input type="text" value="<? echo $rows['guest_5']; ?>" name="guest_5"></td>
                </tr>
            </table></td>
            
            <td width="70%"><table width="100%" cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td>Will Be Attending <input type="text" value="<? echo $rows['attending']; ?>" name="vegetarian"></td>
                </tr>
                <tr>
                    <td>Number Vegetarian Meals <input type="text" value="<? echo $rows['vegetarian']; ?>" name="vegetarian"></td>
                </tr>
                <tr>
                    <td>How Many People Attending<input type="text" value="<? echo $rows['total_guests']; ?>" name="vegetarian"></td>
                </tr>
                <tr>
                    <td>Email Address <input type="text" value="<? echo $rows['email']; ?>" name="vegetarian"></td>
                </tr>
            </table></td>
        </tr>
        <tr>
            <td><input type="submit" value="Update"></td>
        </tr>
    </table>
    </form>
    </div>
    </body>
    </html>
    In the URL bar it displays the correct id and all of the information pops up in the correct boxes. Here is where it doesn't work. When I click update, it doesn't update on the sql server. Here is the code for the edit_process.php :
    PHP Code:
    <?php

    $host
    ="localhost"
    $username="username"
    $password="password"
    $db_name="database"

    header("Location: rsvp_view.php");

    // Connect to server and select database.
    mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    $id $_POST["id"];
    $guest_1 $_POST['guest_1'];
    $guest_2 $_POST['guest_2'];
    $guest_3 $_POST['guest_3'];
    $guest_4 $_POST['guest_4'];
    $guest_5 $_POST['guest_5'];
    $attending $_POST['attending'];
    $vegetarian $_POST['vegetarian'];
    $total_guests $_POST['total_guests'];
    $email $_POST['email'];

    $query 'SELECT * FROM $tbl_name WHERE id = $id';

    mysql_query("UPDATE guests SET guest_1='$guest_1',guest_2='$guest_2',guest_3='$guest_3'.guest_4='$guest_4',guest_5='$guest_5',attending='$attending',vegetarian='$vegetarian',total_guests='$total_guests',email='email' WHERE id=$id"); 
    echo 
    mysql_error();
    ?>

    Any ideas of why it isn't working. This is the last piece of the puzzle for this site, so any help will be appreciated.

  2. #2
    Join Date
    Jun 2009
    Posts
    111
    PHP Code:
    header("Location: rsvp_view.php"); 
    Is there a reason you're redirecting them before it even runs your script? :P

  3. #3
    Join Date
    Apr 2009
    Posts
    107
    no, I wasn't aware I am, could that be the problem?

  4. #4
    Join Date
    Jun 2009
    Posts
    111
    Quote Originally Posted by themonkey40 View Post
    no, I wasn't aware I am, could that be the problem?
    Yes. Move that header() down to the bottom of the file.

  5. #5
    Join Date
    Apr 2009
    Posts
    107
    I moved the header, see below:
    PHP Code:
    <?php

    $host
    ="localhost"
    $username="username"
    $password="password"
    $db_name="database"
    $tbl_name="table";

    // Connect to server and select database.
    mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    $id $_POST["id"];
    $guest_1 $_POST['guest_1'];
    $guest_2 $_POST['guest_2'];
    $guest_3 $_POST['guest_3'];
    $guest_4 $_POST['guest_4'];
    $guest_5 $_POST['guest_5'];
    $attending $_POST['attending'];
    $vegetarian $_POST['vegetarian'];
    $total_guests $_POST['total_guests'];
    $email $_POST['email'];

    $query 'SELECT * FROM $tbl_name WHERE id = $id';

    mysql_query("UPDATE guests SET guest_1='$guest_1',guest_2='$guest_2',guest_3='$guest_3',guest_4='$guest_4',guest_5='$guest_5',attending='$attending',vegetarian='$vegetarian',total_guests='$total_guests',email='email' WHERE id=$id"); 
    echo 
    mysql_error();

    header("Location: rsvp_view.php");
    ?>
    But now I get this error when the page processes: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
    Warning: Cannot modify header information - headers already sent by (output started at /home/content/m/a/r/markandmichell/html/manager/edit_process.php:27) in /home/content/m/a/r/markandmichell/html/manager/edit_process.php on line 29"

    Any ideas

  6. #6
    Join Date
    Jun 2009
    Posts
    111
    Try changing your query to this...
    PHP Code:
    mysql_query("UPDATE guests SET guest_1='{$guest_1}', guest_2='{$guest_2}', guest_3='{$guest_3}', guest_4='{$guest_4}', guest_5='{$guest_5}', attending='{$attending}', vegetarian='{$vegetarian}', total_guests='{$total_guests}', email='{$email}' WHERE id='{$id}'"); 

  7. #7
    Join Date
    Apr 2009
    Posts
    107
    Did that:
    PHP Code:
    // Connect to server and select database.
    mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    $id $_POST["id"];
    $guest_1 $_POST['guest_1'];
    $guest_2 $_POST['guest_2'];
    $guest_3 $_POST['guest_3'];
    $guest_4 $_POST['guest_4'];
    $guest_5 $_POST['guest_5'];
    $attending $_POST['attending'];
    $vegetarian $_POST['vegetarian'];
    $total_guests $_POST['total_guests'];
    $email $_POST['email'];

    $query 'SELECT * FROM $tbl_name WHERE id = $id';

    mysql_query("UPDATE guests SET guest_1='{$guest_1}', guest_2='{$guest_2}', guest_3='{$guest_3}', guest_4='{$guest_4}', guest_5='{$guest_5}', attending='{$attending}', vegetarian='{$vegetarian}', total_guests='{$total_guests}', email='{$email}' WHERE id='{$id}'");
    echo 
    mysql_error();

    header("Location: rsvp_view.php"); 
    I no longer get the error, but the database still isn't updated. I can't think of anything else that is going wrong.

  8. #8
    Join Date
    Jun 2009
    Posts
    111
    Let's see if the ID is getting passed right. Put this at the top of the file:
    PHP Code:
    exit($_POST['id']); 

  9. #9
    Join Date
    Apr 2009
    Posts
    107
    When I put that at the top of the edit_process.php file, nothing happens. The page just goes white. Now something interesting I discovered. When I am on the rsvp page and click edit, the id of the row that I want to edit appears in the url "manager/edit.php?id=7" but when I make changes and click update, this is what appears in the url "manager/edit_process.php". The ID information all but disappears when it is trying to update the row.

    I am not sure if you know of an easier way of accomplishing this, as you have seen my code. I am open to suggestions.

  10. #10
    Join Date
    Jun 2009
    Posts
    111
    Quote Originally Posted by themonkey40 View Post
    When I put that at the top of the edit_process.php file, nothing happens.
    Ahh. Yeah, that's the point. It means your ID isn't being passed through in the hidden form variable-- if it were, you'd have seen it on that page. Most likely it's not being sent to edit_process.php at all.

    EDIT: And yes, that's the problem. This line in your HTML:
    HTML Code:
    <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
    You have $row['id'] instead of $rows['id'].
    Last edited by Four Staples; 06-09-2009 at 07:06 PM.

  11. #11
    Join Date
    Apr 2009
    Posts
    107
    Man you are good. That one "s" is what was keeping this from working the whole time. A thousand thanks man, you really helped me out on this one.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.14104 seconds
  • Memory Usage 3,017KB
  • Queries Executed 15 (?)
More Information
Template Usage (35):
  • (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
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_html
  • (7)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (11)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (11)postbit
  • (11)postbit_onlinestatus
  • (11)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./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
  • ./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
  • 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
  • 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