Can't create php update from multiple checkbox to mysql HELP !

Discussion in 'Programming' started by jurisgr, Sep 29, 2009.

  1. #1
    Hi,

    I need to create web page that can read from and update records in mysql table. I need to update only one field in table - "name". Data structure for everey record - ID=1, Name="checkbox values should be there ON/OFF or 1/0 or yes/no", Data=.., Data=.., etc. This code works with text field, but this is not what i want - i need for every record On/Off value to be available for selection and after pressing submit - update to my database. This field "name" i have to attach to checkbox - so that user can update all fields as neccesary and as many times as he wants. I need in result that in browser i see all records and for every record is available checkbox that i can check in and check out and after submit button press all data are stored in mysql - in corresponding record.

    Please help me to create this code i have no idea how to create this loop in this code - to get it fully working.
    Here is mysql code and both php files, and same information in attached file View attachment code.zip :

    Create Table:
    CREATE TABLE IF NOT EXISTS `name_list` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` text NOT NULL,
      `SomeData` varchar(100) NOT NULL,
      `LinkID` varchar(3) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
    Insert Data:
    INSERT INTO `name_list` (`id`, `name`, `SomeData`, `LinkID`) VALUES
    (1, '0', '11111111111111111111', 'X1'),
    (2, '1', '22222222222222222222', 'X2');
    Code (markup):
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1257" />
    <?php
    $host="localhost";
    $db_user="root";
    $database="JURIS";
    mysql_connect($host,$db_user);
    mysql_select_db($database); ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>UpdateFromInput</title>
    </head>
    <body>
    <form id="form1" name="form1" method="post" action="update.php">
    <table border="1" cellpadding="3" cellspacing="0">
    <tr><td align="center" bgcolor="#FFCC00"><strong>ID</strong></td>
    <td align="center" bgcolor="#FFCC00"><strong>Name</strong></td></tr>
    <?php $result=mysql_query("select * from name_list order by id asc");
    while($row=mysql_fetch_assoc($result)){
    echo $row['name']; ?>
    <tr><td bgcolor="#FFFFCC"><?php echo $row['id']; ?></td>
    <td bgcolor="#FFFFCC"><input name="name_<?php echo $row['id']; ?>" type="text" id="name_<?php echo $row['id']; ?>" value="<?php echo $row['name']; ?>"></td>
    <td bgcolor="#FFFFCC"><input name="name_<?php echo $row['id']; ?>" type="checkbox" id="name_<?php echo $row['id']; ?>" value="<?php echo $row['name']; ?>"></td>
    <?php } ?>
    </tr>
    </table> <input type="submit" name="Submit" value="Update" />
    </form>
    </body>
    </html>
    Code (markup):
    
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1257" />
    <?php if($_POST['Submit']){
    $host="localhost";
    $db_user="root";
    $database="JURIS";
    mysql_connect($host,$db_user);
    mysql_select_db($database);
    $result=mysql_query("select id from name_list order by id asc");
    while($row=mysql_fetch_assoc($result)){
    $name=$_POST["name_" .$row['id']];
    mysql_query("update name_list set name='$name' where id='$row[id]'"); }
    echo "--- Update Complete ---"; }
    ?>
    <form name="Back" method="post" action="form.php"><input type="submit" name="submit" value="Back"></form>
    
    Code (markup):
    Thanks!
     
    jurisgr, Sep 29, 2009 IP