Jump to content

HELP - How to add multiple rows selected by a checkbox to the database


benardik

Recommended Posts

Hi,

 

I am new to php and need help.

I am building a library system for an assignment.

I want to provide a functionality that registers (adds) all transactions to defferent databases.

I am stuck on how to add a selected row (selected by a checkbox) to the database and make sure that 2 users cannot update the same record at the same time.

 

Thanks for your help.

 

bellow is my code:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title>Web Works DD - Web Development & Software Solutions</title>
         <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
      <link rel="stylesheet" type="text/css" href="css/style.css" /> 
  
  </head>
  <body>
  <!--<div class="utilities">
         <!-- UTILITIES MENU --
      <a href="index.php" style=text-decoration:none;><img src="images/home.png" border="0" alt="Home Page"/> Home Page</a>    <a href="sitemap.php" style=text-decoration:none;><img src="images/sitemap.png" border="0" alt="Sitemap"/> Sitemap</a>
        </div><!-- END UTILITIES -->

  <div id="right_bar">   <!-- right bar starts here -->
  </div> <!-- end of right bar -->

  <div id="top_bar">   <!-- top bar starts here -->
  <a href="index.php" style=text-ecoration:none; title="Web Works DD - Web Development & Software Solutions"><img src="images/mainLib.jpg" border=0 style=height:180;/> </a>
  <img src="images/topText.jpg" border=0 style=height:180;/>
  </div> <!-- end of top bar -->
        
<div id="container">
<div id="top-logo"> 
  <!--<a href="index.php" style=text-ecoration:none; title="Web Works DD - Web Development & Software Solutions"><img src="images/Main_logo.jpg" border=0 /> </a> -->
  </div>   
         
     <ul class="dropdown dav">
     <li><a href="index1.php">HOME</a></li>
     <li><a href="#" class="dir">VIEW RWSOURCES</a>
      <ul>
         <li><a href="books.php">BOOKS</a></li>
         <li><a href="dvds.php">DVDS</a></li>
         <li><a href="journals.php">JOURNALS</a></li>
    </ul>
   </li>
   <li><a href="staffLogin.php">STAFF ZONE</a></li>
    <li><a href="studentsLogin.php">STUDENT ZONE</a></li>
   
</ul>

<table>
<!--This defines a new form -->
<form action="" method="post"> 
<!--This creates the dropdown in html-->
<select name="fields"> 
<option value="1">Please select...</option>
<option value="isbnno">isbnno</option>
<option value="title">title</option> 
<option value="description">description</option>
<option value="author">author</option>   
</select> 
</td>
</tr>
<tr>
<td>
<input type="text" name="value" size="40">
</td>
</tr>
<tr>
<td>
<!-- Create a button -->
<input type="submit" value="Select" name="select"> 
</td>
</tr>
</form> 
</table>

<?php
session_start();
include("secure/db_open.php");
       /* $myusername=$_SESSION['myusername']; 
        echo $myusername;
        $insert = $checkbox[$i];
        $cops = $_POST['copies'];
        $userid ="SELECT id FROM students WHERE username = '$myusername'";  
        $uido = mysql_query($userid); 
        $row = mysql_fetch_array($uido);
        echo $row['id'];    */
        
        
  
// get variable after selecting something from the dropdown with name 'fields'

$select = $_POST['select']; 
$valu = $_POST['value'];
// if something has been chosen
if (!empty($select)) 
{ 

$fields = $_POST['fields'];  //get the chosen value
$allbooks="SELECT * FROM books WHERE {$fields} = '$valu'";
//select books that are available - driven by the user's input
$copas="SELECT * FROM books WHERE {$fields} = '$valu' AND available_copies > 0";
$copy = mysql_query($copas); 
//count the number of rows resulting from the query
$count=mysql_num_rows($copy);
//$copse = mysql_fetch_array($copy);

//select books that are not available
$notavailable="SELECT * FROM books WHERE {$fields} = '$valu' AND available_copies = 0";
$shownotavailable=mysql_query($notavailable);
$count1=mysql_num_rows($shownotavailable);

//display a form 
echo "<table width='1100' border='0' cellspacing='1' cellpadding='0'>";
echo "<tr>";
echo "<td>" ;
echo "<form name='form1' method='post' action=''>";
echo "<table width='1100' border='0' cellpadding='3' cellspacing='1' bgcolor='#CCCCCC'>";
echo "<tr>";
echo "<td align='center' colspan='9' bgcolor='#FFFFFF'>";
echo "<strong>Available to Loan </strong>" ;
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center' bgcolor='#FFFFFF'>Loan</td>";
echo "<td align='center' bgcolor='#FFFFFF'><strong>ISBN No.</strong></td>";
echo "<td align='center' bgcolor='#FFFFFF'><strong>Title</strong></td>";
echo "<td align='center' bgcolor='#FFFFFF'><strong>Description</strong></td>";
echo "<td align='center' bgcolor='#FFFFFF'><strong>Author</strong></td>";
echo "<td align='center' bgcolor='#FFFFFF'><strong>Loan Type</strong></td>";
echo "<td align='center' bgcolor='#FFFFFF'><strong>Location</strong></td>"; 
echo "<td align='center' bgcolor='#FFFFFF'><strong>Available Copies</strong></td>";
echo "<td align='center' bgcolor='#FFFFFF'><strong>Desired Num of Copies</strong></td>";
echo "</tr>"; 
while(($row = mysql_fetch_assoc($copy)))
  {  
  echo "<tr>";
  echo "<td align='center' bgcolor='#FFFFFF'><input name='checkbox[]' type='checkbox' id='checkbox[]' value=".$row['bookid']."></td>";
  echo "<td>" .$row['isbnno']. "</td>";
  echo "<td>" .$row['title']. "</td>";
  echo "<td>" .$row['description']. "</td>";
  echo "<td>" .$row['author']. "</td>";
  echo "<td>" .$row['loan_type']. "</td>";
  echo "<td>" .$row['location']. "</td>";
  echo "<td>" .$row['available_copies']. "</td>"; 
  echo "<td align='center' bgcolor='#FFFFFF'><input name='copies' type='text' id='copies' size='2'></td>";  
  echo "</tr>";
  }        
echo "<tr>";
echo "<td colspan='9' align='center' bgcolor='#FFFFFF'><input name='loan' type='submit' id='loan' value='Loan Book'></td>";
echo "</tr>";
echo "</table>"; 

if (isset($_POST))
{
      session_start();
        $myusername=$_SESSION['myusername'];     
        $cops = $_POST['copies'];
        $userid ="SELECT id FROM students WHERE username = $myusername";  
        $uido = mysql_query($userid); 
        $row1 = mysql_fetch_array($uido);
        $sid = $row1['id']; 
        
    for ($i=0;$i<$count;$i++)
    {   
        $insert = $checkbox[$i];
       //echo "Welcome, ".$row['id'];
        $insertloaned="INSERT INTO studentloanedbooks(studentid, bookid, noofcopies) VALUES ($sid, $insert, $cops)";
        $loaned = mysql_query($insertloaned);
            
    }   
     
}  
    
if($count1>0)
{
    //display the books details
    //display a form 
echo "<br>";
echo "<br>";  
echo "<table width='1100' border='0' cellspacing='1' cellpadding='0'>";
echo "<tr>";
echo "<td>" ;
echo "<form name='form1' method='post' action=''>";
echo "<table width='1100' border='0' cellpadding='3' cellspacing='1' bgcolor='#CCCCCC'>";
echo "<tr>";
echo "<td align='center' colspan='9' bgcolor='#FFFFFF'>";
echo "<strong>Available to Loan </strong>" ;
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center' bgcolor='#FFFFFF'>Loan</td>";
echo "<td align='center' bgcolor='#FFFFFF'><strong>ISBN No.</strong></td>";
echo "<td align='center' bgcolor='#FFFFFF'><strong>Title</strong></td>";
echo "<td align='center' bgcolor='#FFFFFF'><strong>Description</strong></td>";
echo "<td align='center' bgcolor='#FFFFFF'><strong>Author</strong></td>";
echo "<td align='center' bgcolor='#FFFFFF'><strong>Loan Type</strong></td>";
echo "<td align='center' bgcolor='#FFFFFF'><strong>Location</strong></td>"; 
echo "<td align='center' bgcolor='#FFFFFF'><strong>Available Copies</strong></td>";
echo "<td align='center' bgcolor='#FFFFFF'><strong>Desired Num of Copies</strong></td>";
echo "</tr>"; 
while(($row = mysql_fetch_assoc($shownotavailable)))
  {  
  echo "<tr>";
  echo "<td align='center' bgcolor='#FFFFFF'><input name='checkbox[]' type='checkbox' id='checkbox[]' value='.$row[bookid]'></td>";
  echo "<td>" .$row['isbnno']. "</td>";
  echo "<td>" .$row['title']. "</td>";
  echo "<td>" .$row['description']. "</td>";
  echo "<td>" .$row['author']. "</td>";
  echo "<td>" .$row['loan_type']. "</td>";
  echo "<td>" .$row['location']. "</td>";
  echo "<td>" .$row['available_copies']. "</td>"; 
  echo "<td align='center' bgcolor='#FFFFFF'><input name='copies' type='text' id='copies' size='2'></td>";  
  echo "</tr>";
  }        
echo "<tr>";
echo "<td colspan='9' align='center' bgcolor='#FFFFFF'><input name='reserve' type='submit' id='reserve' value='Reserve Book'></td>";
echo "</tr>";
echo "</table>"; 

    
}



}
// include("secure/db_close.php");   


//include("secure/db_close.php");  
?>
   


<!--if($cop>0){  -->



<!-- this is my footer div -->
<div id="footer">
Copyright © 2010 | <a href="privacy.php">Privacy Policy</a> | <a href="terms.php">Terms of Service</a>
<img src="images/mini_logo1.png" align="right" style="margin-right:5" />
</div>   <!-- End footer div -->

    </div>
    </div>
  </body>
</html>

 

MOD EDIT:

 . . . 

BBCode tags added.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.