Undefined offset: 1 using mysql_real_escape_string($data[0]) - php

Good day again guys. APOSTROPHES gettin on my nerve. Please Help guys! I get this error when the RESPO column have an APOSTRPHE word on it "OFFICE'S".
Notice: Undefined offset: 1 in xxx:\xxxx\www\ptoms2\reports\view_reports.php on line 14
Notice: Undefined offset: 1 in xxx:\xxxx\www\ptoms2\reports\view_reports.php on line 15
My line 14 and 15 are this codes which this makes the error lines:
$month = date("m", strtotime($data[1]));
$year = date("Y", strtotime($data[1]));
To sum up all my view_reports.php are this following:
<?php
$respo = $_GET['respo'];
$data = explode("+", ($respo));
$month = date("m", strtotime($data[1]));
$year = date("Y", strtotime($data[1]));
$viewrecord = "SELECT dv.*, dv.respo, (pr.pr_gsis_c + pr.pr_gsis_l) AS deduction FROM tbl_dv dv LEFT OUTER JOIN tbl_payroll pr on dv.dv_id = pr.dv_id LEFT OUTER JOIN tbl_payroll tpr on tpr.dv_id = dv.dv_id WHERE dv.respo='".mysql_real_escape_string($data[0])."' && year(dv.date_added)=$year && month(dv.date_added)=$month ";
$run_viewrecord = mysql_query($viewrecord) or die(mysql_error());
{
etc....
THank you in advance. I gettin this error for a week and so.. I never imagine that just this APOSTROPHES making this error.. Please Help!
EDIT: This is the actual code I have.. I tried to inject mysql_real_escape_string maybe this is the solution of most APOSTROPHES error. But nothin is working..
This is a DROPDOWN Generated report. In the report.php I choose a RESPO and will gather all RESPO respectively upon the month I selected too.. BUT I have a RESPO that is PROV'L BUDGET OFFICE. WHich the PROV'L contains a APOSTROPHE.. WHEN I Generate the report according to this RESPON I got that error.. and the address is like this:
http://xxxxx/xxxxxx/reports/view_reports.php?respo=PROV
it should be like this sample without apostrophe:
http://xxxxx/xxxxx/reports/view_reports.php?respo=BIPC%2B2014-02-04+10%3A53%3A04

You need to use urlencode / decode on the $_GET value passed so it will not truncate the information after the apostrophe. That will resolve your problem.
You would use decode on the code posted above like so:
$respo = urldecode($_GET['respo']);
The code you are preparing this statement from would be:
$variable = urlencode($row['somethinghere']
Now if it contains an apostrophe it would still send the entire string of data just fine.

Related

Warning: Trying to access array offset on value of type int in

This code is giving error. I have gone through previous questions. I have used gettype() and it shows that my data is integer. I have tried to convert it to string by using strval() but it didnt work.
I saw this but in dont know how this can be implemented with my code.
sugested similar questions talk about undefined variables.
My problem not undefine but that its value type. variables are defined. It is stored in db as string but when i check its variable type using gettype(), it is an integer. and it seems foreach() doesnt work with integer.
If anyone can help please. i am still in the learning stage.
foreach($schedule_data as $schedule_row)
{
$end_time = strtotime($schedule_row["agent_schedule_end_time"] . ':00');
$start_time = strtotime($schedule_row["agent_schedule_start_time"] . ':00');
$total_agent_available_minute = ($end_time - $start_time) / 60;
$average_inspection_time = $schedule_data["average_inspection_time"];
}
i wrote query
SELECT * FROM agent_schedule_table
WHERE agent_schedule_id = '$agent_schedule_id'
data stored in $schedule_data var_dump ()
array(8) {
["agent_schedule_id"]=> int(18)
["agent_id"]=> string(7) "S1O0NWE"
["agent_schedule_date"]=> string(10) "2022-08-18"
["agent_schedule_day"]=> string(8) "Thursday"
["agent_schedule_start_time"]=> string(5) "08:35"
["agent_schedule_end_time"]=> string(5) "22:35"
["average_inspection_time"]=> int(60)
["agent_schedule_status"]=> string(6) "Active"
}
foreach()
var_dump($end_time); = int(1660595700)
//Agent create appointment for himself
Agent add through a form that has Date: ..., starttime: ...., averagetime: ...., endtime:.....
i wrote query to select from appointemnt the agent appointment time.
i used while() for the stmt-> excute
Problem 1: while loop make it repeat same date and time so many times.
I tried using foreach() or for(), it is not working. I dont know if am using it wrongly or just that it wont work.
Problem 2: what i want to achieve is that the table should;
for every date, role1 start time, role2 start time + average time, role 3 role2time + average time. so the output will be like
peter 22/08 4:00
peter 22/08 4:30,
peter 22/08 5:00 etc
$sql = "SELECT * FROM agent_schedule_table
INNER JOIN agent
ON agent.agent_id = agent_schedule_table.agent_id
INNER JOIN property
ON property.agent_id = agent.agent_id
INNER JOIN rent_interest
ON rent_interest.Property_number = property.Property_number
WHERE (agent_schedule_table.agent_schedule_date >= '$todaysdate' AND property.Property_number = '".$_SESSION["apartment_number"]."')
ORDER BY agent_schedule_table.agent_schedule_date ASC";
$stmt = $pdo->prepare($sql);
$stmt ->execute(array());
$rows = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
$result = $row;
}
I don't think you need a loop. $schedule_data is an associative array, just access the element from there, not from a nested element.
$end_time = strtotime($schedule_data["agent_schedule_end_time"] . ':00');
$start_time = strtotime($schedule_data["agent_schedule_start_time"] . ':00');
$total_agent_available_minute = ($end_time - $start_time) / 60;
$average_inspection_time = $schedule_data["average_inspection_time"];

How to use the result of mysql query as the column name of another query

i have a following code
<?php
include("includes\conn.php");
$start = $_POST['start_date'];
$end = $_POST['end_date'];
$result = mysqli_query($con,"SELECT * FROM data");
while($row = mysqli_fetch_assoc($result)){
$time_stamp = $row['time_stamp'];
$temp_date= substr($time_stamp, 2, 12);
$date= explode('.', $temp_date);
$final= $date[2]. '-' .$date[1]. '-' .$date[0];
$result1 = mysqli_query($con,"SELECT * FROM data WHERE $final BETWEEN $start AND $end");
while($row2 = mysqli_fetch_assoc($result1)){
print_r($row2);
}
}
?>
Error:
Notice: Undefined offset: 2 in C:\xampp\htdocs\Nestle\trend_test.php on line 10
Notice: Undefined offset: 1 in C:\xampp\htdocs\Nestle\trend_test.php on line 10
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\Nestle\trend_test.php on line 12
Your kind help is needed!
Your date extraction from time_stamp might be a problem.
$final= substr($time_stamp, 0, 10)
would be better.
If needed include Quotation marks at start and end of date items within query.
Add another echo for $final before calling MySQL and you can understand better and correct yourself.
Lets assume that this is the data of the date in your db '09.12.2019 12:54:50' so in order to get the format yyyy-mm-dd, hence your date format has date and time, first is you remove the time:
$temp_date= substr('09.12.2019 12:54:50', 0, 10);
After getting the date, we will be making it to this format yyyy-mm-dd
$date= explode('.', $temp_date);
$final= $date[2]. '-' .$date[1]. '-' .$date[0];
As a result, $final has a value 2019-12-09 [yyyy-mm-dd] and you can now use it in the next query.
Since your date values in db uses '.' (09.12.2019 12:54:50) instead of
'-' (09-12-2019 12:54:50), we made an approachable solution in it.

Notice: Undefined Offset php

I have this php code which throws the alert notice: undefined offset
$thisMonth=$_POST['month']; //the value is today's date's month
$thisYear=$_POST['year']; //the value is today's date's year
$thisDay=$_POST['day']; //the value is today's date's day
$table=mysql_query("SELECT * FROM `kids` WHERE `debt`!='0'") or die(mysql_error());
$debt=0;
while($row=mysql_fetch_assoc($table)){
$explodedDate=explode('/',$row['enrollmentdate']);
$theYear=$explodedDate[0];
$theMonth=$explodedDate[1]; //this line throws the error
$theDay=$explodedDate[2]; //and also this line
if((int)$theYear==(int)$thisYear && (int)$theMonth==(int)$thisMonth){
if((int)$theDay==(int)$thisDay || (int)$thisDay==0){
$debt+=$row['debt'];
}
}
}
I have been reading all over the internet for a solution but seems like this error is dependent on the code and unfortunately I can't seem to figure out how to fix it.
any ideas how to fix the error or what's causing it?
this is the full error:
Notice: Undefined offset: 1 in C:\wamp\www\kids_house\php\functions.php on line 600
Notice: Undefined offset: 2 in C:\wamp\www\kids_house\php\functions.php on line 601
The reason that these two lines are throwing errors is because the value in this is not yyyy/mm/dd, as you expect:
$explodedDate=explode('/',$row['enrollmentdate']);
If you look at the value that throws the error with something like this, you should see the issue:
$thisMonth=$_POST['month']; //the value is today's date's month
$thisYear=$_POST['year']; //the value is today's date's year
$thisDay=$_POST['day']; //the value is today's date's day
$table=mysql_query("SELECT * FROM `kids` WHERE `debt`!='0'") or die(mysql_error());
$debt=0;
while($row=mysql_fetch_assoc($table)){
$explodedDate=explode('/',$row['enrollmentdate']);
if ( count( $explodedDate ) <= 1 ) {
var_dump( $row ); //this will show you the row that is causing the notice
var_dump( $explodedDate ); //this will show you the date
die();
}
$theYear=$explodedDate[0];
$theMonth=$explodedDate[1]; //this line throws the error
$theDay=$explodedDate[2]; //and also this line
if((int)$theYear==(int)$thisYear && (int)$theMonth==(int)$thisMonth){
if((int)$theDay==(int)$thisDay || (int)$thisDay==0){
$debt+=$row['debt'];
}
}
}
If you want to retry with commas for lines with an enrollment date formatted like yyyy,mm,dd you could do this. It's not the most elegant solution, but it sounds like you have dirty data so may have to.
$thisMonth=$_POST['month']; //the value is today's date's month
$thisYear=$_POST['year']; //the value is today's date's year
$thisDay=$_POST['day']; //the value is today's date's day
$table=mysql_query("SELECT * FROM `kids` WHERE `debt`!='0'") or die(mysql_error());
$debt=0;
while($row=mysql_fetch_assoc($table)){
$explodedDate=explode('/',$row['enrollmentdate']);
//try again with commas
if ( count( $explodedDate ) == 0 ) {
$explodedDate=explode(',',$row['enrollmentdate']);
}
//skip the record if still no enrollment date
if ( count( $explodedDate ) == 3 ) {
$theYear=$explodedDate[0];
$theMonth=$explodedDate[1]; //this line throws the error
$theDay=$explodedDate[2]; //and also this line
if((int)$theYear==(int)$thisYear && (int)$theMonth==(int)$thisMonth){
if((int)$theDay==(int)$thisDay || (int)$thisDay==0){
$debt+=$row['debt'];
}
}
}
}

Evaluating php code stored in database

I have several strings stored in a database (yes, I know it's not the best practice, but I'm working with a system someone else designed and I cannot change it.)
For example, in my table called specialDates, I have a computedDate column that contains this:
date('d', strtotime("thursday, november ".date("Y")." + 3 weeks"))
This is pulled into my code and stored in a variable called $request['order_date'].
This, when used in standard PHP code, returns the date that Thanksgiving falls on in the current year. I need to pass the evaluated value of this (for this year - 2013 - the value is 28) to another query. But I can't seem to get the value to evaluate.
$query = 'SELECT count(orderID) as numberOfOrders FROM customer_orders WHERE customerID = ? AND orderDate = ?';
$param = array($request['customer_id'], eval($request['order_date']));
doesn't work. And when I just try to echo out the value to the screen, doing this:
echo eval($request['order_date']);
or this:
echo eval(" return \$request['order_date']; ");
just prints out the literal value of the string (i.e. date('d' etc.) rather than the expected evaluated value.
What am I doing wrong?
UPDATED
I tried AScherer's answer below, and although it does work when I just echo the value to the screen:
echo eval(" return {$request['order_date']}; ");
it does not work when I try to use the eval inside my array:
$param = array($request['customer_id'], eval("return {$request['order_date']}"));
How can I get this working so that the value I'm passing in the array is the actual value of 28, rather than the string pre-eval?
You were escaping the $request in your return part, which you dont want to do. This method should work, but id suggest adding return the the row in the database so u dont have to modify the eval
$sql = "SELECT customer_id, CONCAT( 'return ', order_date, ';' ) as function from customer_orders WHERE customerID = ? AND orderDate = ?";
$param = array( $request[ 'customer_id' ], eval( $request[ 'function '] ) );
---
OR
---
$evalData = eval(" return {$request['order_date']}; ");
$param = array($request['customer_id'], $evalData );
Yes, you can change it. If not to a sensible result, then to a useful result nontheless.
Your input data is a string:
date('d', strtotime("thursday, november ".date("Y")." + 3 weeks"))
This is an information to "calculate Thanksgiving of this year". You can set up a lookup table that detects this string and the needed function to execute:
$dateCalculations = array(
'date(\'d\', strtotime("thursday, november ".date("Y")." + 3 weeks"))' => function()
{
return date('d', strtotime("thursday, november ".date("Y")." + 3 weeks"));
}
'date(\'...whatever else is in the system...' => function(){}
// alias names also work
'thanksgiving' => function()
{
return date('d', strtotime("thursday, november ".date("Y")." + 3 weeks"));
}
);
You can then try to find the string from the database in the array and instantly have the function you need to call:
$thanksgiving = $dateCalculations[$dbvalue]();
From here you can go anywhere. Note that this will also work with the alias name "thanksgiving", so you might be able to replace it in the database if you know where else this braindead approach is also used... If not, at least you tried your best.

Parsing date in a Facebook application using PHP

i'm trying a simple facebook application, running on localhost using wamp 2.0 , i got the date using the following code as shown here, when i run echo($bd); i get the correct result but when the following code runs i get an error.
Code
$bd = $fbme['birthday'];
$datearr = "0";$month = "0"; $date = "0"; $year = "0";
$datearr = explode('-', $bd);
list($month,$date,$year) = $datearr;
echo($month);
Error :
Notice: Undefined offset: 2 in C:\wamp......\index.php on line 30
Notice: Undefined offset: 1 in C:\wamp.....\index.php on line 30
Could you please suggest the reason and why this is occuring, any way how to get rid of this. Thanks a lot!!
The string stored in $fbme['birthday'] does not contain any '-' chars. Most likely it is empty. Check where $fbme is populated and an actual birthday is present.

Categories