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



Recent Articles