Multiple record updates with checkboxes

Discussion in 'Programming' started by steveallen, May 1, 2007.

  1. #1
    Hi All,

    Have been trying to get this right for ages - update several records/rows from my database table.

    <?php
    # FileName="Connection_php_mysql.htm"
    # Type="MYSQL"
    # HTTP="true"
    $hostname_timesheet = "192.168.100.104";
    $database_timesheet = "timesheet";
    $username_timesheet = "*******";
    $password_timesheet = "*******";
    $tbl_name="test_mysql"; // Table name
    //$timesheet = mysql_pconnect($hostname_timesheet, $username_timesheet, $password_timesheet) or trigger_error(mysql_error(),E_USER_ERROR);


    // Connect to server and select databse.
    mysql_connect("$hostname_timesheet", "$username_timesheet", "$password_timesheet")or die("cannot connect");
    mysql_select_db("$database_timesheet")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>Id</strong></td>
    <td align="center"><strong>Name</strong></td>
    <td align="center"><strong>Lastname</strong></td>
    <td align="center"><strong>Email</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
    <td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
    <td align="center"><input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>"></td>
    <td align="center"><input name="email[]" type="text" id="email" value="<? echo $rows['email']; ?>"></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 name='$name[$i]' WHERE name='$id[$i]'";
    $result1=mysql_query($sql1);
    }
    }

    if($result1){
    header("location:test.php");
    }
    mysql_close();
    ?>


    Any ideas woudl be much appreciated ...


    Steve
     
    steveallen, May 1, 2007 IP