When I click the borrow button, it should execute my borrowing.php but it just returns to itself. It's not executing the action. Is there some way I can solve this?
<form id = "myform" action="includes/borrowing.php" method = "POST" enctype ="multipart/form-data">
<div class = "form-group pull-left">
<label>Student Name:</label>
<br />
<select name = "fIDNumber" id = "student">
<option value = "" selected = "selected" disabled = "disabled">Select an option</option>
<?php
$qborrow = $con->query("SELECT * FROM `tblstudents` ORDER BY `fLastName`") or die(mysqli_error());
while($fborrow = $qborrow->fetch_array()){
?>
<option value = "<?php echo $fborrow['fIDNumber']?>"><?php echo $fborrow['fFirstName']." ".$fborrow['fMiddleName']." ".$fborrow['fLastName']?></option>
<?php
}
?>
</select>
</div>
<div class = "form-group pull-right">
<button name = "save_borrow" class = "btn btn-primary"><span class = "glyphicon glyphicon-thumbs-up"></span> Borrow</button>
</div>
<table id = "table" class = "table table-bordered">
<thead>
<th>Select</th>
<th> Code </th>
<th> Title </th>
<th> Author </th>
<th> Category </th>
<th> Shelf Location </th>
<th> Edition </th>
</thead>
<tbody>
<?php
$sql = $con->query("SELECT * FROM `tblbooks`") or die(mysqli_error());
while($row = $sql->fetch_array()){
$q_borrow = $con->query("SELECT * FROM `tblbooks` WHERE `fBookCode` = '$row[fBookCode]' and `fStatus` = '$row[fStatus]'") or die(mysqli_error());
$status = $q_borrow->fetch_array();
?>
<tr>
<td>
<?php
if($status == 'Borrowed'){
echo "<center><label class = 'text-danger'>Not Available</label></center>";
}else{
echo '<input type = "hidden" name = "fBookCode[]" value = "'.$row['fBookCode'].'"><center><input type = "checkbox" name = "selector[]" value = "1"></center>';
}
?>
</td>
<td><center> <?php echo $row['fBookCode']; ?> </center></td>
<td> <?php echo $row['fTitle']; ?> </td>
<td> <?php echo $row['fAuthor']; ?> </td>
<td> <?php echo $row['fCategory']; ?> </td>
<td> <?php echo $row['fShelfLocation']; ?> </td>
<td> <?php echo $row['fEdition']; ?> </td>
</tr>
<?php
}
?>
</tbody>
</table>
</form>
Here's my borrowing.php This is not yet fully finished but atleast it should be able to read this. I tried using a link but it won't read my fields anymore. I want to use the form method but it really is not seeing this.
<?php
require_once 'connection_db.php';
if(!ISSET($_POST['fIDNumber'])){
echo '
<script type = "text/javascript">
alert("Select student name first");
window.location = "../borrow.php";
</script>
';
}else{
if(!ISSET($_POST['selector'])){
echo '
<script type = "text/javascript">
alert("Selet a book first!");
window.location = "../borrow.php";
</script>
';
}else{
foreach($_POST['selector'] as $key=>$value){
$fIDNumber = $_POST['fIDNumber'];
$fBookCode = $_POST['fBookCode'][$key];
$date = date("Y-m-d", strtotime("+8 HOURS"));
$conn->query("INSERT INTO `tbltransactions` VALUES('', '$fBookCode', '$fIDNumber', '$book_qty', '$date', 'Borrowed')") or die(mysqli_error());
echo '
<script type = "text/javascript">
alert("Successfully Borrowed");
window.location = "../borrow.php";
</script>
';
}
}
}
Looks like you were trying to access the $row in an incorrect manner. Try this:
$fBookCodeVal = $row['fBookCode'];
$fStatus = $row['fStatus'];
$q_borrow = $con->query("SELECT * FROM `tblbooks` WHERE `fBookCode` = '$fBookCodeVal' and `fStatus` = '$fStatus'") or die(mysqli_error());
Also I noticed that you were not using prepared statements(with binded parameters). It seems as though $row is being populated via a query which of course if a row's fBookCode value contained malicious code then SQL injection could be performed.
Also change:
$status = $q_borrow->fetch_array();
to
$status = $q_borrow->mysqli_fetch_assoc();
and then update:
if($status['status'] == 'Borrowed')
Related
I have a MYSQL table with Update link button on each row. he Update link goes to update.php which has a form. now the problem is i want to fetch the id from the row clicked on to update.php and show that id on update form in id field. i have transfered the id variable through link. but in other .php file it is printing anything.
i want to print id in update form field when i click on update button
Here is the code in index.php file
Display.php=>
<html>
<head><title>Support Page</title></head>
<body>
<table border="2">
<tr>
<th>ID</th>
<th>NAME</th>
<th>Email</th>
<th>phone no</th>
<th>Product Name</th>
<th>Company</th>
<th>Query</th>
<th>Any Other Info</th>
<th>Status</th>
<th>Date</th>
<th>Operations</th>
</tr>
<?php
$query ="SELECT * FROM query_data";
$data = mysqli_query($conn,$query);
$total = mysqli_num_rows($data);
echo $result['id']." ".$result['Name']." ".$result['Email']." ".$result['phone_no']." ".$result['Product_Name']." ".$result['Company']." ".$result['Query']." ".$result['Any_Other_Query']." ".$result['Status']." ".$result['Date'];
if($total!=0)
{
while($result = mysqli_fetch_assoc($data))
{
echo "
<tr>
<td>".$result['id']."</td>
<td>".$result['Name']."</td>
<td>".$result['Email']."</td>
<td>".$result['phone_no']."</td>
<td>".$result['Product_Name']."</td>
<td>".$result['Company']."</td>
<td>".$result['Query']."</td>
<td>".$result['Any_Other_Info']."</td>
<td>".$result['Status']."</td>
<td>".$result['Date']."</td>
<td><a href = 'http://mexyz.tech/?page_id=586?i=$result[id]'>Update</td>
</tr>";
}
}else{
echo "No record found";
}
?>
</table>
</body>
</html>
and this is update.php=>
<?php
include("connection.php");
$id = $_GET['i'];
?>
form action="" method="GET">
Enter id : <input type="text" value="<?php echo "id" ?>" name="id" required>
<br><br>
Enter Query Code: <input type="text" name="QC">
<br><br>
<td>
<select name="Status">
<option>Choose Option</option>
<option>Pending</option>
<option>Assigned</option>
<option>Resolved</option>
</select>
</td>
<br><br>
<input type="submit" value="Submit" name="Submit">
</form>
<?php
if (isset($_POST['Submit'])) {
$ids = $_POST['id'];
$s = $_POST['Status'];
$QC = $_POST['QC'];
$tablename = "query_data";
$data = array("Status" =>$s, "Query_Code" =>$QC);
$wherecondition = array('id' => $ids);
$updated = $wpdb->update( $tablename, $data, $wherecondition );
// $sql = $wpdb->insert("query_data",array("Status" =>$s,"Query_Code" =>$QC));
if ($updated == true) {
echo "<script> alert('Submitted Successfully!...')</script>";
}
}
?>
</div><!-- #primary -->
<?php
if (astra_page_layout() == 'right-sidebar') :
get_sidebar();
endif;
get_footer();
I dont know exactly what you are trying to achieve from your code but
you should pass your update link with a query string if you want to get the id in your update.
this is how to pass the link with a query string
<td>Update</td>
if you do it that way then got to your update page
and do this
if (isset($_GET[id])){
$id = $_GET[id];
}
index.php
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'Engineering')">Engineering</button>
<button class="tablinks" onclick="openCity(event, 'LAW')">LAW</button>
</div>
<div id="Engineering" class="tabcontent">
<table class="items">
<tr>
<th>State</th>
<th>College Name</th>
</tr>
<?php
$query = "select * from college where field = 'engineering'";
$show = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($show))
{
?>
<tr>
<td><?php echo $fetch['state']?></td>
<td><?php echo $fetch['college_name']?></td>
<td>
edit
</td>
</tr>
<?php
}
?>
</table>
</div>
<div id="Law" class="tabcontent">
<table class="items">
<tr>
<th>State</th>
<th>College Name</th>
</tr>
<?php
$query = "select * from college where field = 'law'";
$show = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($show))
{
?>
<tr>
<td><?php echo $fetch['state']?></td>
<td><?php echo $fetch['college_name']?></td>
<td>
edit
</td>
</tr>
<?php
}
?>
</table>
</div>
edit.php
<?php
if(isset($_POST['update']))
{
$college_name = $_POST['colleges'];
$state = $_POST['state'];
$sqli = "update college set college_name = '$college_name', state = '$state' where id = '$id'";
$results = mysqli_query($link,$sqli);
if($result == true)
{
$msg .= "<p style='color:green;'>Your data update successfully</p>";
}
else
{
$msg .= "<p style='color:red;'>Errror!</p>";
}
}
?>
<form method="POST" enctype="multipart/form-data" >
<select name="state" id="state">
<option value="<?php echo $stateid; ?>"><?php echo $statename; ?></option>
<option value="">Select State</option>
<?php
$sql = "select * from statemaster";
$result = mysqli_query($link,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<option value=".$row['stateid'].">".$row['statename']."</option>";
}
?>
</select>
<select name="colleges" id="colleges">
<option value="<?php echo $college_name; ?>"><?php echo $college_name; ?></option>
<option value="">Select College</option>
</select>
<button type="submit" name='update' id='update'>update</button>
</form>
In this code when I click on edit button then it will go to edit.php page where I get id from url and run update query after updating table college the data will update but when I move from edit page to index.php page the data will remain same but in database update data will be there. So, How can I fix this issue ?
Thank You
Check for caching. It could be that the browser is not going to the server in order to get the contents of index.php, as it thinks it has it.
Try calling index.php with a variable, like:
Home
Hi Guys please assist with the below code,
I want to update a selected record using MVC, MVC that does not include view object declaration in the model.
-my code list members.
-allow for clicking edit selcted record.
-validates user against fields...
else I call method for updating.
after clicking update I get no updates. please assist I have got a jobbut I cant update, and I might get fired. please.
Controller - Index.php
<?php
require('db/ABSA/conn.php');
require('model.php');
$conn = new Connect_to_db();
$conn->Connect_db();
$m = new model();
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action']; } else {
$action = 'view_members';
$members = $m->getDetails();
include('view_members.php');
}
if ($action == 'update_members') {
global $member_id;
$member_id= $_GET['member_id'];// Get the current ID
$members = $m->getDetails_member($member_id);
//var_dump($members);
//var_dump($member_id);
$fname = $_POST['First_Name'];
$id_num = $_POST['ID_Number'];
$checkdate = $_POST['Checked_Date'];
$checked_by = $_POST['Checked_by'];
if(empty($fname)){
echo "Please enter fname!";include('update_members.php');exit;
header('Refresh: 3; url=index.php');
}else
if(empty($id_num)){
echo "Please enter id_num";include('update_members.php');exit;
header('Refresh: 3; url=index.php');
}
if(empty($checkdate)){
echo "Please enter checkdate!";include('update_members.php');exit;
header('Refresh: 3; url=index.php');
}else
if(empty($checked_by)){
echo "Please enter checked_by";include('update_members.php');exit;
}
else{
$member_i= $_GET['member_id'];
$m->update_members($member_i,$fname,$id_num,$checkdate,$checked_by);
header('Refresh: 4; url=index.php');
}
}
?>
Here is my view :
<!DOCTYPE html>
<html>
<head>
<meta content="width=device-width, minimum-scale=1, maximum-scale=1" name="viewport">
<title>Update and view </title><link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' integrity='sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u' crossorigin='anonymous'>
</head>
</head>
<div class="container">
</br>
<table class="table table-hover">
<tr>
<th>Member ID </th>
<th>First Name </th>
<th> ID Number </th>
<th>Checked Date </th>
<th>Checked by</th>
</tr>
<?php foreach($members as $row):
echo "
<td>$row[m_id]</td>
<td>$row[First_Name]</td>
<td>$row[ID_Number]</td>
<td>$row[Checked_Date]</td>
<td>$row[Checked_by]</td>
";
?>
<td>
<form method='GET' action='index.php' >
<input type ="hidden" name="action" value="update_members" />
<input type ="hidden" name="member_id" value="<?php echo $row['m_id']; ?>" />
<input type = "submit" value="Edit">
</form>
</td>
</tr>
<?php endforeach;?>
</table>
</div>
</body>
</html>
Here is my model :
<?php
error_reporting(E_ALL & ~E_NOTICE);
/*
Author name : Neo Lloyd Sono
Occupation : Junior web developer#PLP Group
Programme name : Update and view leads members
date : 2016-09-09
place : PLP Group, 10 hunter ...
*/
//query, prepare, execute, and fetch,
class model{
function getDetails(){
global $pdo;//globalize connection
$tbl_name = "leads";//use var for table
$stm = $pdo->prepare("select * from $tbl_name");
$stm->execute();
while($row[] = $stm->fetch()){
$i++;
$row[$i] = $stm->fetch();
}
return $row;
}
public static function getDetails_member($id){
global $pdo;//globalize connection
$tbl_name = "leads";//use var for table
$stm = $pdo->prepare("select * from $tbl_name where m_id= :m_id limit 10 ");
$stm->bindParam(':m_id',$id);
$stm->execute();
$row = $stm->fetch();
return $row;
}
function update_members($id,$fname,$id_num,$checkdate,$checked_by){
global $pdo;//globalize connection
$tbl_name = "leads";//use var for table
//$arr = array(":First_Name"=>$fname,":ID_"=>$id_num,":Checked_D"=>$checkdate,":checked_b"=>$checked_by,":id"=>$id);
$stm = $pdo->prepare("update `leads` set `First_Name` ='$fname',
`ID_Number` ='$id_num',
`Checked_Date` ='$checkdate',
`Checked_by` ='$checked_by'
where `m_id` ='$id'");
$stm->bindParam(':id',$id,PDO::PARAM_INT);
$stm->bindParam(':First',$fname,PDO::PARAM_STR, 12);
$stm->bindParam(':ID_',$id_num,PDO::PARAM_STR, 12);
$stm->bindParam(':Checked_D',$checkdate);
$stm->bindParam(':Checked_b',$checked_by,PDO::PARAM_STR, 12);
$stm->execute();
if($stm){
echo "Updated ";
}else{
echo "Not Updated ";
}
var_dump($member_id);
var_dump($stm);
return $stm;
}
}
?>
here is my update records view :
<!DOCTYPE html>
<html>
<head>
<meta content="width=device-width, minimum-scale=1, maximum-scale=1" name="viewport">
<title>Update and view </title>
<div class="container">
</br>
<table class='table table-hover'>
<tr>
<th>Member ID </th>
<th>First Name </th>
<th> ID Number </th>
<th>Checked Date </th>
<th>Checked by</th>
</tr>
<?php
//var_dump($members);
?> <form method='post' action='.' ><td style = "color:white"> </td>
<td><input type = 'text' value="<?php echo $members['First_Name']; ?>" name='First_Name'/></td>
<td><input type = 'text' value="<?php echo $members['ID_Number']; ?>" name='ID_Number'/></td>
<td><input type = 'text' value="<?php echo $members['Checked_Date']; ?>" name='Checked_Date'/></td>
<td><input type = 'text' value="<?php echo $members['Checked_by']; ?>" name='Checked_by'/></td>
<tr><td>
<input type="hidden" name="action" value="update_members" />
<input type="hidden" name="member_id" value="<?php echo $members['m_id']; ?>" />
<input type = 'submit' name="u" value='Update' />
</td>
</form>
</tr>
</table>
</form>
</div>
</body>
</html>
I have 3 tables in my database (standards, courses, students). I want to display Fname and gender from students table enrolled in selected 'standard' and 'course' from dropdown - but the "submit" button doesn't seem to work. The code is as shown below. I am sure it is connected to the database as the dropdowns for 'courses' and 'standards' work:
<html>
<head>
<title>Courses</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div id="container">
<div id="wrapper">
<h1> Students</h1>
<div id="data">
<form action="index.php" method="POST">
<select name="standards">
<option>Standard</option>
<?php
include 'includes/dbconnect.php';
$query1 = "SELECT * FROM standards";
$result1 = mysql_query($query1);
while($rows1 = mysql_fetch_array($result1)){
$standardID = $rows1['id'];
$rowsData1 = $rows1['standardName'];
?>
<option value="<?php echo $standardID; ?>">
<?php
echo $rowsData1; ?></option>
<?php
}
?>
</select>
</div>
<div id="data2">
<select name="courses">
<option>Courses</option>
<?php
$query2 = "SELECT * FROM courses";
$result2 = mysql_query($query2);
while($rows2 = mysql_fetch_array($result2)){
$coursesID = $rows2['id'];
$rowsData2 = $rows2['courseName'];
?>
<option value="<?php echo $courseID;?>">
<?php echo $rowsData2; ?></option> <?php }?>
</select>
</div>
<div id="submit">
<input type="submit" name="submit" id="submit" value="submit"/>
<table border="1" id="table1">
<tr>
<th>Student Name</th>
<th>Gender</th>
</tr>
<?php
if(isset($_POST['submit'])){
$standardName = $_POST['standards'];
$courseName = $_POST['courses'];
$query3 = "SELECT students.Fname, students.gender
FROM students
WHERE students.standardID = '$standardName'
AND students.courseID = '$courseName'";
$result3 = mysql_query($query3);
while($rows3 = mysql_fetch_array($result3)){
//$dataID = $rows3['id'];
$studentName = $rows3['FName'];
$gender = $rows3['gender'];
?>
<tr>
<td><?php echo $studentName; ?></td>
<td><?php echo $gender; ?></td>
<tr>
<?php
}
}
?>
</table>
</div>
</div>
</body>
</html>
Inscribe the input elements within a form tag with post as method. Input elements with type submit are used for submission of forms.
You only do anything with the submitted form data if that data includes a submit field.
if(isset($_POST['submit'])){
However you have:
<input type="submit" name="submit" id="submit"/>
Your submit button don't have a value, so nothing will be included in the form data for it.
Whenever the hyperlink with the yes id is clicked i dont want the page to refresh and then show the status, i want the status to change instant without page refreshing. I know Ajax deals with this, but can anyone provide me with a working example with my code please? As it melting my head :/
<h3 class="page-header"> Enquiries </h3>
<form id="enquiry" method="post" action="enquiry_csv.php">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th> First Name</th>
<th> Last Name</th>
<th>Email</th>
<th>Message</th>
<th>Date</th>
<th>Responded to Enquiry?</th>
<th>Status</th>
<th></th>
<th><input class='btn-success' name='export' id='btnExport' type='submit' value='Export to CSV'/></th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM enquiries";
$select_enquiries = mysqli_query($connection,$query);
while($row = mysqli_fetch_assoc($select_enquiries)) {
$Enquiry_ID = $row['Enquiry_ID'];
$FirstName = $row['First_Name'];
$LastName =$row['Last_Name'];
$Email = $row['Email'];
$Message = $row['Message'];
$Date =$row['Date'];
$Responded =$row['Responded'];
echo "<tr>";
echo "<td>$FirstName </td>";
echo "<td>$LastName </td>";
echo "<td>$Email </td>";
echo "<td>$Message </td>";
echo "<td>$Date </td>";
echo "<td> <a id='yes' class='success' style='' href='enquiries.php?Yes=$Enquiry_ID'>Yes</a> | <a class='success' href='enquiries.php?No=$Enquiry_ID'>No</a> </td>";
echo "<td> $Responded</td>";
echo "<td> <a class='btn btn-danger' href ='enquiries.php?delete=$Enquiry_ID'>Delete</a> </td>";
echo "</tr>";
}
?>
<?php
if(isset($_GET['Yes'])){
$enquiry_id = $_GET['Yes'];
$query = "UPDATE enquiries SET Responded = 'Yes' WHERE Enquiry_ID = {$Enquiry_ID}";
$query = mysqli_query($connection, $query);
}
if(isset($_GET['No'])){
$enquiry_id = $_GET['No'];
$query = "UPDATE enquiries SET Responded = 'No' WHERE Enquiry_ID = {$Enquiry_ID}";
$query = mysqli_query($connection, $query);
}
if(isset($_GET['delete'])){
$review_id = $_GET['delete'];
$query = "DELETE FROM enquiries WHERE Enquiry_ID = {$Enquiry_ID} ";
$delete_query = mysqli_query($connection, $query);
}
?>
<tr>
<td></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
</form>
You can do it with jquery ajax api it performs an asynchronous HTTP (Ajax) request
http://api.jquery.com/jquery.ajax/
Refer the above link which #Sanya Zahid posted and try to do some thing like this:
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" >
$(function() {
$(".submit").click(function() {
var name = $("#name").val();
var age = $("#age").val();
var dataString = 'name='+ name +'&age='+ age;
if(name=='' || age =='')
{
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}
else
{
$.ajax({
type: "POST",
url: "submit.php",
data: dataString,
success: function(){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
</script>
<form method="post" name="form">
<input id="name" name="name" type="text" /><br>
<input id="age" name="age" type="text"/>
<div>
<input type="submit" value="Submit" class="submit"/>
<span class="error" style="display:none"> Please Enter Data</span>
<span class="success" style="display:none"> Data Saved!!</span>
</div>
</form>
Here submit.php contains database related stuff(insert/update/delete). I just added name and age fileds here in code. Add fields as per your form.
submit.php :
<?php
$conn = mysqli_connect("localhost","root","","test");
/* Insert form data with out page refresh */
if($_POST)
{
$name=$_POST['name'];
$age = $_POST['age'];
mysqli_query($conn,"Query will come here");
}else{
echo "Please try again!!";
}
/* Insert form data with out page refresh */
?>