I am trying to pass a session variable from one page to another but it seems liek the session variable is empty on the next page. Thanks for the help!
Here is the entirety of the code on the two pages.
mysql_connect($db_hostname, $db_username, $db_password);
mysql_select_db($db_sitename);
$pagename= $row_getDisplay['USER_NAME'];
$query=mysql_query("SELECT * FROM COMMENTS WHERE COMMENT_ON='$pagename' LIMIT 0, $no_of_comments");
echo "<hr />";
echo "<h3>Latest Comments</h3>";
session_start();
$count = 0;
while($fetch=mysql_fetch_array($query))
{
echo "<p>".$fetch['COMMENT']."X<br/><sub><b>Comment by: </b>".$fetch['COMMENT_BY']."</sub><hr /><p>";
$_SESSION[strval($count)]=$fetch['COMMENT'];
//echo $_SESSION[strval($count)];
$count = $count + 1;
print_r($_SESSION);
}
mysql_close();
delete_comment.php
<?php
session_start();
print_r($_SESSION);
error_reporting(E_ALL);
ini_set('display_errors', '1');
$count = $_GET['count'];
echo $count;
$comment = $_SESSION[strval($count)];
$db_sitename="genydb";
$db_hostname="localhost";
$db_username=
$db_password=
$con = mysqli_connect($db_hostname, $db_username, $db_password, $db_sitename);
echo $comment;
$myquery = sprintf("DELETE FROM COMMENTS WHERE COMMENT=%s", GetSQLValueString($_SESSION[$_GET['count']], 'text'));
$mysql=($myquery) or die(mysql_error());
mysqli_close($con);
?>
This is the error
Notice: Undefined offset: 0 in /var/www/geny/bootstrap/delete_comment.php on line 8
$comment = $_SESSION[strval($count)];
I believe it is because you are using a numeric key with $_SESSION. I tried it out locally and I got this notice on the first page:
Notice: Unknown: Skipping numeric key 0 in Unknown on line 0
I saw this bug report as well after getting that notice https://bugs.php.net/bug.php?id=42472
If you do something like this it doesn't raise the notice and it passes the variable properly:
$_SESSION['keyName'][strval($count)]=$fetch['COMMENT'];
Edit: Thanks to Wiseguy for turning me on to this bit of info.
His comment below is:
According to this answer, "keys in the $_SESSION array are subject to
the same limitations as regular variable names in PHP, i.e. they
cannot start with a number". I guess this was a side effect of
register_globals. Curiously, the docs no longer state this. Perhaps
this limitation was removed when the register_globals option was
removed in PHP 5.4.0
To elaborate on that a little bit the reason it didn't work with register_globals is because of how PHP treats super global variables such as $_SESSION. One of the things that makes a super global special is that it exists in all scopes without needing to use the 'global' keyword. The other thing is that it creates variables for all of the child elements automatically... again in all scopes. So if you have something like
$_SESSION['username'] = 'Levi';
That would create an additional variable $username which is equal to 'Levi'. So in the example above when you set a
$_SESSION[1] = 'Test';
PHP is going to try and set a variable $1 = 'Test'; Which is not possible
Related
I have a a form that pulls data from a database(mysql to be specific) and echos the data into the value section of <input> tags. It doesn't seem to be working I have coded a view section of my website to do the same thing but from a different table in my database. I use the same code to make making changes easy and if another developer works on my site in the future. Anyway it doesn't seem to be working I'm not sure why though.
The full error I get:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/caseol5/public_html/jj/admin/news_update.php on line 9
Here is line 9 that the error is referring to:
$result = mysqli_query($link,$sql);
I know that both of those function are not null as I did:
echo $link
echo $sql
before that line after I started feting the error and they both are not null.
Here is the full code segment:
$nid = $_GET['nid'];
include ("../sql/dbConnect.php");
$sql = "SELECT * FROM jj_news WHERE news_id = $nid";
echo "<p>The SQL Command: $sql </p>";
echo "<p>Link: $link </p>";
$result = mysqli_query($link,$sql);
if (!$result)
{
echo "<h1>You have encountered a problem with the update.</h1>";
die( "<h2>" . mysqli_error($link) . "</h2>") ;
}
$row = mysqli_fetch_array($result);
$ntitle = $row['news_title'];
$ntline = $row['news_titleline'];
$ndesc = $row['news_desc'];
$nother = $row['news_other'];
I have looked into mysqli_query and I can't find anything I'm missing. I have also tired breaking the code down (and running parts of it and it gives the same error. My guess is it something small that I missed. I've looked at other question on this site that do that are a little similar but none seem to help. I've been looking at this for a while now and need another pair of eyes.
Update
As requested the contents of my dbconnect.php file:
$hostname = "localhost";
$username = "caseol5_jjoes";
$database = "caseol5_jj_site";
$password = "password1";
$link = mysqli_connect($hostname, $username, $password, $database);
$link = mysqli_connect($hostname,$username,$password,$database) or die("Error " . mysqli_error($link));
if (!$link)
{
echo "We have a problem!";
}
As clearly stated in the error message, mysqli_querydocs expects the first parameter to be a mysqli resource. In your case, this parameter is called $link but it holds a null value. A proper mysqli resource is normally obtained from connecting with the database by making use of mysqli_connectdocs
I expect the ../sql/dbConnect.php file holds the logic to connect with the database. Verify whether the $link variable is indeed initialized there. If it's not there, try to find an occurrence of mysqli_connect - maybe the resource is set to a different variable.
Without knowing what exactly is in ../sql/dbConnect.php, your problem right now is that you do not have a valid mysqli resource to use for mysqli_query.
I am retrieving data from a database in android so created a php file as below.
<?php
$db_host = "localhost";
$db_uid = "root";
$db_pass = "";
$db_name = "abc";
$db_con = mysql_connect($db_host,$db_uid,$db_pass) or die('could not connect');
mysql_select_db($db_name);
$sql = "SELECT * FROM people WHERE birthyear > '". $_POST["birthyear"]."'";
$result = mysql_query($sql);
while($row=mysql_fetch_assoc($result))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>
Now when i run it in the browser as localhost/script.php
error is thrown and the output is displayed as below
Notice: Undefined index: birthyear in C:\xampp\htdocs\jasonscript.php on line 14
[{"id":"1","name":"m","sex":"1","birthyear":"1989"},{"id":"2","name":"a","sex":"1","birthyear":"1986"},{"id":"3","name":"b","sex":"0","birthyear":"1986"}]
Please tell me how to correct my code.
$output[]=array("key"=>$row['field_name'],"key1"=>$row['field_name2']);
store array like this
You are directly inserting $_POST["birthyear"] in your query which makes you vulnerable to SQL injection. Stop doing it right now!
That is also why you get the error. When you directly call the script in your browser, that will be with a GET request and there wont be any POST variables available. So your $_POST array wont have a key for birthyear and thus it warns you about it.
You should start with something like
<?php
$by = isset($_POST["birthyear"]) ? $_POST["birthyear"] : "";
if (empty($by)) {
echo 'Invalid birthyear';
exit;
}
//SANITIZE YOUR BIRTHYEAR HERE
//in this case, probaly check for a integer between 1900 and 2100 or something.
//Although just an int could be enough to prevent injection
if (!is_int($by)) {
echo 'You failed to provide a valid year';
exit;
}
$sql = "SELECT * FROM people WHERE birthyear > '". $by."'";
//execute the code
?>
Although the above code is safe, you should check out bound parameters like used in mysqli prepared statements or PDO
you probably failed to send the values trough post properly.
try print_r($_POST); to see what you are actually sending
you still get all results because every year is > ''
I have a project (exisiting) and I am ordered to continue it
but there's something strange
in my connection
<?php
include "adodb5/adodb.inc.php";
$config['mysqlhost'] = 'localhost';
$config['mysqluser'] = 'xxx';
$config['mysqlpass'] = 'yyy';
$config['mysqldb'] = 'zzz';
$DB = ADONewConnection('mysql');
$DB->Connect($config['mysqlhost'],$config['mysqluser'],$config['mysqlpass'],$config['mysqldb'],true);
?>
and if I try to call query (same queries as below) from this page, it works (and when I echo, it shows the value)
So I go to other page
<?
include ("inc/con.php");
?>
<?php
$sql = ("SELECT * FROM table");
$query = mysql_query($sql)or die($myQuery."<br/><br/>".mysql_error());
$result = mysql_fetch_array($query);
echo $result ['table id'];
?>
and the result is
Notice: Undefined variable: myQuery in C:\xampp\htdocs\d88\www\mypage.php on line 9
No database selected
is there anything wrong with it?
since i try on con page, it works and when i include it to other page, it not working
You are not defining any $myQuery either in inc/con.php nor in the same file itself. Also you are not selecting any database with mysql_select_db:
mysql_select_db($config['mysqldb']);
You are suggest, also, not to use mysql_* functions as they are going to be deleted and are yet deprecated (and you can use PDO or mysqli).
Notice: I think $sql = ("SELECT * FROM table") gets evaluated as $sql = true.
You can not connect with ADODB connection and establish a query with mysql_query.
the syntax is something like this mysql_query ($query ,$con). $con is optional but if you do not specify it, the last link opened by mysql_connect() is assumed; but you have not any mysql_connect() statement before
because of my version of php, i must use <?php ?> instead of <? ?>
thanks for helping
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
So Im back with another php error, that I cant solve. But Im getting better:)
I have created a simple script that stores images in a database, I have no problems to store the file, but when Im reading the file i get an index error. It says
Notice: Undefined index: id in C:\wamp\www\gallery2\show.php on line
13
I cant really get what the problem is since Im thinking that everything is correct !?
the code for showing the images are
<?php
$username = "root";
$password = "";
$host = "localhost";
$database = "guestbook";
#mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
#mysql_select_db($database) or die("Can not select the database: ".mysql_error());
$id = $_GET['id'];
if(!isset($id) || empty($id)){
die("Please select your image!");
}else{
$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];
header('Content-type: image/jpg');
echo $content;
}
?>
You are trying to access $_GET['id']. However, if no argument id is present in the querystring of the request, the index id will not be available in the $_GET superglobal. That's why you receive the notice.
So you should be doing something like this:
$id = !empty( $_GET[ 'id' ] ) ? $_GET[ 'id' ] : null;
It is telling you the value id does not exist in the $_GET array. Does the URL you are accessing have a ?id=something on it?
The variable...
$_GET['id'];
...doesn't contain a value, therefore it is not defined.
Check if your form mechanism leading to this $_GET is working, maybe var_dump it.
If your script should cope with an empty variable there, maybe check this variable first, something like this:
$id = "";
if (isset($_GET['id']) { $id = $_GET['id']; }
I have this php file. The lines marked as bold are showing up the error :"mysql_query() expecets parameter 2 to be a resources. Well, the similar syntax on the line on which I have commented 'No error??' is working just fine.
function checkAnswer($answerEntered,$quesId)
{
//This functions checks whether answer to question having ques_id = $quesId is satisfied by $answerEntered or not
$sql2="SELECT keywords FROM quiz1 WHERE ques_id=$quesId";
**$result2=mysql_query($sql2,$conn);**
$keywords=explode(mysql_result($result2,0));
$matches=false;
foreach($keywords as $currentKeyword)
{
if(strcasecmp($currentKeyword,$answerEntered)==0)
{
$matches=true;
}
}
return $matches;
}
$sql="SELECT answers FROM user_info WHERE user_id = $_SESSION[user_id]";
$result=mysql_query($sql,$conn); // No error??
$answerText=mysql_result($result,0);
//Retrieve answers entered by the user
$answerText=str_replace('<','',$answerText);
$answerText=str_replace('>',',',$answerText);
$answerText=substr($answerText,0,(strlen($answerText)-1));
$answers=explode(",",$answerText);
//Get the questions that have been assigned to the user.
$sql1="SELECT questions FROM user_info WHERE user_id = $_SESSION[user_id]";
**$result1=mysql_query($sql1,$conn);**
$quesIdList=mysql_result($result1,0);
$quesIdList=substr($quesIdList,0,(strlen($quesIdList)-1));
$quesIdArray=explode(",",$quesIdList);
$reportCard="";
$i=0;
foreach($quesIdArray as $currentQuesId)
{
$answerEnteredByUser=$answers[$i];
if(checkAnswer($answerEnteredByUser,$currentQuesId))
{
$reportCard=$reportCard+"1";
}
else
{
$reportCard=$reportCard+"0";
}
$i++;
}
echo $reportCard;
?>
Here is the file connect.php. It is working just fine for other PHP documents.
<?php
$conn= mysql_connect("localhost","root","password");
mysql_select_db("quiz",$conn);
?>
$result2=mysql_query($sql2,$conn);
$conn is not defined in the scope of your function (even if you're including the connect.php file before that.
although you can use the suggestion to make $conn global, it's usually better practice to not make something global just for the sake of globalizing it.
i would instead pass $conn to the function as a parameter. this way, you can reuse the same function you wrote with different connections.
$conn isn't declared as a global so the function cannot access it, as it is not defined within it.
Either simply add
global $conn;
To the top of the function to allow it to access the $conn.
Or you can remove $conn from the mysql_query() statement. By default it will use the current connection (as mentioned in the comments below).
Where do you set $conn? It doesn't look like you set a connection within that function