clicking images for their detail page - php

I'm trying to enable a user to go to a post detail page. They can find these images after a search. But I always get this:
Warning: Illegal string offset 'postID' in C:\xampp\htdocs\eindwerk2\index.php on line 106
Warning: Illegal string offset 'postID' in C:\xampp\htdocs\eindwerk2\index.php on line 180
this is the php code. The seach function works btw.
$_SESSION['KEYWORD'] = array();
$error = '';
//$allResults2 = array();
if (isset($_POST['Find'])) {
try {
$pdoConnect = new PDO("mysql:host=localhost;dbname=imdterest", "root", "");
} catch (Exception $exc) {
echo $exc->getMessage();
exit();
}
$postTags = $_POST['naam'];
//$pdoQuery = "SELECT * FROM posts INNER JOIN user ON posts.postUser = user WHERE postTags = :postTags";
$pdoQuery = "SELECT posts.postID, posts.postUser, posts.postImageUrl, posts.postDescription, user.firstname, user.lastname, user.userID FROM posts INNER
JOIN user ON posts.postUser=user.email WHERE postTags = :postTags;";
$pdoResult = $pdoConnect->prepare($pdoQuery);
$pdoExec = $pdoResult->execute(array(":postTags" => $postTags));
if(!empty($_POST)){
while ($row = $pdoResult->fetch(PDO::FETCH_ASSOC)) {
$_SESSION['KEYWORD'][] = $row['postImageUrl']['postID'];
//$poster = $row['postID'];
//$allResults2[] = $row['postUser'];
//echo $postTags;
}
}
else {
echo 'no';
}
if(count($_SESSION['KEYWORD']) === 0) {
$error = "Sorry no results!";
}
This is the html
<div class="search">
<?php
foreach ($_SESSION['KEYWORD'] as $imageLink){
$postid = $imageLink['postID'];
echo '';
}
/*foreach ($allResults2 as $imageUser){
echo '$imageUser';
}*/
?>
<div class="searchError"><?php echo $error; ?></div>
</div>

Your error is from this line of code:
$_SESSION['KEYWORD'][] = $row['postImageUrl']['postID'];
This isn't legal syntax in PHP. You can reproduce it with:
<?php
$test = array(
test1 => "Hello",
test2 => "hello2"
);
echo $test["test1"]["test2"];
?>
When you try to execute this code, you get:
PHP Warning: Illegal string offset 'test2' in
Sadly, I'm not that sure what you try to achieve with this line of code. Anyway, your SQL result is a one-dimensional array. Your code acts like you work with a two dimensional array. You can only use $row["postImageUrl"] or $row["postID"] you you can't use both directly.
If you describe a bit better what you try to do with this line, I can help you. But actually I have no idea what your thoughts were.

Related

What part of my code is causing an Undefined Index error? (PHP)

I am working on a small forum and I am currently trying to display the forum topics in a separate file. For whatever reason when I open the file it is displaying these errors:
Notice: Undefined index: cid in C:\xampp\htdocs(A)Book 2.0\Bootstrap\topics.php on line 19
Notice: Undefined index: scid in C:\xampp\htdocs(A)Book
2.0\Bootstrap\topics.php on line 20
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result,
boolean given in C:\xampp\htdocs(A)Book
2.0\Bootstrap\content_function.php on line 41
I've tried to use an isset function for the variables, but it hasn't worked. I'm calling the variables from a separate functions file.
Here's the code displaying the topics:
<?php
session_start();
require_once ('../db.php');
// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
$_SESSION['message'] = "You must log in before viewing your profile page!";
header("location: error.php");
exit; //<--- add this
}else {
// Makes it easier to read
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$active = $_SESSION['active'];
}
include ('content_function.php');
$cid2 = $_GET['cid'];
$scid2 = $_GET['scid'];
?>
<!DOCTYPE html>
<html lang = "en">
<head>
<title>Topics</title>
<link href="main.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div class = "content">
<?php
/*
$select = mysqli_query($mysqli, "SELECT * FROM categories");
while($row = mysqli_fetch_assoc($select)){
echo "<h1>", $row['category_title']."</h1>" ;
}*/
disptopics($cid2, $scid2, $mysqli);
/*if (isset($_GET['cid'], $GET['scid'])){
disptopics();
}*/
?>
</div>
</body>
</html>
Also, here is the code for the functions:
<?php
require '../db.php';
function dispcategories($mysqli){
$select = mysqli_query($mysqli, "SELECT * FROM categories");
while($row = mysqli_fetch_assoc($select)){
echo "<table class = 'category-table'";
echo "<tr><td class= 'main-category'colspan='2'>".$row['category_title']."</tr></td>";
dispsubcategories($row['cat_id'], $mysqli);
echo "</table>";
}
}
function dispsubcategories($parent_id, $mysqli){
$select = mysqli_query($mysqli, "SELECT cat_id, subcat_id, subcategory_title, subcategory_descr FROM categories, subcategories
WHERE ($parent_id = categories.cat_id) AND ($parent_id = subcategories.parent_id)");
echo "<tr><th width='90%'>Categories</th><th width='10%'>Topics</th></tr>";
while($row = mysqli_fetch_assoc($select)){
echo "<tr><td class='category_title'><a href='topics.php/".$row['cat_id']."/".$row['subcat_id']."'>
".$row['subcategory_title']."<br/>";
echo $row['subcategory_descr']."</a></td>";
echo "<td class='num-topics'>".getnumtopics($parent_id, $row['subcat_id'], $mysqli)."</td></tr>";
}
}
//Displays categories
function getnumtopics($cat_id, $subcat_id, $mysqli){
$select = mysqli_query($mysqli, "SELECT category_id, subcategory_id FROM topics WHERE ".$cat_id." = category_id
AND ".$subcat_id." = subcategory_id");
$get = mysqli_num_rows($select);
return $get;
}
//Displays Topics Within categories
function disptopics($cid, $scid, $mysqli){
$select = mysqli_query($mysqli, "SELECT topic_id, author, title, date_posted, views, replies FROM categories, subcategories, topics
WHERE ($cid = topics.category_id) AND ($scid = topics.subcategory_id) AND ($cid = categories.cat_id)
AND ($scid = subcategories.subcat_id) ORDER BY topic_id DESC");
if(mysqli_num_rows($select) != 0) {
echo "<table class='topic-table'>";
echo "<tr><th>Title</th><th>Posted By</th><th>Date Posted</th><th>Views</th><th>Replies</th></tr>";
while($row = mysqli_fetch_assoc($select)){
echo "<tr><td><a href='readtopic.php/".$cid."/".$scid."/".$row['topic_id']."'>
".$row['title']."</a></td><td>".$row['author']."</td><td>".$row['date-posted'].".</td><td>".$row['views']."</td>
<td>".$row['replies']."</td></tr>";
}
echo "</table>";
} else {
echo "<p>This category has no topics yet! <a href='newtopic.php/".$cid."/".$scid."'> Add a new one!</a></p>";
}
}
?>
I've looked over each file multiple times and used a code checker that says I have no syntax errors.
$_GET is an array, and if the user haven't defined a scid=something in the query string, then $_GET['scid'] is undefined. There, you got an undefined index Notice.
You should test if isset($_GET['scid']) before trying to read it's value.
from where this $_GET['cid'] and $_GET['scid'] is getting data please check echo $_GET['cid'] or you also do var_dump($_GET);

Fail to update the data in database

I try to update the data in the database, but when I run the code, there is no error message appear, looks like its a logical error but I still don't have any clue about what is happening with my code.
Here is the code
<?php
include("conn.php");
SESSION_START();
if($_SESSION["loggedin"]!="true"&& $_SESSION['login'] != '')
header("location:login.php");
$aid = $_SESSION["usr"];
$result = mysql_query("select r.CustomerID from customer c inner join results r on r.CustomerID = c.CustomerID where c.Username = '".$aid."' ");
if (false === $result) {
echo mysql_error();
}
$row = mysql_fetch_assoc($result);
?>
<?php
if (isset($_POST["submitbtn"]))
{
$bookid = $_POST["bookid"];
$LP = $_POST["LP"];
$budget = $_POST["budget"];
$smokep = $_POST["SmokeP"];
$spreq = $_POST["sp_req"];
$query = mysql_query("UPDATE `results` SET LP = '$LP', budget = '$budget', SmokeP = '$smokep', sp_req = '$spreq'
WHERE results.BookID = '".$bookid."' and results.CustomerID = '".$result."'");
if (false === $query)
{
echo mysql_error();
}
?>
<script type = "text/javascript">
alert("Amendment Saved!!");
</script>
<?php
}
?>
Is the error coming from the select query? Or the if statement for the submitbtn went wrong?
First of all you cant put session start here
You must put it on the first line after open php tag
Second
update res='$ new_value' where ...
Tell me if it's not usefull to try another solution

PHP Variable Error When Selecting From Table

I have a blog where I'm selecting the articles from a database using PHP. The problem is that becuase of my search terms I'm hitting an error. Here is my code:
<?php
if(isset($_GET["cat"])){
$cat = $_GET["cat"];
}else{
$cat = "all";
};
?>
<?php
if($cat == "all"){
$cat_var = "";
}else{
$cat_var = "WHERE cat = '$cat'";
}; // NOTE THIS LINE
?>
<?php
if(isset($_GET["issue"])){$issue = $_GET["issue"];}else{
$issue = "all";
};
?>
<?php
if($issue == "all"){
$issue_var = "";
$limit = 4;
}
else{
$issue_var = "AND issue = '$issue'"; // NOTE THIS LINE
$limit = 200;
};
?>
<?php
$count_posts_sql = "SELECT id FROM articles $cat_var $issue_var"; // NOTE THIS LINE
$count_posts_res = mysqli_query($con, $count_posts_sql);
$num_init_posts = mysqli_num_rows($count_posts_res);
//If None, Then Exit
if($num_init_posts == 0){
header("Location: /home");
exit();
}
...
?>
So my url would be http://website.com/articles/all/2015-10, which is what I want. However $cat_var & $issue_var is causing the error because it's selecting:
SELECT * FROM articles AND issue = '2015-10' // NO WHERE STATEMEMT IS SHOWN
How do I overcome this error?
You could get this going by sticking a WHERE 1=1 in
$count_posts_sql = "SELECT id FROM articles WHERE 1=1 $cat_var $issue_var"; // NOTE THIS LINE
This is because you start off with an AND value = 1 without starting the WHERE clause, which creates an invalid query.
Then take the WHERE out of this line and replacing it with an AND:
$cat_var = "AND cat = '$cat'";
You can initialize your where query string like this:
$where = 'WHERE 1 = 1 ';
and for there after you can concatenate depending on your inputs.

PHP mySQL search script for website

I highly appreciate that you try to help me.
My problem is this script:
<?php include("inc/incfiles/header.inc.php"); ?>
<?php
$list_user_info = $_GET['q'];
if ($list_user_info != "") {
$get_user_info = mysql_query("SELECT * FROM users WHERE username='$list_user_info'");
$get_user_list = mysql_fetch_assoc($get_user_info);
$user_list = $get_user_list['username'];
$user_profile = "profile.php?user=".$user_list;
$profilepic_info = $get_user_list['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./img/avatar.png";
}
else {
$profilepic_info = "./userdata/profile_pics/".$profilepic_info;
}
if ($user_list != "") {
?>
<br>
<h2>Search</h2>
<hr color="#FF8000"></hr>
<div class="SearchList">
<br><br>
<div style="float: left;">
<img src="<?php echo $profilepic_info; ?>" height="50" width="50">
</div>
<?php echo "<h1>".$user_list."</h1>"; ?>
</div>
<?php
}
else {
echo "<br><h3>User was not found</h3>";
}
}
else {
echo "<br><h3>You must specify a search query</h3>";
}
?>
I am creating a search script that takes the mysql databse information and shows the result associated to the search query. My script is the above, but keep in mind the sql connection is established in an extern scipt.
The problem is that i want the script to first check if the user is found with the search query in the username row, and then get the entre information from that user and display it. If the user is not found with the username query, it should try and compare the search query with the name row, and then with the last name row. If no result is displayed it should then return an else statement with an error, e.g. "No user wsas found"
Yours sincerely,
Victor Achton
Do the query as Muhammet Arslan ... but just counting the rows would be faster ...
if(mysql_num_rows($get_user_info)){
//not found
}
you should add a "Limit 1" at the end if you are just interested in one result (or none).
But read about prepared statements
pdo.prepared-statements.php
This is how it should be done in 2013!
Something like this but you don't need 3 queries for this. you can always use OR in mysql statements
$handle1 = mysql_query("SELECT * FROM users WHERE username = $username"); // Username
if (($row = mysql_fetch_assoc($handle1) !== false) {
// username is found
} else {
$handle2 = mysql_query("SELECT * FROM users WHERE name = $name"); // name
if (($row = mysql_fetch_assoc($handle2) !== false) {
// name is found
} else {
$handle3 = mysql_query("SELECT * FROM users WHERE lastname = $lastname"); // Last name
if (($row = mysql_fetch_assoc($handle3) !== false) {
// last name is found
} else {
// nothing found
}
}
}
Already you did ,but you can improve it by using "AND" or "OR" on ur sql statement.
$get_user_info = mysql_query("SELECT * FROM users WHERE username='$list_user_info' or name = '$list_user_info' or last_name = '$list_user_info'");
$get_user_list = mysql_fetch_assoc($get_user_info);
if(empty($get_user_list))
{
echo "No User was found";
}
and you should control $list_user_info or u can hacked.
Here some adapted copy pasting from php.net
Connect
try {
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
foreach($dbh->query('SELECT * from FOO') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
fetch data
$stmt = $dbh->prepare("SELECT * FROM users where name LIKE '%?%'");
if ($stmt->execute(array($_GET['name']))) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}
the rest is your programing ...
And do some reading it's very dangerous to use copied code without understanding !

php string error

i am experiencing some weird behaviour here.
i am using the following code to reference the facebook api.
$query = "SELECT msg, user_id, comment_time FROM comments WHERE aid = '$aid' ORDER BY comment_time DESC";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_object($result)){
$uidval = $row->user_id;
$posterInfo = $facebook->api_client->users_getInfo($uidval, array('name', 'pic_square_with_logo', 'profile_url'));
$nameuser = $posterInfo[0]['name']; //this is line 50
$pic = $posterInfo[0]['pic_square_with_logo'];
$profile_url = $posterInfo[0]['profile_url'];
echo '<img src="'.$pic.'" />';
echo ''.$nameuser.'';
echo '<br>';
echo $row->comment_time;
echo '<br>';
echo $row->msg;
}
}
it gives me this error:
Fatal error: Cannot use string offset as an array in /home/amitver/public_html/roadies/comments.php on line 50
but surprisingly i am using the exact same code successfully at the top of my page. why this weird behaviour. this is the code at the top of page:
//connect to fB
$uid = $user_id;
$userInfo = $facebook->api_client->users_getInfo($user_id, array('name', 'pic_square'));
$nameuser = $userInfo[0]['name'];
$pic = $userInfo[0]['pic_square'];
I think that sometimes theusers_getInfo is returning an array, while other times it is returning a string. Probably it only returns a simple string if only one result is available.
Try this:
$nameuser = ($posterInfo[0]) ? $posterInfo[0]['name'] : $posterInfo['name'];
this will happen if $posterInfo is actually an empty string ('').
can you var_dump($posterInfo) in the loop and check what it's doing...

Categories