I want to update my database by using checkbox. But it seems like the code doesn't loop in a foreach. Where did I go wrong ? Is there have anything wrong with my foreach ?
<?php
foreach($_POST['listMenu'] as $checkBox1){
$strUpdateData = "
update tblMenuAkses
set MenuAkses = 'N'
where MenuAksesID = $checkBox1
";
$rsUpdateData = odbc_exec($dbconnVOT,$strUpdateData);
if($rsUpdateData){
echo "Success";
} else {
echo "ERROR <br>";
echo odbc_errormsg($dbconnVOT);
}
}
//It loops here
var_dump($_POST['listMenu']);
?>
<form method="post" action="skrinMenu.php" enctype="multipart/form-data">
<table align= "center" border="1" cellspacing="0" cellpadding="2">
<?php
$strgettable="select * from tblMenuAkses";
$varRecCount=0;
$rs=odbc_exec($dbconnVOT, $strgettable);
if($rs){
while (odbc_fetch_row($rs)) {// loop a table from database and checkbox too
$varRecCount++;
echo '<tr><td>';
echo '<center> '.$varRecCount.'</center>';
echo '</td>';
echo '<td>';
echo '<center> '.odbc_result($rs,"UsrGrpNama").'</center>';
echo '</td>';
echo '<td>';
echo '<center> '.odbc_result($rs,"MenuNama").'</center>';
echo '</td>';
echo '<td>';
echo '<center> '.odbc_result($rs,"MenuAkses").'</center>';
echo '</td>';
echo '<td>';
echo '<center> <input type="checkbox" name="listMenu[]" id="listMenu" value='.odbc_result($rs,"MenuAksesID").'> </center>';
echo '</td>';
echo '</td></tr>';
}
}
?>
</table>
<input type="submit" name="btnMenu" id="btnMenu" value = "Kemaskini">
</form>
The result of the var_dump($_POST['listMenu']) : NULL
Related
I'm creating a contact based system, i have a contact list and want it to go to a full contact page when i click on a button, but the variable is being detected as a function?
<div id="po2" style="Margin:10% ">
<h1>Contacts</h1>
<?php
$sql = "SELECT * FROM `contacts`";
$query = mysqli_query($conn, $sql);
echo '<table class="data-table">';
echo'<thead>';
echo'<tr>';
echo '<th>Forename</th>';
echo '<th>Surname</th>';
echo '<th>Other</th>';
echo'</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_array($query))
{
echo '<tr>';
echo '<td>'.$row['Forename'].'</td>';
echo '<td>'.$row['Surname'].'</td>';
echo $var==$row['Forename']("<td><a href='View.php?ID= " . urlencode($var) ."'>
<button type='button'>link</button>
</a></td>");
echo '</tr>';
}
echo'</tbody>';
echo '</table>';
?>
</div>
You are using a comparison of $var and the $row. Try setting $var to the $row each loop iteration.
echo '<thead>';
echo '<tr>';
echo '<th>Forename</th>';
echo '<th>Surname</th>';
echo '<th>Other</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_array($query))
{
$var = $row['Forename'];
echo '<tr>';
echo '<td>' . $var . '</td>';
echo '<td>' . $row['Surname'] . '</td>';
echo "<td><a href='View.php?ID=" . urlencode($var) . "'><button type='button'>link</button></a></td>";
echo '</tr>';
}
echo '</tbody>';
I want to update the value in a certain row. But it seems impossible since this table (from database) doesn't have the primary key. It only have two foreign key in its column. What should I do ? please, any help ?
here is my code :
<table align= "center" border="1" cellspacing="0" cellpadding="2">
<?php
$strgettable="select tblUsrGrp.UsrGrpNama, tblmenu.MenuNama, tblmenuakses.MenuAkses
FROM tblUsrGrp
inner join tblMenuAkses
on tblUsrGrp.UsrGrpID = tblMenuAkses.usrgrpid
inner join tblMenu
on tblMenu.menuid = tblMenuAkses.menuid";
$varRecCount=0;
$rs=odbc_exec($dbconnVOT, $strgettable);
if($rs)
{
echo '<tr><td style=width:10%;>';
echo '<center><b> No. </b></center>';
echo '</td>';
echo '<td style=width:30%;>';
echo '<center><b> User Group </b></center>';
echo '</td>';
echo '<td style=width:20%;>';
echo '<center><b> Menu Name </b></center>';
echo '</td>';
echo '<td style=width:20%;>';
echo '<center><b> Menu Akses </b></center>';
echo '</td>';
echo '<td style=width:20%;>';
echo '<center><b> Update </b></center>';
echo '</td></tr>';
while (odbc_fetch_row($rs))
{
$varRecCount++;
echo '<tr><td>';
echo '<center> '.$varRecCount.'</center>';
echo '</td>';
echo '<td>';
echo '<center> '.odbc_result($rs,"UsrGrpNama").'</center>';
echo '</td>';
echo '<td>';
echo '<center> '.odbc_result($rs,"MenuNama").'</center>';
echo '</td>';
echo '<td>';
echo '<center> '.odbc_result($rs,"MenuAkses").'</center>';
echo '</td>';
echo '<td>';
echo '</td>';
echo '</td></tr>';
}
}
?>
</table>
<div style="text-align:center;">
<input type="button" name="btnUpdate" id="btnUpdate" onClick="javascript:sendData('updatedata');" value = "Update">
</div>
any help would be much appreciated . TQ
This is my PHP & HTML code, it all works fine, except it gives me some error as "Notice: Undefined index: attend in C:\xampp\htdocs\ams\postattentry.php on line 43"
<?php
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("index.php");
exit;
}
include('header.php');
?>
<div class="row-fluid sortable">
<div class="box span8">
<div class="box-header well" data-original-title>
<h2><i class="icon-edit"></i> Attendance Entry</h2>
</div>
<div class="box-content">
<?php
$hour=$_GET["hour"];
$date=$_GET["date"];
$year=$_GET["year"];
$sem=$_GET["sem"];
$sec=$_GET["sec"];
$dept=$_GET["dept"];
$bat=$_GET["bat"];
if((strpos($fgmembersite->UserAttrib(),"i")>0) and $fgmembersite->attentrycheck($hour,$date,$year,$sem,$sec,$dept,$bat,$fgmembersite->Userempid()))
{
$error = $fgmembersite->GetErrorMessage();
if(!empty($error))
{
echo '<div class="alert alert-info">';
echo $error;
echo '</div>';
}
if(!$fgmembersite->DBLogin())
{
die("Error Locating the Students Database.");
}
if($bat=="ALL")
{
$qryae = "Select * from $fgmembersite->studtblname where dept='$dept' and year='$year' and sem='$sem' and sec='$sec'";
}
else
{
$qryae = "Select * from $fgmembersite->studtblname where dept='$dept' and year='$year' and sem='$sem' and sec='$sec' and bat='$bat'";
}
if(!($uapae=mysql_query( $qryae ,$fgmembersite->connection)))
{
die("Error Locating the Students Database.");
}
echo '<table class="table table-striped table-bordered">';
echo '<thead>';
echo '<tr>';
echo '<td align="center">S.No.</td>';
echo '<td align="center">Roll No.</td>';
echo '<td>Student Name</td>';
echo '<td align="center">Attendance</td>';
echo '</tr>';
echo '</thead>';
echo '<form id="attentry" class="form-horizontal" action="postattentry.php" method="post">';
echo '<tbody>';
$i=0;
$j=1;
while ($uapres = mysql_fetch_array($uapae)) {
echo '<tr>';
echo "<td>{$j}</td>";
echo "<td>{$uapres['rollno']}<input type='hidden' name='rollno[$i]' value='{$uapres['rollno']}' /></td>";
echo "<td>{$uapres['sname']}</td>";
echo "<td><div class='control-group'>
<div class='controls'>
<label class='checkbox'>
<input type='checkbox' name='attend[$i]' id='attend[$i]' value='1' checked=''/>
</label>
</div>
</div></td>";
echo '</tr>';
++$i;
++$j;
}
echo '<tr>';
echo '<td align="center">Total</td>';
echo '<td align="center">Hour</td>';
echo '<td>Date</td>';
echo '<td align="center">Year</td>';
echo '</tr>';
echo '<tr>';
echo "<td>{$i}<input type='hidden' name='fcount' id='fcount' value='{$i}'/></td>";
echo "<td>{$hour}<input type='hidden' name='fhour' id='fhour' value='{$hour}'/></td>";
echo "<td>{$date}<input type='hidden' name='fdate' id='fdate' value='{$date}'/></td>";
echo "<td>{$year}<input type='hidden' name='fyear' id='fyear' value='{$year}'/></td>";
echo '</tr>';
echo '<tr>';
echo '<td align="center">Dept.</td>';
echo '<td align="center">Sem</td>';
echo '<td>Section</td>';
echo '<td align="center">Batch</td>';
echo '</tr>';
echo '<tr>';
echo "<td>{$dept}<input type='hidden' name='fdept' id='fdept' value='{$dept}'/></td>";
echo "<td>{$sem}<input type='hidden' name='fsem' id='fsem' value='{$sem}'/></td>";
echo "<td>{$sec}<input type='hidden' name='fsec' id='fsec' value='{$sec}'/></td>";
echo "<td>{$bat}<input type='hidden' name='fbat' id='fbat' value='{$bat}'/></td>";
echo '</tr>';
echo '<tr>';
echo "<td></td>";
echo "<td><button type='submit' name='submit' value='Submit' class='btn btn-primary'>Register</button></td>";
echo "<td><button type='reset' class='btn'>Reset</button></td>";
echo "<td></td>";
echo '</tr>';
echo '</tbody>';
echo '</form>';
echo '</table>';
}?>
</div>
</div><!--/span-->
</div><!--/row-->
<?php include('footer.php'); ?>
The processing form code is...
<?php
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("index.php");
exit;
}
include('header.php');
?>
<div class="row-fluid sortable">
<div class="box span8">
<div class="box-header well" data-original-title>
<h2><i class="icon-edit"></i> Post Attendance Entry</h2>
</div>
<div class="box-content">
<?php
echo $hour=$_POST['fhour'];
echo $date=$_POST['fdate'];
echo $year=$_POST['fyear'];
echo $sem=$_POST['fsem'];
echo $sec=$_POST['fsec'];
echo $dept=$_POST['fdept'];
echo $bat=$_POST['fbat'];
echo $count=$_POST['fcount'];
if(!$fgmembersite->DBLogin())
{
die("Error Locating the Students Database.");
}
if(!$fgmembersite->EnsureAttTable())
{
die("Unable to Create Attendance Table.");
}
$attvars = array();
$i = 0;
while ($i < $count) {
echo $rollno= $_POST['rollno'][$i];
echo $attend= $_POST['attend'][$i];
++$i; }
?>
</div>
</div><!--/span-->
</div><!--/row-->
<?php include('footer.php'); ?>
I tried the same code by changing input type from the checkbox to text, it worked fine. I was unable to figure out where the problem is.
In a form, if a checkbox is not checked, it doesn't get posted.
change:
echo $attend= $_POST['attend'][$i];
to:
if(isset($_POST['attend']) && isset($_POST['attend'][$i]){
echo $attend =$_POST['attend'][$i]
} else {
echo $attend =0; //or whatever value you wish
}
If a checkbox is not checked, it is not sent at all as a POST variable.
In order to identidity if a checkbox is not checked, you must do a logic step.
if (isset($_POST[attend[$i]])){
{
$attended=1;
}else{
$attended=0;
}
I am officially too slow.
I have 2 search forms. One is linked to my local db of records and the other one links through an API to another db.
I want to now when my db does not have the required record, have it use the second search form without the user needing to insert the data again.
Is this possible and how do I go about merging the two?
Thanks for all the help guys.
Combination of form functions:
<div class="article">
<h2>
<br>
SEARCH A REFERENCE
<br>
</h2>
</center>
<body>
<div id="formsearch">
<br>
<form method="post" action="" name="form1" id="form1" >
Enter the ID Number
<br><br>
<b>Search Record </b> <input type="text" name="term" /><br />
<br>
<input type="submit" name="submit" value="Search" class="btn" />
</form>
</div>
<h3> <center> Search Result (s) </h3>
<?php
if ($_REQUEST['submit']) {
$term = $_POST['term'];
$row ['employerid'] == $user_data ['user_id'];
$XX = "<br><br><div class='messagebox'><h2> <center> Oops! </h2> <p>We were only to retrieve a partial record on <strong>$term</strong> you have entered. Please make use of our contact form if you would like us to get you your reference. Be sure to enter the three required fields. <a href='Mailforms/refrequest.php' class='lightbox'>Click Here!</a> or to validate the id <a href='idverification.php'> Click here</a></p>
<br />
</div>";
$sql = mysql_query("SELECT idnumber,
firstname,
lastname,
companyname,
jobtitle,
dismissal,
terminationdate,
startdate,
CONCAT(TIMESTAMPDIFF(YEAR,startdate,terminationdate), ' years and ',
MOD(TIMESTAMPDIFF(MONTH,startdate,terminationdate),12), ' months ') AS time_diff
FROM ref_employees
WHERE idnumber= '$term'")
or die('Error in query : $sql. ' .mysql_error());
if (mysql_num_rows($sql) > 0 ) {
while ($row = mysql_fetch_array($sql)){
if ($row ['employed'] == '1') {
echo '<h4>Currently Employed By : '.$row['companyname'];
echo '</h4> ';
echo 'Any doubts? Enquire about this candidate ';
}
if ($row ['employed'] == '0') {
echo ' <h4>Some Available Options For:</h4>';
include 'includes/admenu.php';
echo '<h4>Not Currently employed '.$row['companyname'];
echo '</h4> ';
}
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID Number</th> <th>First Name</th> <th>Last Name</th><th>company</th> <th>Job Title</th> <th>Period</th><th>Reason for dismissal</th><th></th><th></th></tr>";
echo "<tr>";
echo '<td>' . $row['idnumber'] . '</td>';
echo '<td>' . $row['firstname'] . '</td>';
echo '<td>' . $row['lastname'] . '</td>';
echo '<td>' . $row['companyname'] . '</td>';
echo '<td>' . $row['jobtitle'] . '</td>';
echo '<td>' .($row['startdate'] + $row['terminationdate']).'</td>';
echo '<td>' . $row['dismissal'] . '</td>';
echo '<td>Achievements</td>';
echo '<td>training</td>';
echo "</tr>";
}
// close table>
echo "</table>";
}
}
else
{
$id_number = $_POST['term'];
$authenticate = authentication();
$login_message = $authenticate['error_code'];
$session_id = $authenticate['session_id'];
$id_verification = verify_id($session_id, $id_number);
}
if (!empty($id_verification)){
echo '<br/>';
echo '<h3>Search Information</h3>';
echo 'Time Stamp: ';
echo $id_verification['TIME_STAMP'];
echo 'SOAP Result: ';
echo $id_verification['LOGIN-MESSAGE'];
echo ' ';
echo $id_verification['SEARCH-MESSAGE'];
echo ' ';
echo $id_verification['SEARCH-RESULT'];
echo '<br/>';
echo '<h3>Data: </h3>';
echo '<br/>ID Number: ';
echo $id_verification['IDNUMBER'];
echo '<br/>First Names: ';
echo $id_verification['FIRSTNAMES'];
echo '<br/>Last Name: ';
echo $id_verification['SURNAME'];
echo '<br/>Birthday: ';
echo $id_verification['BIRTHDAY'];
echo '<br/>Age: ';
echo $id_verification['AGE'];
echo '<br/>Gender: ';
echo $id_verification['GENDER'];
echo '<br/>Citizen: ';
echo $id_verification['CITIZEN'];
echo '<br/>Death Status: ';
echo $id_verification['DEATH-STATUS'];
echo '<br/>Death Date: ';
echo $id_verification['DEATH-DATE'];
echo '<br/>Death Place: ';
echo $id_verification['DEATH-PLACE'];
unset($_SESSION['id_verification']);
}
mysql_free_result($sql);
mysql_close($connection);
?>
Send both search form into one location.
First you need to search into local if not again search it in API. Mean you need to do the search in single server file.
Hope this clear else post me in detail.
I'm having some display problems here.
I have a "backend.php" file where I ask for two inputs.
These inputs are processed by "Addproducts.php" file and this file redirects to backend.php.
Backend.php also shows the current records in the database.
Here's the code for backend.php
<html>
<head>
<title>Inventory - Backend</title>
</head>
<body>
<form action="addproducts.php" method="post">
<table>
<tr>
<td>Product Name : </td>
<td><input type="text" name="pname"/></td>
</tr>
<tr>
<td>Product Quantity : </td>
<td><input type="text" name="productq"/></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type="submit" name="Add Product"/></td>
</tr>
</table>
</form>
<h2>Current Products</h2>
<?php
$db = mysql_connect('127.0.0.1', 'root', '') or die ('Unable to Connect.Check your connection parameters');
mysql_select_db('stock_inventory', $db) or die (mysql_error($db));
$query = 'SELECT * FROM PRODUCTS';
$result = mysql_query($query, $db) or die (mysql_error($db));
echo '<table>';
echo '<tr>';
echo '<th>Product ID </th>';
echo '<th>Producr Name </th>';
echo '<th>Product Stock </th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
echo '<br/>';
echo '</table>';
}
else
{
echo "No products in the database";
}
}
?>
</body>
</html>
It displays something like this :-
Product ID Producr Name Product Stock
1 NewProduct 1
2HTC One5
3Samsung10
4Sony10
You see?
Only the first product is aligned, the rest are not.
How do I make them all align ?
Thanks.
The reason is you are closing your table tag within the loop, move it outside the loop like follows:
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
else
{
echo "No products in the database";
}
}
echo '<br/>';
echo '</table>';
Update: A better fix (see Barmar's comment below):
if (empty(mysql_num_row($result))) {
echo "<tr><td colspan='3'>No products in the database</td></tr>";
} else {
while($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
}
echo '</table>';
Also start looking into using mysqli(http://php.net/manual/en/book.mysqli.php) or PDO (http://php.net/manual/en/book.pdo.php), mysql_ is deprecated!
I would suggest removing the
<br/>
from within your table. I believe that putting markup like line breaks inside of table markup will break the layout.