how do i define an isset($_GET['id']) statement in an $_POST update query - isset

I am carrying 'ID' over from a display table and want to store it in a variable called employee_display_id. I am aware that if you do not create the below function...
if(sset($_GET['id'])){
$employee_display_id = $_GET['iod'];
}
and your proceed to use the '$employee_display_id' variable, you receive an undefined variable error.
How do you incorporate the...
if(sset($_GET['id'])){
$employee_display_id = $_GET['iod'];
}
into...
if(isset($_POST['update']))
{
$updatename = htmlentities(strip_tags(mysql_real_escape_string($_POST['update_name'])));
$updateusername = htmlentities(strip_tags(mysql_real_escape_string($_POST['update_username'])));
echo $update_query = "UPDATE `employees` SET `name`='$updatename', `username`='$updateusername' WHERE `employee_id`='4'";/* $employee_display_id */
$update_result = mysql_query($update_query);
}
else
{
echo mysql_error();
}
so i can use the variable in the WHERE clause of the UPDATE query??

You are checking isset for $_GET['id'] and assigning $_GET['iod']
itz "iod" yu are assigning. Are you sure this variable assigned a value ???

Nazneen has a point. Double-check your variable names.
Also, do you know the difference between GET and POST? How they work?
Presumably, you want to update the employee data from an update link in a table? So your link would be something like http://example.com/update.php?id=6
Well then, in this example, GET['id'] would return 6, which you can then use with your code and pass it into your SQL query.
Example:
if(isset($_POST['update']))
{
if(isset($_GET['id'])){
$employee_display_id = $_GET['id'];
}
if(isset($_POST['update_name'])){
$updatename = htmlentities(strip_tags(mysql_real_escape_string($_POST['update_name'])));
}
// and so on...
}
Does that answer your question?

You could use a shorthand if to determine if the variable is set within the update command, but you need to set it to something or the query will not operate as you expect.
I would recommend checking if the variable is set prior to the SQL. If it's not set don't perform the query.
if(isset($employee_display_id)){
echo $update_query = "UPDATE `employees` SET `name`='$updatename', `username`='$updateusername' WHERE `employee_id`=" . $employee_display_id;
$update_result = mysql_query($update_query);
} else {
echo "ID not set."
}

Related

Update scalar variable globally inside if statement in Perl?

I'm trying to update a variable declared and used outside of a series of if / elsif / else statements, and updating it within the series. Is there a functional way to make this sorting/updating variable work?
my $daterange = 'initial';
if ($in{from_date} & $in{to_date}) {
my $daterange=~"AND (date BETWEEN '$fromdate' and '$todate')";
}
elsif ($in{from_date}) {
my $daterange=~"AND (date > '$fromdate')";
}
elsif ($in{to_date}) {
my $daterange=~"AND (date < '$todate')";
}
else {
my $daterange=~"blank";
}
print $daterange;
my creates a new variable. That means that inside your "then` clauses, you create a new variable, then assign a string to it. You never use that variable again!
You want to assign to the existing variable, so stop creating new ones with the same name.
There are numerous other problems with your code:
Your code suffer from SQL injection bugs.
You used =~ to assign to a variable when the assignment operator is =.
You used & to check if two value are true when the logical-AND operator is &&.
You used "blank" to create an empty string when it doesn't.
You sometimes use $in{from_date} and sometimes $fromdate.
You sometimes use $in{to_date} and sometimes $todate.
You assign an initial value (initial) to $daterange that you never end up using.
You have some useless parentheses in your SQL.
Fixed:
my $daterange;
if ($in{from_date} && $in{to_date}) {
$daterange = " AND (date BETWEEN ".$dbh->quote($in{from_date})." AND ".$dbh->quote($in{to_date}).")";
} elsif ($in{from_date}) {
$daterange = " AND date > ".$dbh->quote($in{from_date});
} elsif ($in{to_date}) {
$daterange = " AND date < ".$dbh->quote($in{to_date});
} else {
$daterange = "";
}
Delete all my except the first one.
This prevents one local variable being introduced for each {}, which is then unknown outside.
That way, the accesses inside the {} will affect your global variable.
As c3st7n noted (thanks), you will also have to doublecheck what you actually do to your variable, it is likely that it is not what you intended. I.e. you probably want to use = instead of =~.

How to perform logic if findBy’Field’ does match?

I trying to do some logic if my inputed form email matches one found in the database.
How do I make the comparison if the findBy’Field” returns the whole collection instead of just the field I want to compare to? I'd think it should be done without using a foreach loop as that would kinda defeat the purpose of using my findBy method.
An Example:
$formEmail = $form->get('email')->getData();
existingEmail = $em->getRepository(‘UserBundle:User’)->findOneByEmail($formEmail);
// or existingEmail = $em->getRepository(‘UserBundle:User’)->findByEmail($formEmail);
if ($formEmail == $loggedEmail){
//perform some logic here
}
try this
$formEmail = $form->get('email')->getData();
$existingEmail = $em->getRepository('UserBundle:User')->findOneByEmail($formEmail);
if ($existingEmail){
//found
}else{
//not found
}

What does this string of code: gr.sys_id[key] = current.getValue(glideElement.getName());

I'm trying to copy (duplicate) a record in ServiceNow table of incidents, but can not make this string work: gr.sys_id[key] = current.getValue(glideElement.getName());
The goal is to copy all fields values except sys_id.
Take a look at the UI Action Insert & Stay which is kind of a Duplicate Script.
You can use the same functionality in your Business rule or any other server side script:
doInsertAndStay();
function doInsertAndStay() {
var saveMe = current;
if (typeof current.number != 'undefined' && current.number){
current.number = ""; // generate a new number
}
current.insert();
action.setRedirectURL(saveMe);
}
The GlideRecord function insert() duplicates a record and of course a new sys_id is used for the new record. As far as I know you are not able to define the sys_id by your self.

It is possible to execute MySQLi prepared statement before the other one is closed?

Lets say I have a prepared statement. The query that it prepares doesn't matter. I fetch the result like above (I can't show actual code, as it is something I don't want to show off. Please concentrate on the problem, not the examples meaningless) and I get
Fatal error: Call to a member function bind_param() on a non-object in... error. The error caused in the called object.
<?php
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
class table2Info{
private $mysqli;
public function __construct($_mysqli){
$this->mysqli = $_mysqli;
}
public function getInfo($id)
{
$db = $this->mysqli->prepare('SELECT info FROM table2 WHERE id = ? LIMIT 1');
$db->bind_param('i',$db_id);
$db_id = $id;
$db->bind_result($info);
$db->execute();
$db->fetch();
$db->close();
return $info;
}
}
$t2I = new table2Info($mysqli);
$stmt->prepare('SELECT id FROM table1 WHERE name = ?');
$stmt->bind_param('s',$name);
$name = $_GET['name'];
$stmt->bind_result($id);
$stmt->execute();
while($stmt->fetch())
{
//This will cause the fatal-error
echo $t2I->getInfo($id);
}
$stmt->close();
?>
The question is: is there a way to do another prepared statement while another one is still open? It would simplify the code for me. I can't solve this with SQL JOIN or something like that, it must be this way. Now I collect the fetched data in an array and loop through it after $stmt->close(); but that just isn't good solution. Why should I do two loops when one is better?
From the error you're getting it appears that your statement preparation failed. mysqli::prepare returns a MySQLi_STMT object or false on failure.
Check for the return value from your statement preparation that is causing the error. If it is false you can see more details by looking at mysqli::error.

Help needed formatting Doctrine Query in Zend Framework

Can anyone tell me how to format the query below correctly in my controller.
Currently it gives me nothing in my FilteringSelect. However if I change it to >= I get back all the kennelIDs which is incorrect also but at least I'm getting something.
I've tested that the session variable is set and can confirm that there are kennels with the matching capacity.
// Create autocomplete selection for the service of this booking
public function servkennelAction()
{
$sessionKennelBooking = new Zend_Session_Namespace('sessionKennelBooking');
// disable layout and view rendering
$this->_helper->layout->disableLayout();
$this->getHelper('viewRenderer')->setNoRender(true);
// get list of grooming services for dogs from the table
$qry= Doctrine_Query::create()
->from('PetManager_Model_Kennels k');
//This should be set by default and narrows down the search criteria
if(isset($sessionKennelBooking->numPets)){
$b=(int)$sessionKennelBooking->numPets;
$qry->addWhere('k.capacity = ?','$b');
}
$result=$qry->fetchArray();
//generate and return JSON string using the primary key of the table
$data = new Zend_Dojo_Data('kennelID',$result);
echo $data->toJson();
}
Many thanks in Advance.
Graham
I think that addWhere condition is wrong. It has to be:
$qry->addWhere('k.capacity = ?', $b);
i.e. $b without quotes.