+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Update Entire SQL Table

  1. #1
    espfutbol98's Avatar
    espfutbol98 is offline x10 Sophmore espfutbol98 is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Zagreb... želim
    Posts
    200

    Update SQL Rows via PHP

    I was wondering how would I echo an entire table with a link on each row to the update page specifically for that row. I can already create a new row, view an entire table, but I have no clue how to do this.

    If you need any source code, just ask. Thanks for the help.
    Last edited by espfutbol98; 06-20-2009 at 10:27 PM. Reason: Undescriptive title

  2. #2
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: Update Entire SQL Table

    This full working example shows you how to delete records from a shopping cart.

    With some changes, you can make it update records instead of deleting them.

    Link to example

  3. #3
    espfutbol98's Avatar
    espfutbol98 is offline x10 Sophmore espfutbol98 is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Zagreb... želim
    Posts
    200

    Re: Update Entire SQL Table

    I have gotten close to finishing but when I get to update.php?rid=<WHATEVER> and click the update button, nothing happens. The information from the database goes in the inputs but it cant update:

    PHP Code:
    <?php

    $record 
    $_GET["rid"];

    $db_server "localhost";
    $db_name "database";
    $db_user "username";
    $db_pass "password";

    $con1 mysql_connect($db_server$db_user$db_pass) or die(mysql_error());
        
    mysql_select_db($db_name$con1);
        
    if (isset(
    $_POST['submit']) && $_POST['submit'] == "Update") {
    $query_update "UPDATE table SET " .
            
    "date = '" $_POST['date'] . "', " .
            
    "amount = '" $_POST['amount'] . "', " .
            
    "description = '" implode(", ",$_POST['description']) . "', " 
            
    "name = '" $_POST['name'] . "', " .
            
    "paid = '" $_POST['paid'] . "', " .
            
    "id = '" $_POST['id'] .
            
    "' WHERE id = '" $record "'";
    $result_update mysql_query($query_update) or die(mysql_error());
    header("Location: view_table.php");
    }
    else {
    $query "SELECT * FROM table " .
               
    "WHERE id = '" $record "'";
    $result mysql_query($query
        or die(
    mysql_error());
    $row mysql_fetch_array($result);
    $description explode(", "$row['description']);
    ?> 
    <h1>Update Debt</h1>
    Date: <input type="text" name="date" value="<?php echo $row['date']; ?>" /> <br />
    Amount: <input type="text" name="amount" value="<?php echo $row['amount']; ?>" /> <br />
    Description: <textarea name="description[]"><?php echo $row['description']; ?></textarea> <br />
    Name: <input type="text" name="name" value="<?php echo $row['name']; ?>" /> <br />
    Paid: <input type="text" name="paid" value="<?php echo $row['paid']; ?>" /> <br />
    ID: <input type="text" name="id" value="<?php echo $row['id']; ?>" /> <br />
    <input type="submit" name="submit" value="Update" />
    <?php
    }
    ?>

  4. #4
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Update Entire SQL Table

    Here is a working version, with some comments:
    • Always sanitize data from the user (with mysql_real_escape_string() )
    • Try separating your code and your content, always -> MVC
    • You need to wrap a form with the <form> tag for it to work
    • Be consistent with your indenting style

    PHP Code:
    <?php

    $record 
    $_GET["rid"];

    $db_server "localhost";
    $db_name "database";
    $db_user "username";
    $db_pass "password";

    $con1 mysql_connect($db_server$db_user$db_pass) or die(mysql_error());
        
    mysql_select_db($db_name$con1);

    $record mysql_real_escape_string($record);
        
    if (isset(
    $_POST['submit']) && $_POST['submit'] == "Update") {
        
    $query_update "UPDATE table SET " .
                
    "date = '" $_POST['date'] . "', " .
                
    "amount = '" $_POST['amount'] . "', " .
                
    "description = '" implode(", ",$_POST['description']) . "', " 
                
    "name = '" $_POST['name'] . "', " .
                
    "paid = '" $_POST['paid'] . "', " .
                
    "id = '" $_POST['id'] .
                
    "' WHERE id = '" $record "'";
        
    $result_update mysql_query($query_update) or die(mysql_error());
        
    header("Location: view_table.php");
        exit();
    } else {
        
    $query "SELECT * FROM table " .
                   
    "WHERE id = '" $record "'";
        
    $result mysql_query($query
            or die(
    mysql_error());
        
    $row mysql_fetch_array($result);
        
    $description explode(", "$row['description']);
    }
    ?> 
    <h1>Update Debt</h1>
    <form action="?" method="post">
    Date: <input type="text" name="date" value="<?php echo $row['date']; ?>" /> <br />
    Amount: <input type="text" name="amount" value="<?php echo $row['amount']; ?>" /> <br />
    Description: <textarea name="description[]"><?php echo $row['description']; ?></textarea> <br />
    Name: <input type="text" name="name" value="<?php echo $row['name']; ?>" /> <br />
    Paid: <input type="text" name="paid" value="<?php echo $row['paid']; ?>" /> <br />
    ID: <input type="text" name="id" value="<?php echo $row['id']; ?>" /> <br />
    <input type="submit" name="submit" value="Update" />
    </form>
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Update Entire SQL Table

    description[] probably isn't doing what you expect. In the code posted, $_POST['description'] is a one element array. You could drop the implode() with no ill effect.

    To xav0989's recommendations I'd add replace (2) "or die(...)" with exceptions or trigger_error. You can also perform some validation/sanitization with the filter functions.

  6. #6
    espfutbol98's Avatar
    espfutbol98 is offline x10 Sophmore espfutbol98 is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Zagreb... želim
    Posts
    200

    Re: Update Entire SQL Table

    I tried the source above by xav0989 and the page shows all of the data from the sql database but when I click Change, I am redirected but the data is not updated. I am sure all of the sql corresponds to the sql database.

  7. #7
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Update Entire SQL Table

    I see the error, the 'record' get variable isn't passed down. Here is an updated version of the last lines
    HTML Code:
    <h1>Update Debt</h1>
    <form action="?rid=<?php echo $record; ?>" method="post">
    Date: <input type="text" name="date" value="<?php echo $row['date']; ?>" /> <br />
    Amount: <input type="text" name="amount" value="<?php echo $row['amount']; ?>" /> <br />
    Description: <textarea name="description"><?php echo $row['description']; ?></textarea> <br />
    Name: <input type="text" name="name" value="<?php echo $row['name']; ?>" /> <br />
    Paid: <input type="text" name="paid" value="<?php echo $row['paid']; ?>" /> <br />
    ID: <input type="text" name="id" value="<?php echo $row['id']; ?>" /> <br />
    <input type="submit" name="submit" value="Update" />
    </form>
    Last edited by xav0989; 06-21-2009 at 07:19 PM.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  8. #8
    espfutbol98's Avatar
    espfutbol98 is offline x10 Sophmore espfutbol98 is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Zagreb... želim
    Posts
    200

    Re: Update Entire SQL Table

    Works great! thx

  9. #9
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Update Entire SQL Table

    You are very welcomed
    Last edited by xav0989; 06-21-2009 at 07:40 PM.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  10. #10
    espfutbol98's Avatar
    espfutbol98 is offline x10 Sophmore espfutbol98 is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Zagreb... želim
    Posts
    200

    Re: Update Entire SQL Table

    I found one slight problem easily fixed. It was turning $_POST['description'] into an array that didn't have to be. Here is the final code:
    PHP Code:
    <?php

    $record 
    $_GET["rid"];

    $db_server "localhost";
    $db_name "database";
    $db_user "username";
    $db_pass "password";

    $con1 mysql_connect($db_server$db_user$db_pass) or die(mysql_error());
        
    mysql_select_db($db_name$con1);

    $record mysql_real_escape_string($record);
        
    if (isset(
    $_POST['submit']) && $_POST['submit'] == "Update") {
        
    $query_update "UPDATE table SET " .
                
    "date = '" $_POST['date'] . "', " .
                
    "amount = '" $_POST['amount'] . "', " .
                
    "description = '" $_POST['description'] . "', " 
                
    "name = '" $_POST['name'] . "', " .
                
    "paid = '" $_POST['paid'] . "', " .
                
    "id = '" $_POST['id'] .
                
    "' WHERE id = '" $record "'";
        
    $result_update mysql_query($query_update) or die(mysql_error());
        
    header("Location: view_debt.php");
        exit();
    } else {
        
    $query "SELECT * FROM table " .
                   
    "WHERE id = '" $record "'";
        
    $result mysql_query($query
            or die(
    mysql_error());
        
    $row mysql_fetch_array($result);
    }
    ?> 
    <h1>Update Debt</h1>
    <form action="this_page.php?rid=<?php echo $record?>" method="post">
    Date: <input type="text" name="date" value="<?php echo $row['date']; ?>" /> <br />
    Amount: <input type="text" name="amount" value="<?php echo $row['amount']; ?>" /> <br />
    Description: <textarea name="description"><?php echo $row['description']; ?></textarea> <br />
    Name: <input type="text" name="name" value="<?php echo $row['name']; ?>" /> <br />
    Paid: <input type="text" name="paid" value="<?php echo $row['paid']; ?>" /> <br />
    ID: <input type="text" name="id" value="<?php echo $row['id']; ?>" /> <br />
    <input type="submit" name="submit" value="Update" />
    </form>
    Thanks everyone for your help.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. SQL moving a field within a table
    By wjh2303 in forum Programming Help
    Replies: 6
    Last Post: 05-28-2009, 06:34 AM
  2. MysQL Query for Master table & child table
    By phpasks in forum Programming Help
    Replies: 8
    Last Post: 08-07-2008, 08:07 AM
  3. sql table
    By Anonymousx1 in forum Free Hosting
    Replies: 1
    Last Post: 02-01-2008, 10:13 PM
  4. how to builed\manage a sql table
    By shayob in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 10-25-2006, 03:54 PM
  5. Table inside table
    By wizeman in forum Tutorials
    Replies: 4
    Last Post: 07-11-2005, 05:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers