Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    inserting multiple rows into mysql table using check box to select multiple option

    Hello Guys,

    I am new to php coding. I am trying to insert value call from a table into another table.
    Here below is my code/script

    am not getting anything inserted at all. Can some one please help me out to make it work.

    A big help will be appreciated



    <?php
    include 'dbc.php';
    p
    ?>
    <html>
    <head>

    <script>
    function showUser(str)
    {
    if (str=="")
    {
    document.getElementById("txtHint").innerHTML="";
    return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
    }
    xmlhttp.open("GET","test.php?q="+str,true);
    xmlhttp.send();
    }
    </script>
    <link href="login_data/style.css" rel="stylesheet" type="text/css">
    <style type="text/css">
    <!--
    .style8 {font-family: "Courier New", Courier, monospace; font-size: 12px; }
    .style10 {font-size: 14px; font-family: "Courier New", Courier, monospace; font-weight: bold; }
    -->
    </style>
    </head>
    <body>
    <div align="center">
    <table width="716" border="0" cellpadding="0" cellspacing="0" >
    <tr>
    <td width="716"><div id="txtHint">
    <form action="test.php" method="POST">
    <?php

    // Get values from form
    $subject = $_POST['subject'];
    $surname = $_POST['surname'];
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $class = $_POST['class'];
    $class_id =$ _POST['class_id'];
    $level = $_POST['level'];
    $subject_alt_id=$_POST['student_alt_id'];
    $tbl_name="subject_name"; // Table name
    $tbl_name2="student_data";
    $studentID=$_POST['studentID'];

    // Check if button name "Submit" is active, do this
    if($_POST['doSubmit'] == 'Add')
    {
    if($checkbox){
    for($i=0;$i<$count;$i++){


    foreach($_POST['studentID'] as $i) {
    $sql1="INSERT INTO schedule_students (id,surname, firstname, lastname, class, class_id, level,student_alt_id)VALUES( '$studentID','$surname','$firstname', '$lastname','$class', '$class_id','$level', '$student_alt_id')";

    $result1=mysql_query($sql1);
    }
    }
    }
    }
    ?>
    <p><span class="forms"><strong>Select Class:</strong>
    <select name="Level" onChange="showUser(this.value)">
    <option value="">-Select-</option>
    <option value="1">JSS-1</option>
    <option value="2">JSS-2</option>
    <option value="3">JSS-3</option>
    <option value="4">SSS-1</option>
    <option value="5">SSS-2</option>
    <option value="6">SSS-3</option>
    </select></span>
    <?php
    $rs = mysql_query("SELECT * FROM $tbl_name") or die(mysql_error());

    echo "<span class='forms'><b>Select Subject:</b><select name='subject' class='forminput'><option value=''> -Select- </span></option>";

    while($row = mysql_fetch_array($rs)){
    echo "<option value='".$row["subject"]."'>".$row["subject"]."</option>";
    }
    mysql_free_result($rs);
    echo "</select>";
    ?>
    </p>
    <p>
    <?php
    $q = intval($_GET['q']);
    //$check=$_POST['<input type="checkbox" name='users[]' value="<?php echo $row['class_id']">'];

    $con = mysqli_connect('localhost','root','','report');
    if (!$con)
    {
    die('Could not connect: ' . mysqli_error($con));
    }


    mysqli_select_db($con,"report");
    $sql="SELECT * FROM $tbl_name2 WHERE class_id = '".$q."'";

    $result = mysqli_query($con,$sql);
    $count=mysql_num_rows($result);

    ?>
    <table border="0" width="104%" cellspacing="0" cellpadding="3" >
    <tr>
    <th width="8%" class="myaccount"><span class="style10">Check</span></th>
    <th width="6%" class="myaccount"><span class="style10">ID</span></th>
    <th width="25%" class="myaccount"><span class="style10">Surname</span></th>
    <th width="13%" class="myaccount"><span class="style10">Firstname</span></th>
    <th width="13%" class="myaccount"><span class="style10">Lastname</span></th>
    <th width="9%" class="myaccount"><span class="style10">Class</span></th>
    <th width="10%" class="myaccount"><span class="style10">Level</span></th>
    <th width="16%" class="myaccount"><span class="style10">Student ALT ID</span></th>
    </tr>

    <?php
    $i = 0;
    while($row = mysqli_fetch_array($result))

    {
    ?>

    <tr class="row">
    <td align='center' ><input name="studentID[]" type="checkbox" id="studentID[]" value="<?php echo "$q"?>" /></td>
    <td align='center' ><span class="style8"><?php echo $row['studentID']?></span></td>
    <td align='center' ><span class="style8"><?php echo $row['surname']?></span></td>
    <td align='center' ><span class="style8"><?php echo $row['firstname']?></span></td>
    <td align='center' ><span class="style8"><?php echo $row['lastname']?></span></td>
    <td align='center' ><span class="style8"><?php echo $row['class']?></span></td>
    <td align='center' ><span class="style8"><?php echo $row['level']?></span></td>
    <td align='center' ><span class="style8"><?php echo $row['student_alt_id']?></span></td>
    </tr></thead>

    <?php
    }
    ?>
    </table>
    <?php
    mysqli_close($con);
    ?>


    <p></p>
    <input type="submit" name="doSubmit" id="doSubmit" value="Add"><br> </form>
    </div></td></tr>
    </table>
    </div>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,316
    Thanks
    23
    Thanked 613 Times in 612 Posts
    You should use the # mark in the tool bar when submitting code.
    I don't know if this is your problem, but it will stop exicution of your code and needs to be fixed. Line 57 has:
    Code:
    $class_id =$ _POST['class_id'];
    and it shiould be:
    Code:
    $class_id = $_POST['class_id'];
    The $_POST does not need a space as in the wrong code.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    You should use the # mark in the tool bar when submitting code.
    I don't know if this is your problem, but it will stop exicution of your code and needs to be fixed. Line 57 has:
    Code:
    $class_id =$ _POST['class_id'];
    and it shiould be:
    Code:
    $class_id = $_POST['class_id'];
    The $_POST does not need a space as in the wrong code.
    .................................................................................................... ..
    #
    Thank sir i have done that yet is remain not posting any data at all.

    What i intend doing is to select row with check box from the some thing like this attached image and get it into my table on the database.

    Thank you
    Attached Thumbnails Attached Thumbnails inserting multiple rows into mysql table using check box to select multiple option-sample.jpg  

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,316
    Thanks
    23
    Thanked 613 Times in 612 Posts
    @vantglobal, You are getting the information you are displaying from the database already. You don't need to " get it into my table" because it's already there.
    Here is A way of getting all the information contained in a row and getting it when the check box is changed :

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Title of the document</title>
    <style type="text/css">
    #my_table td{
    	text-align:center;
    }
    #my_table th{
    	text-align:center;
    	background-color: gray;
    }
    </style>
    </head>
    
    <body>
    <table border="1" width="104%" cellspacing="0" cellpadding="3" id="my_table">
    <tr>
    <th class="myaccount">Check</th>
    <th class="myaccount">ID</th>
    <th class="myaccount">Surname</th>
    <th class="myaccount">Firstname</th>
    <th class="myaccount">Lastname</th>
    <th class="myaccount">Class</th>
    <th class="myaccount">Level</th>
    <th class="myaccount">Student ALT ID</th>
    </tr>
    
    <tr class="row">
    <td><input name="studentID[]" type="checkbox" id="studentID[]" onchange="get_info(1);"></td>
    <td>81</td>
    <td>Adeola</td>
    <td>Ndubuisi</td>
    <td>Rachael</td>
    <td>Hibscuis</td>
    <td>JSS-1</td>
    <td>1234</td>
    </tr>
    
    <tr class="row">
    <td><input name="studentID[]" type="checkbox" id="studentID[]" onchange="get_info(2);"></td>
    <td>52</td>
    <td>Oladejo</td>
    <td>Abiola</td>
    <td>Mutiu</td>
    <td>Hibscuis</td>
    <td>JSS-1</td>
    <td>9500</td>
    </tr>
    </table>
    
    <script type="text/javascript">
    function get_info(row){
    	var baset = document.getElementById("my_table").rows[row];
    	for (y=1; y <= baset.cells.length; y++){
    		var x = baset.cells[y].innerHTML;
    		alert (x);
    	}
    }
    </script>
    </body>
    </html>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Posting Permissions

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