DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   please help. how do i add checkbox values in mysql row (http://www.daniweb.com/forums/thread142534.html)

fortiz147 Aug 27th, 2008 4:35 am
please help. how do i add checkbox values in mysql row
 
here's the process:
1. i display the mysql data in rows with checkboxes on each row
2. i want to add the checked values into another table under the specific field employee

here's the problem: i can't seem to insert the checked value to the specified project table.
what if i had different projects in that table, how will the query know that i want to insert that specific employee to the project table?

phpuser Aug 27th, 2008 8:25 am
Re: please help. how do i add checkbox values in mysql row
 
hi,may be i help you.i understand little what you want but if you paste your code here then it will be useful for me :)

fortiz147 Aug 27th, 2008 11:10 pm
Re: please help. how do i add checkbox values in mysql row
 
thank you!!! here's my code.

Display the Mysql Table and assign checkboxes for each value:
<html>
<head>
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
<link rel="icon" href="images/favicon.ico" type="image/x-icon">


<title>SM PORTAL WEBSITE</title>

<meta http-equiv="Content-Type" content="text/html;">
<SCRIPT language=JavaScript>
function makeArray() {
    for (i = 0; i<makeArray.arguments.length; i++)
        this[i] = makeArray.arguments[i];
 }

function getFullYear(d) {
    var y = d.getYear();
    if (y < 1000) {y += 1900};
    return y;
}

//var zone = "EDT";
var days = new makeArray("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var months = new makeArray("January","February","March","April","May","June","July","August","September","October","November","December");

function format_time(t) {

    var Day = t.getDay();
    var Date = t.getDate();
    var Month = t.getMonth();
    var Year = t.getFullYear();

    timeString = "";
    timeString += days[Day];
    timeString += ", ";
    timeString += " ";
    timeString += months[Month];
    timeString += " ";
    timeString += Date;
    timeString += ", ";
    timeString += " ";
    timeString += Year;

  return timeString;
 }
</SCRIPT>
<link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body bgcolor="#B49BCD" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" link="#747AC6" vlink="#B49BCD">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%">
  <tr>
    <td valign="top" height="84" background="images/header_fill.jpg"><img src="images/smheaderlogo.jpg" width="321" height="84"></td>
  </tr>
  <tr>
    <td valign="top" height="22" background="images/blue_fill.gif" align="right">
      <table width="220" border="0" cellspacing="0" cellpadding="0" height="22">
        <tr>
          <td width="37"><img name="blue_crn" src="images/blue_crn.gif" width="37" height="22" border="0"></td>
          <td background="images/lt_blue_fill.gif" class="date" width="200">
            <script language=JavaScript>
                        <!--
                        d = new Date();
                        document.write(format_time(d));
                        // -->
                </script>
          </td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td valign="top">
      <table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%">
        <tr>
          <td width="140" bgcolor="#92A2E1" valign="top" align="center"> <br>
            <table width="80%" height="228" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td class="links"><p><a href="home.htm"><img src="images/square.gif" width="7" height="7" border="0"></a> HOME<br>
                      <br>
                      <a href="http://10.101.4.21/nagios/"><img src="images/square.gif" width="7" height="7" border="0"></a> NAGIOS<br>
                      <br>
                      <a href="http://10.101.4.21/cgi-bin/smokeping.cgi"><img src="images/square.gif" width="7" height="7" border="0"></a> SMOKE
                  PING <br>
                      <br>
                      <a href="http://10.101.160.59:9675/account/login"><img src="images/square.gif" width="7" height="7" border="0"></a> SPICEWORKS<br>
                      <br>
                      <a href="http://10.101.160.59:9675/helpdesk"><img src="images/square.gif" width="7" height="7" border="0"></a> HELP
                  DESK <br>
                      <br>
                      <a href="humanresourcemanagement1.htm"><img src="images/square.gif" width="7" height="7" border="0"></a> HUMAN
                  RESOURCE MANAGEMENT</p>
                  <p><a href="memberlogin.php"><img src="images/square.gif" alt="df" width="7" height="7" border="0"></a> BLOG</p></td>
              </tr>
            </table>
            <p>&nbsp;</p></td>
          <td valign="top" bgcolor="#FFFFFF">
            <table width="100%" border="0" cellspacing="0" cellpadding="30" height="100%">
              <tr>
                <td width="90%" valign="top" bgcolor="#FFFFFF" class="text"><p>&nbsp;</p>
                <p>&nbsp;</p>
                               




<?php

//mysql connection
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "smportal";


        //Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
        //Select Database
mysql_select_db($dbname) or die(mysql_error());
        // Retrieve data from Query String






$query="SELECT * FROM employee"; // query on table
$qry_result = mysql_query($query) or die(mysql_error());


$count = mysql_num_rows($qry_result); // count query result

 

  ?>


<form method="post" action="addemptoproj.php">







  <?php
$display_string = "<table border='2' cellpadding='3'>";
$display_string .= "<tr>";
$display_string .= "<th>First Name</th>";
$display_string .= "<th>Last Name</th>";
$display_string .= "<th>Qualification</th>";
$display_string .= "<th>Add</th>";
    //loop here

  while($row = mysql_fetch_array($qry_result))

    {

    //inside while

        $display_string .= "<tr>";
        $display_string .= "<td>$row[Fname]</td>";
        $display_string .= "<td>$row[Lname]</td>";
        $display_string .= "<td>$row[Qualification]</td>";



       


$display_string .= "<td><input type='checkbox' name='checkbox[]' id='checkbox[]' value='$row[empId]' /></td>";


 
$display_string .= "</tr>";
  }

$display_string .= "</table>";
$display_string .= "<table cellpadding='5' align='center'><td><input id='delete' type='submit' name='delete' value='Delete'/></td></table>";

echo $display_string;

?>



 </form>


 


</td>
                <td width="10%" height="370" valign="top" bgcolor="#FFFFFF" class="text">&nbsp;</td>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td width="140" bgcolor="#92A2E1" height="27">&nbsp;</td>
          <td align="right" height="27" bgcolor="#FFFFFF"><img name="footer_right" src="images/footer_right.gif" width="110" height="27" border="0"></td>
        </tr>
        <tr>
          <td width="140" background="images/footer_fill.jpg" height="38">&nbsp;</td>
          <td align="right" height="38" background="images/footer_fill.jpg">
            <table width="100%" border="0" cellspacing="0" cellpadding="0" background="images/footer_fill.jpg">
              <tr>
                <td valign="bottom" align="center" class="footer">Copyright &copy;
                  SM Mart Inc. All Rights Reserved.</td>
                <td align="right" width="110"><img name="footer_right2" src="images/footer_right2.gif" width="110" height="38" border="0"></td>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td width="140" height="10">&nbsp;</td>
          <td align="right" height="10">&nbsp;</td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
This code is suppose to add the checked values to the latest/maximum empId which is autogenerated in the specified field,employee, in table hrm1.
<?php
#
//mysql connection here

//mysql connection
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "smportal";


        //Connect to MySQL Server
$dbconn=mysql_connect($dbhost, $dbuser, $dbpass);
        //Select Database
mysql_select_db($dbname) or die(mysql_error());
        // Retrieve data from Query String
 
#
if($_POST['delete']) // from button name="delete"
#
{
#
$checkbox = $_POST['checkbox']; //from name="checkbox[]"
#
$countCheck = count($_POST['checkbox']);
#
 
#
for($i=0;$i<$countCheck;$i++)
#
{
#
$del_id = $checkbox[$i];
#

$sql = "SELECT * FROM employee WHERE empId=$del_id";

$result = mysql_query($sql, $dbconn);

while($row = mysql_fetch_array($result)){
$sql3 = "SELECT hrmId, MAX(hrmId) FROM hrm1";
$sql2 = "INSERT INTO hrm1 (workingemp) VALUES ('{$row[Fname]}')";
mysql_query($sql2, $dbconn);
mysql_query($sql3, $dbconn);
}



}
#
if($result)
#
{

echo "SUCCESS";

}

else

{
include("delete.php");

}

}

?>

ahmadjhoney Aug 28th, 2008 8:38 am
Re: please help. how do i add checkbox values in mysql row
 
hello everyone,,i new in php...

plz tell me about getting page id,,\

how can i get a apecifuc page id..\\


All times are GMT -4. The time now is 7:30 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC