I've got two queries going on, one is working fine... Echos all the right column values. The second one shows all the data var_dump but keeps showing Undefined Index. I don't understand what I've done wrong.
I tried to make an array to define the index via the suggestion of a developer but that gave me zero results
$navpage = array();
$navpage['splash_id']='';
etc but that results in the empty space obviously... I don't know why this was the solution I was led to.
Error code says : Notice: Undefined index: splash_id in...
I know there are so many questions like this and most of them are talking about email forms they are working on, while I am just trying to output data from the database.
Here is my query file arts.php
<?php
require 'scripts/start.php';
$pages = $db->query("SELECT article_id, department, title, article_link, shortdesc, img_meta, live FROM content WHERE department='Arts' AND live='Yes'
")->fetchAll(PDO::FETCH_ASSOC);
$navpage = $db->query("SELECT splash_id, page, live, big_message, img1, text1, link1, expdate FROM splash WHERE page='arts' AND live='Yes'
")->fetchAll(PDO::FETCH_ASSOC);
require VIEW_ROOT . '/artpages.php';
?>
The page where I echo things like column - text1
<?php require VIEW_ROOT . '/department/arts_header.php';
?>
<div id="major">
<div id="so" class="mainimage">
<div class="mainimage"><img src="cms_img/header/<?php echo $navpage['img1']?>" class="splashimage"></div>
</div>
<div id="contentbox">
<div id="middle"><div id="articleheadline" class="titlefont"><?php echo $navpage['text1']?></div>
<?php if (empty($pages)): ?>
<p class="textfont">Sorry, there are no articles on this page.</p>
<?php else: ?>
<?php foreach($pages as $page): ?>
<div class="article_container">
<div class="article_promo"><img src="cms_img/<?php echo escape($page['img_meta']) ?>"class="promofit"></div>
<div class="article_title"><span class="article_title_font"><?php echo $page['title']?></span></div>
<div class="article_desc"><span class="article_desc_font"><?php echo $page['shortdesc']?></span></div>
</div><?php endforeach; ?>
<?php endif; ?>
</div></div></div></div>
<?php
require VIEW_ROOT . '/templates/footer.php';
?>
I expect echos of $navpage to fill in like $page does.
Fetch all get a multiple array result so you need to use foreach loop like you did in $pages or use the following code below to fetch only a single array.
$dbh = new PDO("SELECT article_id, department, title, article_link, shortdesc, img_meta, live FROM content WHERE department='Arts' AND live='Yes'");
$stmt = $dbh->prepare("SELECT name FROM mytable WHERE id=4 LIMIT 1");
$stmt->execute();
$navpage= $stmt->fetch();
Because you use ->fetchAll(PDO::FETCH_ASSOC). Its return array of item navpage
So you must to code $navpage[0]['img1'] and $navpage[0]['text1']
Notice check !empty($navpage) before use $navpage[0]
Related
I just started php and sql, so this question is probably very silly. I have a database containing nicknames, ids and scores, and I would like to display with php all the nicknames and their corresponding scores, how can I do it ?
So far, I've got this but it only displays one pseudo and its score.
<?php
require 'actions/database.php';
$getAllInfos = $bdd->query('SELECT pseudo, score FROM users');
$getAllInfos->execute(array());
$AllInfos = $getAllInfos->fetch();
?>
<body>
<br><br>
<div class="container">
<h2> scores : </h2>
<br>
<?php echo $AllInfos['pseudo'] ?> : <?php echo $AllInfos['score'] ?>
</div>
By the way, here is the creation of my database :
<?php
$bdd = new PDO('mysql:host=localhost;dbname=seio;charset=utf8;', 'root', 'root');
?>
Thanks a lot for reading :)
See: https://www.php.net/manual/en/pdostatement.fetchall.php
Which will give you an array you can loop though.
Been trying to figure out what's wrong for nearly two hours now.
I have made a wall post feature for my website, posts should be displayed on the profile wall, however instead of all of the posts being displayed, only 1 is. I have no idea what is wrong with my code, I can't find any issues.
The $profile_username variable has been defined in previous code and works. Even if I just type the name of the profile in it still only returns one post.
//----------submit wall post----------//
$getwallposts = mysql_query("SELECT * FROM `wallposts` WHERE `postedto`='$profile_username' ORDER BY `postid`");
//check if any rows are returned
while($wallposts = mysql_fetch_assoc($getwallposts)) {
$postid = $wallposts['postid'];
$postedby_username = $wallposts['postedby'];
$wallpostdate = $wallposts['dateposted'];
$wallpost = $wallposts['post'];
$querypostedby_info = mysql_query("SELECT * FROM `users` WHERE `username`='$postedby_username'");
//get the info above
if (mysql_num_rows($querypostedby_info) > 0) {
$getpostedby_info = mysql_fetch_assoc($querypostedby_info);
$postedby_id = $getpostedby_info['id'];
$postedby_profilepicture = $getpostedby_info['profilepicture'];
}
//lets keep the line breaks
$wallpost=nl2br($wallpost);
//display the posts
$wallpoststicker =
"
<div id='wallpost-container'>
<div id='wallpost-header'>
<img src='$postedby_profilepicture'><div id='wallpost-header-by'><a href='/profile.php?id=$postedby_id'>$postedby_username</a> said:</div>
<div id='wallpost-date'>• $wallpostdate</div>
</div>
<div id='wallpost-content'>
$wallpost
</div>
</div>
";
}
Also, I know that mysql is deprecated; my version of PHP can handle mysql queries fine though.
Where to do you echo you're output?
If it is after the While you will only get the last result that is stored in the variable.
Output within the while loop.
I can see a couple of issues with that code. First, what it seems to be your problem:
$wallpoststicker =
"
<div id='wallpost-container'>
<div id='wallpost-header'>
<img src='$postedby_profilepicture'><div id='wallpost-header-by'><a href='/profile.php?id=$postedby_id'>$postedby_username</a> said:</div>
<div id='wallpost-date'>• $wallpostdate</div>
</div>
<div id='wallpost-content'>
$wallpost
</div>
</div>
";
This code overrides the $wallpoststicker variable on every loop. That's why you only print one result at the very end.
But secondly, you're doing a query within a loop, which is potentially very inefficient. Have you thought on doing a LEFT JOIN between "wallposts" and "users" so you have all the data you need before you start your loop?
I am trying to make a small discussion system where people can login and post a discussion. On the homepage I want to show the discussions.
First I retrieve the data for the discussions (topic, category, username, etc) from the table 'discussions'and order them by 'date_time'.
I put these values in div's that are working just how I want it. The problem I am having now is to show the avatar of the user. The avatars are stored in a table 'users' under the column 'avatar'.
So I need to retrieve the value from the column 'avatar' in the table 'users' where 'username' matches the username of the discussion.
This is the code that I have now but it's not working. I have tried different things but I am not very familiar yet with PHP so I don't really know how to go from here.
Thanks in advance!
$result = mysql_query("SELECT
topic,
category,
date_time,
username,
SUBSTRING(discussion, 1, 80) AS discussion
FROM discussions
ORDER BY date_time DESC");
while($record = mysql_fetch_array($result))
{
?>
<div class="discusscolumn">
<div>
<p><? echo $record['category'] . ": <b>" . $record['topic'] . "</b>"?></p>
</div>
<div>
<?php
$discussion_username=$record['username'];
$getavatar = mysql_query("SELECT avatar, username FROM users WHERE username='$discussion_username' ");
$avatarrecord = mysql_fetch_array($getavatar);
echo'<span class="smallpic"><img src="user/'.$record['username'].'/'.$getavatar['avatar'].'"></span>';
?>
</div>
<div>
<p>Posted by <? echo "<a href='user.php?u=".$record['username']."'>".$record['username'] ?> </a></p>
</div>
<div>
<p><br><? echo $record['discussion'] ?> ...</p>
</div>
<div>
<p><? echo $record['date_time'] ?></p>
</div>
<div>Discuss</div>
</div>
<?php } ?>
PS: I know I am working with mysql instead of mysqli and that I'm mixing HTML and PHP code but I just want the basics to work now.
Please check your img source(src), that you have passed as user/username/avatar instead of passing like this please put url like http://www.w3schools.com/images/w3schools_green.jpg so that you will be getting out. But in your case you need to have all your user image in some other global storage area for eg.Amazon s3 bucket, and you need to get string from database construct like above url and append that in img src inside input tag
Anyone please help me fix my php code here.
Im working with php and PDO and I have been switching codes around but nothing works. Any help will be appreciated.
Here is the code for my index page:
<?php
require_once 'init.php';
$supportQuery = $conn -> query("
SELECT id, title, content
FROM table");
while ($row=$supportQuery ->fetchObjecy()) {
$support[]=$row;
}
?>
<?php if(!empty($support)): ?>
<?php foreach($support as $supp): ?>
<a href="page.php?id=<?php echo $supp->id; ?>&title=<?php echo $supp->title; ?>">
<?php echo $supp->title; ?></a>
<?php endforeach; ?>
<?php else: ?>
No items found.
<?php endif; ?>
This code works the way I like it to work but when I run code and click the link, that is where I am getting errors.
Here is my php code for page.php:
<?php
require_once 'init.php';
$articleQuery = $conn -> prepare("
SELECT id, title, content
FROM table WHERE id='$id' and title='$title'");
while ($row=$articleQuery ->fetchObject()) {
$support[]=$row;
}
$articleQuery->execute();
?>
<?php if(!$_SESSION($support)): ?>
<?php echo $support->id; ?>
<?php echo $support->title; ?><br/>
<?php echo $support->content; ?>
<?php else: ?>
<p>Are you inventing titles? Sorry but this page doesnt exist nowhere.</p>
<?php endif; ?>
I need your help please. I am getting the following error messages:
Notice: Undefined index: title and id in /page.php
Notice: Undefined variable: support in /page.php
Notice: Trying to get property of non-object in /page.php
♡nbgmalimit
Ok, to start...
1) In the 2nd code block, you need to execute the statement BEFORE trying to fetch the results.
2) You should be binding the values $id and $title prior to executing to avoid sql injection.
Look at the examples in the manual to see how to do both of these...
http://php.net/manual/en/pdostatement.execute.php
3) You're fetching the row as an object and adding the resulting object as a value in the $support ARRAY.
4) Assuming the id and title are unique, you do not need to loop it and add it to an array, as you should only get 1 row returned. Just use fetch. Using PDO::FETCH_ASSOC for the fetch style will return an array. Using PDO::FETCH_OBJ for the fetch style will return an object.
http://php.net/manual/en/pdostatement.fetch.php
Try something like this...
$row = null;
$stmt = $conn->prepare("SELECT id, title, content FROM table WHERE id = :id and title = :title");
$stmt->bindParam( ":id", $id, PDO::PARAM_INT );
$stmt->bindParam( ":title", $title, PDO::PARAM_STR );
$result = $stmt->execute();
if ($result) {
$row = $stmt->fetch(PDO::FETCH_OBJ);
} else {
// handle error
}
?>
<?php if(!empty($row)): ?>
<?php echo $row->id; ?>
<?php echo $row->title; ?><br/>
<?php echo $row->content; ?>
<?php else: ?>
<p>Are you inventing titles? Sorry but this page doesnt exist nowhere.</p>
<?php endif; ?>
I'm new to PHP and pardon me for asking this very basic question. What I want to do is to display or view a page based on a specific record. For example, I have a home.php page which lists records of lessons. And when I click on a specific record, it will go a page named lesson.php . I have to view the relevant information/data from my dB of that specific lesson. I tried to use GET but I think it's not going to meet the requirement of my system.
This is what I've tried so far:
$qry1stQuarter = $conn->prepare("SELECT l.lesson_title FROM tbllessons as l
JOIN tblstudents as s
ON l.grade_level = s.grade_level
WHERE quarter_code = '1st'
AND s.grade_level=:grade_level");
$qry1stQuarter->execute(array(':grade_level' => $grade_level));
<div id="tabs-2">
<div id="accordion">
<h3><strong>Yunit 1</strong></h3>
<div>
<?php
for($i=0; $row = $qry1stQuarter->fetch(); $i++){
$lesson_title = $row['lesson_title'];
?>
<div id = "lessons">
<?php
echo "<a href = 'lesson_view.php'>$lesson_title </a>";?>
</div>
<?php
} // end of for loop
?>
</div> <!-- end of Yunit 1 -->
What is the best way to do this? Your help is pretty much appreciated. Thanks.
In your database, I assume you have an ID column. A typical way to do what you are asking is to use that ID as a GET parameter on a link, and then include that in your WHERE clause in your SQL statement.
Eg:
echo "<a href='lesson_view.php?id=$lesson_id'>$lesson_title</a>";?>
And then on your lesson_view.php page, your SQL has something like this:
SELECT * FROM tbllessons WHERE id = mysql_real_escape_string($_GET['id'])