SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    upadting sql rows

    Hi
    i'm trying to update few rows in a database.
    I'm really a started with php, so isearched for it and found a litle script thatshold be ableto do the work.
    But i think i've done few errors when changing ... tis is what i have now:
    Code:
    <strong>Update multiple rows in mysql</strong><br>
    
    <?php
    error_reporting(E_ALL);
    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password="lserver"; // Mysql password
    $db_name="phparcadefree"; // Database name
    $tbl_name="main_conf"; // Table name
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);
    
    // Count table rows
    $count=mysql_num_rows($result);
    ?>
    <table width="500" border="0" cellspacing="1" cellpadding="0">
    <form name="form1" method="post" action="">
    <tr>
    <td>
    <table width="500" border="0" cellspacing="1" cellpadding="0">
    
    
    <tr>
    <td align="center"><strong>Name</strong></td>
    <td align="center"><strong>Content</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center"><? $id[]=$rows['name']; ?><? echo $rows['name']; ?></td>
    <td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['content']; ?>"></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
    </table>
    </td>
    </tr>
    </form>
    </table>
    <?php
    // Check if button name "Submit" is active, do this
    if($Submit){
    for($i=0;$i<$count;$i++){
    $sql1="UPDATE $tbl_name SET content='$content[$i]' WHERE name='$name[$i]'";
    $result1=mysql_query($sql1);
    }
    }
    
    if($result1){
    header("location:update_multiple.php");
    }
    mysql_close();
    ?>
    MY tabel has a coluimn (or how you call that ?) 'name' and 'content'
    This script shows on the right all the 'names' and on the left a form box where you can change the content ...
    I can those to warnings when i press submit:
    Notice: Undefined variable: Submit in C:\Server\Apache2\htdocs\arcadewelp\update2.php on line 52

    Notice: Undefined variable: result1 in C:\Server\Apache2\htdocs\arcadewelp\update2.php on line 59


    52: if($Submit){
    59: if($result1){


    Can someone pleasehelp me with this ?
    Thanks

  2. #2
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    London
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like you have "register_globals" set to off. This is a good thing.. but it means you'll need to make a small change to your code.

    Because register_globals is off, the form field "Submit" is not being turned into the variable $Submit when the form is submitted. But you can still get its value from the $_POST collection... like this:

    if($_POST['Submit']){


    This should also be enough to fix the second error (as the IF statement should be able to execute, and $result1 will be set.)

    Post back if it still doesn't work.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do i have to replace if($Submit){ by if($_POST['Submit']){ ?
    Sorry i'm really bad at this

  4. #4
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    London
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. (Sorry, I should have been clearer.)

  5. #5
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Notice: Undefined index: Submit in C:\Server\Apache2\htdocs\arcadewelp\update2.php on line 52

    Notice: Undefined variable: result1 in C:\Server\Apache2\htdocs\arcadewelp\update2.php on line 58

    This is what i get now ...
    i get this error often, undefiend variable, do you know some kind of tutorial so i can learn how to definte them :P ?
    thanks m8

  6. #6
    SitePoint Evangelist Waffles's Avatar
    Join Date
    Nov 2005
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have error_reporting(E_ALL); on and this will report everything, and you haven't defined variables etc, so whilst a minor error, are still errors to the php parser and it is warning you

    i believe you an define them as:


    var $foo;

  7. #7
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well i want every thing gone, that's why i have error_reporting(E_ALL);
    and i don't understand how i should define submit and result1 here ... ?

  8. #8
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Comeon guys please this is used on every cms, and everyone has it's own cms those days ^_^

  9. #9
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    London
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow... you've been busy posting!

    Let's get back to this line:

    if($_POST['Submit']){

    There's nothing wrong with this line when the form is posted... but when the page is first loaded it will trigger an error... becuase $_POST['Submit'] doesn't exist. So you need to test for the exisitance of post. Try putting this line in instead:

    if(isset($_POST['Submit'])){

  10. #10
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If i type somthing now i get those errors:
    Code:
    Notice: Undefined variable: content in C:\Server\Apache2\htdocs\arcadewelp\update2.php on line 54
    
    Notice: Undefined variable: name in C:\Server\Apache2\htdocs\arcadewelp\update2.php on line 54
    
    Notice: Undefined variable: content in C:\Server\Apache2\htdocs\arcadewelp\update2.php on line 54
    
    Notice: Undefined variable: name in C:\Server\Apache2\htdocs\arcadewelp\update2.php on line 54
    
    Warning: Cannot modify header information - headers already sent by (output started at C:\Server\Apache2\htdocs\arcadewelp\update2.php:1) in C:\Server\Apache2\htdocs\arcadewelp\update2.php on line 59

    54:
    $sql1="UPDATE $tbl_name SET content='$content[$i]' WHERE name='$name[$i]'";

    59:
    header("location:update_multiple.php");

    thanks for all

  11. #11
    SitePoint Addict -Ice-php's Avatar
    Join Date
    May 2002
    Location
    UK
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
     <?php
    error_reporting
    (E_ALL);
    $host="localhost"// Host name
    $username="root"// Mysql username
    $password="lserver"// Mysql password
    $db_name="phparcadefree"// Database name
    $tbl_name="main_conf"// Table name
     
    // Connect to server and select databse.
    mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
     
    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);
     
    // Count table rows
    $count=mysql_num_rows($result);
     
    // Check if button name "Submit" is active, do this
    if(isset($_POST['Submit'])){
    for(
    $i=0;$i<$count;$i++){
    $content $_POST['content'];
    $name $_POST['name'];
    $sql1="UPDATE $tbl_name SET content='$content[$i]' WHERE name='$name[$i]'";
    $result1=mysql_query($sql1);
    }
    }
    if(
    $result1){
    header("location:update_multiple.php");
    }
    mysql_close();
    ?>
    header("location:update_multiple.php"); - For this line you would have to send the headers to the server before any text is parsed by the server.

    You also have to use $_POST['content'] for example on the posted data.

    Give that a try as the php code. You will still need your HTML below it.
    -Ice

  12. #12
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    header("location:update_multiple.php"); eu sorry, do i have to change anything on this line ? update_multiple.php is the file name ...

    Also i don't understand anymore where the form should be ?
    or just below the php code, nothing between anymore ?
    thanks

  13. #13
    SitePoint Addict -Ice-php's Avatar
    Join Date
    May 2002
    Location
    UK
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    $host="localhost"// Host name
    $username="root"// Mysql username
    $password="lserver"// Mysql password
    $db_name="phparcadefree"// Database name
    $tbl_name="main_conf"// Table name
     
    // Connect to server and select databse.
    mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
     
    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);
     
    // Count table rows
    $count=mysql_num_rows($result);
     
    // Check if button name "Submit" is active, do this
    if(isset($_POST['Submit'])){
    for(
    $i=0;$i<$count;$i++){
    $content $_POST['content'];
    $name $_POST['name'];
    $sql1="UPDATE $tbl_name SET content='$content[$i]' WHERE name='$name[$i]'";
    $result1=mysql_query($sql1);
    }
    }
    if(
    $result1){
    header("location:update_multiple.php");
    }
    mysql_close();
    ?> 
    <table width="500" border="0" cellspacing="1" cellpadding="0">
    <form name="form1" method="post" action="">
    <tr>
    <td>
    <table width="500" border="0" cellspacing="1" cellpadding="0">
     
     
    <tr>
    <td align="center"><strong>Name</strong></td>
    <td align="center"><strong>Content</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center"><? $id[]=$rows['name']; ?><? echo $rows['name']; ?></td>
    <td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['content']; ?>"></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
    </table>
    </td>
    </tr>
    </form>
    </table>
    -Ice

  14. #14
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i get this:Notice: Undefined variable: result1 in C:\Server\Apache2\htdocs\arcadewelp\update_multiple.php on line 28
    i fixed it with isset, but after submittingi get:

    Notice: Undefined index: content in C:\Server\Apache2\htdocs\arcadewelp\update_multiple.php on line 22

    Notice: Undefined index: content in C:\Server\Apache2\htdocs\arcadewelp\update_multiple.php on line 22

    Warning: Cannot modify header information - headers already sent by (output started at C:\Server\Apache2\htdocs\arcadewelp\update_multiple.php:22) in C:\Server\Apache2\htdocs\arcadewelp\update_multiple.php on line 29

    the databases aren't being updated neether

  15. #15
    SitePoint Addict -Ice-php's Avatar
    Join Date
    May 2002
    Location
    UK
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to have an action. Instead of using a header, you should use <?= $_SERVER['PHP_SELF'] ?>
    This will make the form post data to the same page.

    Try This

    PHP Code:
     <?php
    error_reporting
    (E_ALL);
    $host="localhost"// Host name
    $username="root"// Mysql username
    $password="lserver"// Mysql password
    $db_name="phparcadefree"// Database name
    $tbl_name="main_conf"// Table name

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

    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);

    // Count table rows
    $count=mysql_num_rows($result);

    // Check if button name "Submit" is active, do this
    if(isset($_POST['Submit'])){
    for(
    $i=0;$i<$count;$i++){
    $content $_POST['content'];
    $name $_POST['name'];
    $sql1="UPDATE $tbl_name SET content='$content[$i]' WHERE name='$name[$i]'";
    $result1=mysql_query($sql1);
    }
    }
    mysql_close();
    ?> 
    <table width="500" border="0" cellspacing="1" cellpadding="0">
    <form name="form1" method="post" action="<?= $_SERVER['PHP_SELF'?>">
    <tr>
    <td>
    <table width="500" border="0" cellspacing="1" cellpadding="0">


    <tr>
    <td align="center"><strong>Name</strong></td>
    <td align="center"><strong>Content</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center"><? $id[]=$rows['name']; ?><? echo $rows['name']; ?></td>
    <td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['content']; ?>"></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
    </table>
    </td>
    </tr>
    </form>
    </table>
    -Ice

  16. #16
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    London
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is the "current page" (the scriptwe've been discussing) called update_multiple.php?
    • If so... there's no need for the header line
    • If not... we can't comment because we haven't seen its code


    (For the record... the header error your getting is most likely to be caused by outputting HTML. Move the HTML to affter the bulk of the php.)


    Now, the problem with "content" is that there's nothing in your form called "content". Looks like

    name="name[]"

    should be replaced with

    name="content[]"

    **********

    We're probably not there yet. Next time you post, could you post the latest version of your code.

  17. #17
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    London
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ooops... sorry -Ice-php - Didn't mean to post on top of you.

  18. #18
    SitePoint Addict -Ice-php's Avatar
    Join Date
    May 2002
    Location
    UK
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dont worry about it. Im not saying I'm perfectly correct. Your input is just as valid as mine
    -Ice

  19. #19
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    London
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're right... its worse when the advice conflicts!

  20. #20
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It doesn't update
    it only gave this warning:
    Notice: Undefined index: content in C:\Server\Apache2\htdocs\arcadewelp\sitepoint.php on line 22

    Notice: Undefined index: content in C:\Server\Apache2\htdocs\arcadewelp\sitepoint.php on line 22

    So i added isset()
    But now youdon't get anywarning, here doesn't happen anything
    btw, i just added a column id(primary key) ... it doesn't infect anything and maybe it helps ?
    if anyone has sugestions left ^_^

  21. #21
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    London
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you still don't have anything called content in your form. Please post back your latest code.

  22. #22
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what i have: thanks m8
    Code:
    <?php
    error_reporting(E_ALL);
    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password="lserver"; // Mysql password
    $db_name="phparcadefree"; // Database name
    $tbl_name="main_conf"; // Table name
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);
    
    // Count table rows
    $count=mysql_num_rows($result);
    
    // Check if button name "Submit" is active, do this
    if(isset($_POST['Submit'])){
    for($i=0;$i<count($_POST['naam']);$i++)
    $content = $_POST['content'];
    $name = $_POST['name'];
    $sql1="UPDATE $tbl_name SET content='$content[$i]' WHERE name='$name[$i]'";
    $result1=mysql_query($sql1);
    }
    }
    mysql_close();
    ?>
    <table width="500" border="0" cellspacing="1" cellpadding="0">
    <form name="form1" method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
    <tr>
    <td>
    <table width="500" border="0" cellspacing="1" cellpadding="0">
    
    
    <tr>
    <td align="center"><strong>Name</strong></td>
    <td align="center"><strong>Content</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center"><? $id[]=$rows['name']; ?><? echo $rows['name']; ?></td>
    <td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['content']; ?>"></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
    </table>
    </td>
    </tr>
    </form>
    </table>

  23. #23
    SitePoint Addict -Ice-php's Avatar
    Join Date
    May 2002
    Location
    UK
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok,

    Where is your "content" coming from? There is no field with the name "content" being posted. Unless this is the field you have called name[]

    For a start, the loop looks like it contains a typo
    PHP Code:
    for($i=0;$i<count($_POST['naam']);$i++) 
    Did you mean this:
    PHP Code:
    for($i=0;$i<count($_POST['name']);$i++) 
    Could you explain what each field is?
    -Ice

  24. #24
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    London
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This line...

    for($i=0;$i<count($_POST['naam']);$i++)

    ... needs a "{" thus:

    for($i=0;$i<count($_POST['naam']);$i++){

    As Ice said there's no corresponding field. Should it be:

    for($i=0;$i<count($_POST['content']);$i++){

    .... perhaps???


Bookmarks

Posting Permissions

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