Notice: Undefined index: user_id [duplicate] - php

This question already has answers here:
php $_GET and undefined index
(12 answers)
Closed 1 year ago.
someone can help me. im geting error with code
Notice: Undefined index: user_id in D:\xampp\htdocs\public\s_game\login\gamelogin.php on line 14
Notice: Undefined index: user_pass in D:\xampp\htdocs\public\s_game\login\gamelogin.php on line 15
and here code
<?php
require '../config.php';
function antiinjection($str) {
$banwords = array ("'", ",", ";", "--");
if ( preg_match( "/[a-zA-Z0-9]+/", $str ) ) {
$str = str_replace ( $banwords, '', $str );
} else {
$str = NULL;
}
return $str;
}
$user_id = antiinjection($_GET['user_id']);
$user_pass = antiinjection($_GET['user_pass']);
$connInfo = array(
'Database' => $db,
'UID' => $user,
'PWD' => $pass
);
$conn = sqlsrv_connect($host, $connInfo);
$sql = "SELECT * FROM Account WITH (NOLOCK) WHERE UserId = ? AND Activated = 1";
$res = sqlsrv_query($conn, $sql, array($user_id), array( "Scrollable" => 'static' ));
if (sqlsrv_num_rows($res)>0)
{
$sql = "SELECT isUserCreated,Password,ID,UserKey,Email,BlockedEndDate FROM Account WITH (NOLOCK) WHERE UserId = ?";
$r2 = sqlsrv_query($conn, $sql, array($user_id), array( "Scrollable" => 'static' ));
sqlsrv_fetch( $r2 );
$userPass = sqlsrv_get_field($r2, 1);
$ID = sqlsrv_get_field($r2,2);
$userKey = sqlsrv_get_field($r2,3);
$email = sqlsrv_get_field($r2,4);
$sql = "SELECT DATEDIFF(day, getdate(), BlockedEndDate) FROM Account WITH (NOLOCK) WHERE UserId = ?";
$r3 = sqlsrv_query($conn, $sql, array($user_id), array( "Scrollable" => 'static' ));
sqlsrv_fetch( $r3 );
if (sqlsrv_get_field($r3,0) >= 0)
{
$result = -100;
}
else
{
$sql = "UPDATE Account SET Blocked = 0 WHERE UserId = '$user_id'";
sqlsrv_query($conn, $sql, array($user_id), array( "Scrollable" => 'static' ));
$result = 0;
}
$result = sqlsrv_get_field ($r2,0);
sqlsrv_close($conn);
}
else
{
$result = -99;
}
if (($result == -100) || ($result == -99))
{
echo '2';
die();
}
?>

That's because you didn't pass user_id and user_pass in your GET request. GET request has the parameters in a URL, so you are maybe passing them with POST request.

Related

Trying to access array offset on value of type bool in php [duplicate]

This question already has an answer here:
Trying to access array offset on value of type bool
(1 answer)
Closed 1 year ago.
function Checkactive($active){
$db = new Connect;
$result = '';
if(isset($active)){
if(empty($active)){
}else{
$active = stripslashes(htmlspecialchars($active));
$user = $db -> prepare("SELECT * FROM users WHERE active = :active");
$user->execute(array(
'active' => $active
));
$info = $user->fetch(PDO::FETCH_ASSOC);
if($info['active'] != 'active'){ //line 128
$hash = $this->generateCode(10);
$upd = $db->prepare("UPDATE users SET active=:hash WHERE id=:ex_user");
$upd -> execute(array(
'hash' => $hash,
'ex_user' => $info['id'] //line 133
));
echo "sucess";
}else {
echo "failer";
}
}
}
return $result;
}
I tried to solve it but I can't.
Notice: Trying to access array offset on value of type bool in C:\xampp\htdocs\admin\core\classes\user.Class.php on line 128
Notice: Trying to access array offset on value of type bool in C:\xampp\htdocs\admin\core\classes\user.Class.php on line 133
function Checkactive($active){
$db = new Connect;
echo "first";
$result = '';
if(isset($active)){
if(empty($active)){
}else{
echo $active;
$active = stripslashes(htmlspecialchars($active));
$user = $db -> prepare("SELECT * FROM users WHERE actived = :active");
$user->execute(array('active' => $active)); //remove array('active' => $active).....then it will work normally
$info = $user->fetch(PDO::FETCH_ASSOC);
var_dump($info); // still return bool(false)
// if( $info['actived'] == 'active'){
// $hash = $active;
// $upd = $db->prepare("UPDATE users SET actived = :hash WHERE id = :ex_user");
// $upd -> execute(array(
// 'hash' => $hash,
// 'ex_user' => $info["id"]
// ));
// echo "sucess";
// }else {
// $hash = "two";
// var_dump($active);
// $upd = $db->prepare("UPDATE users SET actived = :hash WHERE id = :ex_user");
// $upd -> execute(array(
// 'hash' => $hash,
// 'ex_user' => $info["id"]
// ));
// var_dump($info);
// var_dump($active);
// echo "failer";
// }
}
}
remove array('active' => $active).....then it will work normally
You have error in $user->fetch(PDO::FETCH_ASSOC), so it return FALSE, not array.
After, you are using $info['active'] and $info['id'] as an array, without result validation.

Joomfish migration script returns error

Here is a script that upgrades joomfish (joomla translation component) from joomla 1.5 to 2.5:
$db = new PDO("mysql:host=localhost;dbname=db;charset=UTF8", "root", "pass");
$stmt = $db->prepare("select distinct(jfc.reference_id),c.catid,jfc.language_id,c.modified,c.modified_by,c.version,c.modified_by ,c.ordering,c.created_by,c.metadesc ,c.created_by_alias from jos_jf_content jfc ,jos_content c where jfc.reference_id = c.id and jfc.reference_table = 'content' ");
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $row) {
$count_row = $db->prepare("select * from jos_jf_content where reference_id = ? and language_id = ?");
$count_row->bindValue(1, $row['reference_id']);
$count_row->bindValue(2, $row['language_id']);
$lang_code = $db->prepare("select lang_code from j25_languages where lang_id = ?");
$lang_code->bindValue(1, $row['language_id']);
$lang_code->execute();
$l_code = $lang_code->fetch(PDO::FETCH_OBJ);
$language_code = $l_code->lang_code;
$count_row->execute();
$title ="";
$fulltext ="";
$introtext ="";
$alias ="";
$published ="";
while($col = $count_row->fetch(PDO :: FETCH_ASSOC))
{
if($col['reference_field'] == "title")
{
$title = $col['value'];
}
if($col['reference_field'] == "fulltext")
{
$fulltext = $col['value'];
}
if($col['reference_field'] == "introtext")
{
$introtext = $col['value'];
}
if($col['reference_field'] == "alias")
{
$alias = $col['value'];
}
$published = $col['published'];
}
$exe = $db->prepare("insert into j25_content (`title`,`alias`,`introtext`,`fulltext`,`published`,`catid`,`created`,`created_by`,`created_by_alias`,`modified`,`modified_by`,`version`,`ordering`,`metadesc`,`language`) values(:title,:alias,:introtext,:fulltext,:published,:categoryid,:created,:created_by,:created_by_alias,:modified,:modified_by,:version,:ordering,:metadesc,:language_code)");
$exe->execute(array(':title' => $title,':alias' => $alias,':introtext' => addslashes($introtext),':fulltext' => addslashes($fulltext),':published' => ".$published.",':categoryid' => $row['catid'],':created' => date("Y-m-d H:i:s"),':created_by' => $row['created_by'],':created_by_alias' => "".$row['created_by_alias']."",':modified' => date("Y-m-d H:i:s"),':modified_by' =>$row['modified_by'],':version' => $row['version'],':ordering' => $row['ordering'],':metadesc' => $row['metadesc'],':language_code' => $language_code));
$i = $db->lastInsertId('id');
$asst = $db->prepare("select asset_id from j25_categories where id = ? ");
$asst->bindValue(1, $row['catid']);
$asst->execute();
$asst_id = $asst->fetch(PDO::FETCH_OBJ);
$cassetid = $asst_id->asset_id;
$sel = $db->prepare("select lft,rgt FROM `j25_assets` where id = (SELECT max(id) FROM `j25_assets`)");
$sel->execute();
$select = $sel->fetch(PDO::FETCH_OBJ);
$left = $select->lft;
$right = $select->rgt;
$left=$left+1;
$right = $right+1;
$stmt = $db->prepare("insert into j25_assets (`parent_id`,`lft`,`rgt`,`level`,`name`,`title`) values(:cassetid,:left,:right,:level,:name,:title)");
$stmt->execute(array(':cassetid' => $cassetid,':left' => $left,':right' => $right,':level' => 4,':name' => "com_content.article.".$i,':title' => $title));
$insertedId = $db->lastInsertId('id');
$update = $db->prepare("update j25_content set asset_id = ? where id = ?");
$update->bindValue(1, $insertedId);
$update->bindValue(2, $i);
$update->execute();
$stmt = $db->prepare("insert into j25_jf_translationmap (language,reference_id,translation_id,reference_table) values (:language_code,:reference_id,:translation_id,:content)");
$stmt->execute(array(':language_code' => $language_code,':reference_id' => $row['reference_id'],':translation_id' => $i,':content' => 'content'));
}
Line of code:
$language_code = $l_code->lang_code;
Returns:
Trying to get property of non-object
I'm not an author of the script and not good in PHP, but I've tried to print_r($l_code->lang_code); and I got expected result en-GB from [lang_code] => en-GB. What I need to change in this code? Thanks.
The line $language_code = $l_code->lang_code > 0; sets $language_code to boolean value. Try var_dump($l_code); and var_dump($language_code); to debug your results. You should also check if $l_code actually is an object, or perhaps null was returned. Hope that helps.

Cannot Select table where Timestamp = GET

I am so confused here, how can i select table where schedule_date_time = $_GET['date_time'];
<?php
require 'core/init.php';
$schedule_id = $_GET['id'];
$schedule_date_time = $_GET['date_time'];
$session_id = $_GET['session'];
$user_id = $_GET['user'];
$user_email = $_GET['email'];
$user_name = $_GET['username'];
$registry_id = $_GET['registry'];
$check1 = DB::getInstance()->query("SELECT * FROM chat_schedule WHERE schedule_date_time = $schedule_date_time");
if ($check1->count()) {
$insert = DB::getInstance()->insert('chat_booked', array(
'schedule_id' => $schedule_id,
'schedule_date_time' => $schedule_date_time,
'session_id' => $session_id,
'user_id' => $user_id,
'user_email' => $user_email,
'user_name' => $user_name,
'registry_id' => $registry_id
));
if ($insert = true) {
echo "Insert was Successful";
}
} else {
echo "No Match Found, INSERT FAILED!";
}
Do i need to parse the date or something. I can not figure this out :(
change the $schedule_date_time to '$schedule_date_time' like:
"SELECT * FROM chat_schedule WHERE schedule_date_time = '$schedule_date_time' "

NOTICE: Undefined index: Page

I dont understand what im doing wrong, im getting this message:
Notice: Undefined index: page in /Users/prinect/htdocs/sistema-noticias/classes/Article.php on line 221
My code:
public static function getList( $numRows=30, $order="publicationDate DESC" ) {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$offset = ($_GET['page'] - 1) * $numRows;
$rowCount = 20;
$sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles
ORDER BY {$order} LIMIT :offset, :rowCount";
$st = $conn->prepare( $sql );
$st->bindValue( ":rowCount", $rowCount, PDO::PARAM_INT );
$st->bindValue( ":offset", $offset, PDO::PARAM_INT );
$st->execute();
$list = array();
while ( $row = $st->fetch() ) {
$article = new Article( $row );
$list[] = $article;
}
// Now get the total number of articles that matched the criteria
$sql = "SELECT FOUND_ROWS() AS totalRows";
$totalRows = $conn->query( $sql )->fetch();
$conn = null;
return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );
}
Undefined index means in your case that you are trying to access to an element of an array that does not exist.
It is most likely your $_GET['page']. Do you check if this exists? You should.
For example:
$page = (isset($_GET['page']) ? $_GET['page'] : 1; // 1 is the default page number
you need to check the page if it don't exist because when the page is not in the query param it will be considered as undefined
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$offset = ($page - 1) * $numRows;
$rowCount = 20;

Can i create array by use php variable in a loop?

Can I create an array by use php variable in a loop ?
<?php
for($i=5;$i<=100;$i++)
{
$sql = "SELECT * FROM users WHERE id = '$i' order by id asc";
$result = mysql_query($sql);
$datas=mysql_fetch_array($result);{
$username = stripslashes(str_replace('\r\n', '<br>',($datas['username'])));
}
${'name' . $i} = $username;
}
?>
Okay , Now I have php variable $name1-$name100
And then I want to create array by use php variable $name1-$name100
$my_array = array(php variable $name1-$name100);
How can I do ?
Just modify how you store the username:
$my_array = array();
for ( $i = 5; $i <= 100; $i++ ) {
$sql = "SELECT * FROM users WHERE id = '$i' order by id asc";
$result = mysql_query($sql);
$datas = mysql_fetch_array($result);
$username = stripslashes(str_replace('\r\n', '<br>',($datas['username'])));
$my_array[] = $username; // push usernames into array
}
Also you shouldn't run so many queries, so I suggest something like this:
$usernames = array();
$query = "SELECT * FROM users WHERE id >= 5 AND id <= 100 ORDER BY id ASC";
$result = mysql_query( $query );
if ( mysql_num_rows( $result ) > 0 ) {
while ( $data = mysql_fetch_assoc( $result ) )
$usernames[] = stripslashes( str_replace( '\r\n', '<br>', $data['username'] ) );
}

Categories