Button for updating data - php

I have a list of buttons that each pass on a different value. The code should store this value as a variable or session, which then is passed on to a function that updates the table row of the value. I tried just storing this value as a variable and then passing it on, but that didn't work so I figured I'd make it into a session. And that didn't work either, no idea where to go from here.
Here's a button example:
<tr>
<td data-title="Name"><img src="banner.jpg" width="400"></td>
<td data-title="Link">
<form name="first" action="pending.php" method="POST"><input type="hidden" name="xz" value="one"><a class="mbutton" onclick="document.forms['first'].submit();">Gedaan</a></form>
</td>
<td data-title="Status"><p class="ptable"><?= $fgmembersite->One(); ?></p></td>
</tr>
Here's where the form leads to:
<?PHP
include("./include/membersite_config.php");
$_SESSION['postdata'] = $_POST;
$bname = $_SESSION['postdata'];
if($fgmembersite->Pending($bname))
{
$fgmembersite->RedirectToURL("index.php");
}
?>
So this should pass on $bname to the function Pending().
And here's pending:
function Pending($bname)
{
if(!$this->DBLogin())
{
$this->HandleError("Database login failed!");
return false;
}
$result = mysql_query("Select * from $this->tablename where confirmcode='y'",$this->connection);
if(!$result || mysql_num_rows($result) <= 0)
{
$this->HandleError("Error");
return false;
}
$row = mysql_fetch_assoc($result);
$ux = $row['username'];
$ustat = $row['one'];
$bname = $_SESSION['postdata']['xz'];
$qry = "Update $this->tablename Set $bname='In behandeling' Where username='$ux'";
if(!mysql_query( $qry ,$this->connection))
{
$this->HandleDBError("Error inserting data to the table\nquery:$qry");
return false;
}
return true;
}

So you are saying that the Pending function is returning TRUE, and redirecting you to membership.php ? or index.php ( or are these the same ) -
if that query fails and returns false - you would just end up on the page that you had posted to .

Related

if else construct issue in value attribute of html

public function getEmployeeId() {
if (!isset($_SESSION["email"]) || !isset($_SESSION["passwrd"])) {
header("Location:index.php");
// Cannot Access this page without Login.
}
if (empty($_POST)){
$_SESSION['ids'] = "";
$query = mysqli_query($this->connection, "SELECT MAX(EmployeeId) AS EmployeeId FROM employees") or die("Query execution failed: " . mysqli_error());
while ($row = $query->fetch_assoc()) {
// Push the id to the array.
$_SESSION['ids'] = $row["EmployeeId"];
}
}
}
The above code snippet bring the latest registered employee ID from the database.
------------------------>--------------------
public function updateSalary(){
if (!isset($_SESSION["email"]) || !isset($_SESSION["passwrd"])) {
header("Location:index.php");
// This code Snippet ensures that in order to access this page Employee needs to be Login.
}
$EmployeeID = (isset($_GET['EmployeeId']) ? $_GET['EmployeeId'] : '');
$query = mysqli_query($this->connection, "SELECT * FROM salary WHERE EmployeeId= '" . $EmployeeID . "'") or die("Query execution failed: " . mysqli_error());
while ($row = $query->fetch_assoc()){
// Push the id to the array
$_SESSION['eids'] = $row["EmployeeId"];
$_SESSION['salry'] = $row["Salary"];
if ($_SESSION['ids']) {
$_SESSION['ids'] = "";
}
}
the above code snippet is my update function to update each record. And i have included both the above function in my html form at the top.
The Problem is that : As my insertion and updation form is same, so the session value which is in if statement is echoed in the text box , else part does not work even if session is unset in if part, What should i do ? See the below code
here is the value attribute :
<td>
<input type="number" name="EmployeeId" placeholder="EmployeeId" value="<?php if (isset($_SESSION["ids"])){echo $id_salaries;}else{echo $emp_id;} ?>" id="EmployeeId" autocomplete="off" class="form-control" readonly>
</td>
This is not session unset $_SESSION['ids'] = ""; this is your assigning empty string value to session . session unset should be unset($_SESSION['ids'])

PHP function return true or false

I am trying to create a function that will check against the data from the database. If the results are empty, continue with the code. If else, abort.
Here is an example of my code.
function check_if_blocked(){
global $wpdb;
$user = '1';
$results = $wpdb->get_results("SELECT * FROM mytable WHERE user = $user");
if(empty($results)){
$var = false;
} else {
$var = true;
}
return $var;
}
Function check_if_blocked is going to be used multiple through out the plugin.
Here is an example of how I plan on using check_if_blocked()..
function msg_form(){
if(check_if_blocked(){
echo '<form action="" method="post">
<input type="text" name="msg">
<input type="submit" name="submit" value="send">';
}
}
Regardless on how I switch around the true, false, and even if(!check_if_blocked...
You are trying to return the wrong value, get_results will return true if the query was successful, not if it found matches, so, it might return true even if there are no matching records found, try this instead:
function check_if_blocked()
{
global $wpdb;
$user = '1';
$results = $wpdb->get_results("SELECT * FROM mytable WHERE user = $user");
if(!$results) {return false;} // query failed, no way to know if there are matching records.
$numrows = count($results); // query worked, now, check if there are matching records.
if (is_int($numrows) && $numrows) {$var = true;} else {$var = false;}
return $var; // return the result.
}
Now, the function will return true only if there are one or more matching records.
Also, you have a syntax error here:
if(check_if_blocked(){
It should be:
if(check_if_blocked()){
After everyone's advice. I finally got it to work. Here is my code.
function check_if_blocked($blocked){
global $wpdb;
$user = '1';
$user2 = '2';
$results = $wpdb->get_results("SELECT * FROM mytable WHERE blocked = $user AND blocker = $user2");
if(empty($results)){
$blocked = 'not_blocked';
} else {
$blocked = 'is_blocked';
}
}
function msg_form(){
if(check_if_blocked($blocked) == 'not_blocked){
echo '<form action="" method="post">
<input type="text" name="msg">
<input type="submit" name="submit" value="send"></form>';
}
}

PHP Redirect Depending on users Column data in MySQL

I have created a login page with Facebook login API. And i have stored the users data (name, gender and etc) into MySQL database (except the column "gorg" in my table) when they are login.
Then, I'll redirect the users to "newgg.php" which is have two links "Giver" and
"Gatherer". So, users can choose either one of them.
My sample code:
<?php
session_start();
error_reporting(E_ALL);
include('src/sql_handler.php');
include('src/facebook_handler_core.php');
$new_fb = new facebook_handler_core;
$new_fb->run();
if (isset($_SESSION['gorg']) == "Gatherer") {
header('Location: map.php');
}
?>
My goal is to redirect them depending on the button they push for there FIRST time visiting the page, heres the button code
<form method="post" action="<?php echo $PHP_SELF;?>">
<input type="submit" class="button orange" name="Giver" value="Giver">
</form>
<form method="post" action="<?php echo $PHP_SELF;?>">
<input type="submit" class="button orange" name="Gatherer" value="Gatherer">
</form>
and now last but not least, IF they have already previously chosen their type of user it needs to just redirect them depending on what the 'gorg' column reads in the users table.
any ideas to why my codes not working properly?
just in case you need them, here are the sql_handlers
<?php
class MySQL_Con {
private $host = 'localhost',
$user = 'NUNURBSINESS',
$pass = 'ASKMEANDMAYBE',
$db = 'teknolog_fruitforest',
$_CON;
function MySQL_Con() {
$this->_CON = mysql_connect($this->host, $this->user, $this->pass);
if(!$this->_CON)
die(mysql_error());
else {
$select_db = mysql_select_db($this->db);
if(!$select_db)
die('Error Connecting To Database'.mysql_error());
}
}
function End_Con() {
mysql_close($this->_CON);
}
}
?>
and now the facebook_handler_core.php
<?php
class facebook_handler_core extends MySQL_Con {
public $session,$_INFO = array(),$U_INFO = array();
public function run() {
require('src/facebook.php');
$set_fb = new Facebook(array(
'appId' => 'MYAPPID',
'secret' => 'CANTTELLYOU',
'cookie' => true));
$this->session = $set_fb->getUser();
if($this->session != 0) {
$this->_INFO = $set_fb->api('/me');
if(!empty($this->_INFO))
$this->fb_session_handler();
}
}
function fb_session_handler() {
$SQL_CON = new MySQL_Con;
$SQL_CON->MySQL_Con();
$query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND email = '" .mysql_real_escape_string($this->_INFO['email'])."'") or die(mysql_error());
if(mysql_num_rows($query) > 0) {
$this->U_INFO = mysql_fetch_array($query) or die(mysql_error());
} else {
$photolink = 'http://graph.facebook.com/'.$this->session.'/picture?type=square';
$query = mysql_query("INSERT INTO users(oauth_uid, oauth_provider, username, first_name, last_name, email, pic_square, gorg, gender)VALUES('".mysql_real_escape_string($this->session)."','facebook', '".mysql_real_escape_string($this->_INFO['name'])."', '".mysql_real_escape_string($this->_INFO['first_name'])."','".mysql_real_escape_string($this->_INFO['last_name'])."','".mysql_real_escape_string($this->_INFO['email'])."','".mysql_real_escape_string($photolink)."','null','".mysql_real_escape_string($this->_INFO['gender'])."')") or die(mysql_error());
$query = mysql_query("SELECT * FROM users WHERE email='".mysql_real_escape_string($this->_INFO['email'])."'") or die(mysql_error());
$this->U_INFO = mysql_fetch_array($query) or die(mysql_error());
}
$SQL_CON->End_Con();
$gorg = $this->U_INFO['gorg'];
if($gorg != null) {
$_SESSION['gorg'] = $gorg;
}
$_SESSION['email'] = $this->U_INFO['email'];
$_SESSION['image'] = $this->U_INFO['pic_square'];
$_SESSION['gender'] = $this->U_INFO['gender'];
if($gorg != null) {
if($gorg == 'Giver') {
//redirect to Giver
header('Location: picktreetype.php');
}
if($gorg == "Gatherer") {
//redirect to Gatherer
}
}
return true;
}
function update_user($param) {
$SQL_CON = new MySQL_Con;
$SQL_CON->MySQL_Con();
if($param == 'Giver')
$query = mysql_query("UPDATE users SET gorg='".mysql_real_escape_string($param)."', FF_Points='100' WHERE email='".mysql_real_escape_string($_SESSION['email'])."'") or die(mysql_error());
if($param == 'Gatherer')
$query = mysql_query("UPDATE users SET gorg='".mysql_real_escape_string($param)."', FF_Points='30' WHERE email='".mysql_real_escape_string($_SESSION['email'])."'") or die(mysql_error());
$SQL_CON->End_Con();
if(!$query)
return false;
else
return true;
}
}
?>
Thanks in advance, i just cant get enough out of this site when it comes to gaining help and proper guidance i really appreciate all the help anyone has ever given me in the past.
The problem is you're doing
isset($_SESSION['gorg']) == "Gatherer"
as isset() returns a boolean result which, using ==, will match any non-explicitly-false value. You would have had direct evidence of the problem if you would have used === (identity comparison operator).
So, in your case, "Gatherer" is evaluated as non-FALSE, aka TRUE.
Every time.
You shouldn't use this kind of comparison; instead try:
isset($_SESSION['gorg']) && $_SESSION['gorg'] == "Gatherer"
if you wish to keep checking whether gorg is set before doing any other evaluation.

PHP to SQL on-change not updating

I have a fairly general problem. I have a small form
<form action="<?=base_url();?>ticket/close_ticket/<?=$ticket_details['id'];?>" method="post" id="close_ticket" name="close_ticket">
<ul>
<li><label for="frm_status">Status<span class="req">*</span></label>
<span class="input">
<select id="frm_status" name="status" onchange="this.form.submit()">
<option value="<? if ($ticket_details['status'] == "Open") $status= "1"; else $status= "2"; echo $status;?>"><? if ($ticket_details['status'] == "Open") $status= "Open"; else $status= "Closed"; echo $status;?></option>
<option value="<? if ($ticket_details['status'] == "Open") $status= "2"; else $status= "1"; echo $status;?>"><? if ($ticket_details['status'] == "Open") $status= "Closed"; else $status= "Open"; echo $status;?></option>
</select>
</span>
</li>
</ul>
</form>
This form contains the drop list option box, that on change submits the form to the close ticket controller......
public function close_ticket()
{
$this->load->model('ticket_model');
$ticket_id = mysql_real_escape_string($this->uri->segment(3));
if($_POST)
{
//save ticket
unset ($_POST['id']);
$_POST['id'] = $ticket_id;
$this->ticket_model->close_ticket($_POST);
redirect(base_url().'ticket/edit/'.$ticket_id.'/');
return;
}
redirect(base_url().'ticket/edit/'.$ticket_id.'/');
}
which it does. This controller is to post the information to the model to update the SQL.....
public function close_ticket($ticket_post)
{
$query = $this->db->query("SELECT id FROM ".$this->tables_ticket." WHERE id = '".mysql_real_escape_string($ticket_post['id'])."';");
if($query->num_rows() > 0)
{
$row = $query->row();
$query = $this->db->query("UPDATE ".$this->tables_ticket."
SET
status = '".mysql_real_escape_string($ticket_post['status'])."'
WHERE
id = '".mysql_real_escape_string($ticket_post['id'])."'
");
}
if($this->db->affected_rows() > 0) return true;
else return false;
}
then after all this, redirect back to the form. I am assuming that on redirect the form will then populate the drop list with the updated data. This is where I am struggling, as It sends the changed data, and somewhere it is not registering the change and returning the page, unchanged.
Question, would this work with a confirmation/secondary submission page, followed by redirect, and is it that I am trying to return the changed data in the same function where it is failing?
$body_data['ticket_list'] = $this->ticket_model->list_ticket();
$body_data['ticket_details'] = $this->ticket_model->get_ticket($ticket_id);
$body_data['ticket_summary'] = $this->ticket_model->list_ticket_summary($ticket_id);
$body_data['customer_list'] = $this->ticket_model->get_customer_details($ticket_id);
$body_data['precan_list'] = $this->ticket_model->list_messages();
$body_data['users_list'] = $this->ticket_model->list_users();
$foot_data['accordian_active'] = 5;
$this->load->view('head',$head_data);
$this->load->view('sidebar/service',$head_data);
$this->load->view('ticket/edit',$body_data);
$this->load->view('foot',$foot_data);
return;
The edit function simply returns a range of population query lists.
unless i need a new query to repopulate the ticket_details list?

sending a form to different pages depending on values

I was wandering how you would go about sending a form to different pages.
If there is an error in the form, stay on the page, showing an error and if there is no error go onto another page
here is my code
<form id = "form" action = "./?page=markandfeedback" method = "post">
<br>
Mark for:
<INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar" value="Enter Student Number" style="min-width:165px;">
<input type="button" value = 'Continue' onclick="validate()">
<?
$studID = $_POST['txtChar'];
$module2 = $_SESSION['module'];
$ex = $_POST['exer'];
$studerr = array();
$sql = 'SELECT * FROM `student`, `modules` WHERE `studentID` = '.$studID.' AND `moduleCode` = '.$_SESSION['module'];
$result = mysql_query ($sql);
echo $_SESSION['module'];
if(isset($studID)){
if($result == null){
$studerr[] = "No Student with that ";
print_r($studerr);
}
$_SESSION['student'] = $studID;
}
echo' <SCRIPT TYPE="text/javascript" LANGUAGE="javascript">
function validate() {
if('.$result.' == null)
{
return false
}
else
{
return true;
}
}
</script>';
?>
</form>
cheers
Very briefly (without knowing other details):
if (!empty($errorMessage)) {
echo $errorMessage;
} else {
header("Location: http://domain.com/success.php");
}
Have you searched something about that ???
YOu can use normal php validation for error checking and
if(there is no error )
{
header('location: redirect2page.php')
}
else
{
show errors
}
you have to do simple javascript validation in form submit if error occures it will return false so, thats remain on same page
like
function validate()
{
if(document.getElementById('elementid').value=='')
{
return false;
}
else
{
return true;
}
just call this function on submit button click

Categories