Undefined index for mysql result - php

<?php
$result = mysqli_query($conn,"SELECT * FROM news ORDER BY date DESC
LIMIT 5")or die(mysql_error());
if (!$result) {
printf("Error: %s\n", mysqli_error($conn));
exit();
}
echo "<table align='left' CELLPADDING='50' >";
while($row = mysqli_fetch_array($result))
{
if ($row['photoid'] == NULL){
$date_string = $row['date'];
$date = strtotime($date_string);
$date = date('m/d/y', $date);
echo "<tr>";
echo "<td style='width: 750px'>" .$row['title'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 700px'>" . $row['news'] . "</td>";
echo "<td style='width: 100px'>" . $date . "</td>";
echo "</tr>";
}
else
{
$result = mysqli_query($conn,"SELECT news.title,news.date,news.news,photo.photo FROM news, photo WHERE news.photoid = photo.photoid ORDER BY date DESC") or die(mysql_error());
$row = mysqli_fetch_array($result);
$date_string = $row['date'];
$date = strtotime($date_string);
$date = date('m/d/y', $date);
echo "<tr>";
echo "<td style='width: 750px'>" . $row['title'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 700px'>" . $row['news'] . "</td>";
echo "<td style='width: 100px'>" . $date . "</td>";
echo "</tr>";
echo "<td style='width: 800px'>" . '<img height="300" width="300" src="data:image/jpeg;base64,'.base64_encode( $row["photo"] ).'" >' . "</td>";
}
}
echo "</table>";
mysqli_close($conn);
?>
SO the website shows a news column, the code gets the news data from the database, if there is a photoid (if the news has a photo to go with it) then it adds the news to the table with a photo. If there is no photo is adds the news to the table without one. Simple. At the moment for testing I have two news articles both with photos, the first one works perfectly then the second errors
Undefined index: photoid in
For the line if ($row['photoid'] == NULL){. There is a photoid.

You need to check if variable/key is set before checking the value. Try this:
if (isset($row['photoid']) && $row['photoid'] == NULL){

Related

Data from Database into the table (can't figure out how to do layout)

This is my code (horrible one):
<?php
include 'connect/con.php';
$result = mysqli_query($con,"SELECT id, vidTitle FROM newsvid");
$result1 = mysqli_query($con,"SELECT imgCover, vidSD FROM newsvid");
$result2 = mysqli_query($con,"SELECT published FROM newsvid");
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result1)) {
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td >" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
mysqli_close($con);
?>
</body>
</html>
The question is how to show data from database in this layout:
--------------------------------
-id----------vidTitle-----------
--------------------------------
-imgCover------vidSD------------
--------------------------------
----------published-------------
So every time I will add more data , another block like I showed before will add up under existing one.
........................................................................................
There's no need to write 3 queries. You could do that with only one select, and then put all the echos inside a while. That way you're writing, it would run all the ids and titles first, then it would put a table after the table, with cover and vidSD.
Try to make a single query:
SELECT id, vidTitle, imgCover, vidSD, published FROM newsvid
That way you will have, on each row returned from database, all the information about the same row.
Now, running a while is the same as you're doing, just adapting some HTML:
echo "<table width='600' border='1'><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'>" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
You may want to order it too. Adding ORDER BY id DESC, would do that.

Updating multiple MySql entrys

I've been working on creating an internal site for our company. I haven't had many issues until now. I have been able to retrieve and insert data into my database, but now for some reason when I try to UPDATE an entry, the database can't be selected for some strange reason. I've attached a copy of my code thus far. I don't know what I am missing. Thank you!
This is my code for looking up the information in the database:
<?php
session_start();
$transport = mysqli_connect("localhost", "user", "pw", "db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<?php
$raw_date = $_POST['appt_date'];
$date = date("Y-m-d", strtotime($raw_date));
if ($raw_date == '') {
echo "Please go back and pick a date";
exit;
}
$sql = "SELECT * FROM appointments WHERE date = '".$date."' ORDER BY appttime";
$result = mysqli_query($transport, $sql);
$i=0;
echo "<h2 align='center'>Schedule for $raw_date</h2>";
echo "<table border='2' style='width: 100%; margin: auto; border-width: 1px'><tr><th>Resident Name</th><th>APT #</th><th>Appt. Time</th><th>Location Phone</th><th>Location Name</th><th>Address</th><th>City</th><th>Zip</th><th>Bus or Car</th><th>Escort Name</th><th>Transfer</th><th>Comments</th><th>Dparting Times</th></tr>";
echo "<form name='update_times' method='post' action='depart.php'>\n";
while($row = mysqli_fetch_array($result))
{
echo "<input type='hidden' name='id[$i]' value=" . $row['id'] . "";
echo "<tr>";
echo "<td align='center'>" . $row['r_name'] . "</td>";
echo "<td align='center'>" . $row['room'] . "</td>";
echo "<td align='center'>" . date("g:i A", strtotime($row['appttime'])) . "</td>";
echo "<td align='center'>" . $row['apptphone'] . "</td>";
echo "<td align='center'>" . $row['l_name'] . "</td>";
echo "<td align='center'>" . $row['address'] . "</td>";
echo "<td align='center'>" . $row['city'] . "</td>";
echo "<td align='center'>" . $row['zip'] . "</td>";
echo "<td align='center'>" . $row['buscar'] . "</td>";
echo "<td align='center'>" . $row['escort_name'] . "</td>";
echo "<td align='center'>" . $row['transfer'] . "</td>";
echo "<td align='center'>" . $row['comments'] . "</td>";
echo "<td align='center'><input name='out[$i]' style='width: 70px' type='text' value='" . date("g:i A", strtotime($row['depart'])) . "' /></td>";
echo "</tr>";
++$i;
}
echo "<input type='submit' value='Set Depart Times'>";
echo "</form>";
echo "</table>";
$_SESSION['sessionVar'] = $raw_date;
?>
This is the update code:
<?php
session_start();
$transport = mysqli_connect('localhost', 'user', 'pw', 'db_name');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<?php
$size = count($_POST['out']);
$i=0;
while ($i < $size)
{
$departing = $_POST['out'][$i];
$departing = date("H:i:s:u",strtotime($departing));
$id = $_POST['id'][$i];
$sql = "UPDATE transport.appointments SET depart = $departing WHERE id = $id";
mysqli_query($transport, $sql) or die ("Error in query: $sql");
echo "Depart times updated!";
++$i;
}
mysql_close($transport);
?>
For some reason the update code doesn't want to select my database. Thank you again!
The MySQL UPDATE syntax is:
UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value;
It looks like you've tried to reference your database and your table in the UPDATE query: transport.appointments. I might be wrong, but I can't find anything on the internet saying that is valid syntax.
Try just referencing the table:
$sql = "UPDATE appointments SET depart = $departing WHERE id = $id";

Can't display image

I don't know why doesn't display the avatar 100x100. Something is wrong and I don't know how to fix it.
$result = mysqli_query($con,"SELECT * FROM `users` WHERE `verified` = 1 and lastcheck=0 order by `authoredPostCount` DESC");
echo "<table border='1'> <tr>
<th>Picture</th>
<th>Verified</th>
<th>Videos</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row "<img src="['avatar']."></td>";
echo "<td>" . $row['userId'] . "</td>";
echo "<td>" . $row['authoredPostCount'] . "</td>";
echo "</tr>";
}
echo "</table>";
if (!$cuserId) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
mysqli_close($con);
?>
Replace
echo "<td>" . $row "<img src="['avatar']."></td>";
With
echo "<td><img src=". $row['avatar']."></td>";
if you are saving image file name in database then use it like that
echo "<td><img src='path/to/folder/". $row['avatar'].".jpeg'></td>";

PHP select only one entry shows

how come when I use this code:
$emailc = 'thomas990428#me.com_classes';
$emaila = 'thomas990428#me.com_assignments';
$emailp = 'thomas990428#me.com_projects';
$resulty = mysqli_query($con,"SELECT * FROM `$emailc` ORDER BY period"); if (!$result) echo mysqli_error(); else // ok, do your thing.
$resulti = mysqli_query($con,"SELECT * FROM `$emaila` ORDER BY duehw"); if (!$result) echo mysqli_error(); else // ok, do your thing.
$resulto = mysqli_query($con,"SELECT * FROM `$emailp` ORDER BY dueproj"); if (!$result) echo mysqli_error(); else // ok, do your thing.
$classcount = 1;
while($row = mysqli_fetch_array($resulty))
{
$period = $row['period'];
$teacher = $row['teacher'];
$subject = $row['subject'];
$subjecto = strtolower($subject);
$subjecto = str_replace(' ', '', $subjecto);
$grade = $rowy['grade'];
echo "<section id='" . $subjecto . "'> \n";
echo "<p class='title'>" . $subject . "</p> \n";
echo "<a style='cursor:pointer;' onclick='homework" . $classcount . "()'>Homework </a>|<a style='cursor:pointer;' onclick='projects" . $classcount . "()'> Projects</a> \n";
echo "<div id='homework" . $classcount . "'><br /><a onclick='addassignment()'>Add Assignment</a><br />";
echo "<table class='homework'>";
echo "<tr>";
echo "<th class='title'>";
echo "Title";
echo "</th>";
echo "<th class='duedate'>";
echo "Due Date";
echo "</th>";
echo "</tr>";
while($row = mysqli_fetch_array($resulti))
{
$subjecthw = $row['subjecthw'];
$namehw = $row['namehw'];
$duehw = $row['duehw'];
echo "<tr>";
echo "<td class='title'>";
echo $namehw;
echo "</td>";
echo "<td class='duedate'>March ";
echo $duehw;
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div> \n";
echo "<div id='projects" . $classcount . "'><br /><a onclick='addassignment()'>Add Assignment</a><br />";
echo "<table class='homework'>";
echo "<tr>";
echo "<th class='title'>";
echo "Title";
echo "</th>";
echo "<th class='duedate'>";
echo "Due Date";
echo "</th>";
echo "</tr>";
while($row = mysqli_fetch_array($resulto))
{
$subjectproj = $row['subjectproj'];
$nameproj = $row['nameproj'];
$dueproj = $row['dueproj'];
echo "<tr>";
echo "<td class='title'>";
echo $nameproj;
echo "</td>";
echo "<td class='duedate'>March ";
echo $dueproj;
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div> \n";
echo "</section> \n";
$classcount += 1;
}
I only get the hw and proj in one section. Am I doing it wrong? Thanks! I know that it probably will never work, but how do I fix it? I have 3 tables and I need to get data from all of them.
Don't use $row within the while loop.
e.g
while($row = mysql_fetch_row($resulti)){
while($row2 = mysql_fetch_row($resulto)){
}
}
Also didn't have time to go through the code but it's never a good idea to run SQL in a loop.

How can I join these two html tables together?

I want to create a compare sort of page. I currently have two tables with the information, but want it to be on table or join both together. The first row would show Name of brand, second would show cost, etc... I am grabbing the values of selected checkboxs and based on what they selected it get compared.
$testName = $_POST['testCompare'];
$rpl = str_replace("_", " ", $testName);
$firstOne = "$rpl[0]";
$secondOne = "$rpl[1]";
echo "$firstOne";
echo "<br/>";
echo "$secondOne";
$query = "SELECT * FROM test_table WHERE test_name = '$firstOne'";
$query2 = "SELECT * FROM test_table WHERE test_name = '$secondOne'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
$result2 = mysql_query($query2) or die ("Error in query: $query2. " . mysql_error());
if (mysql_num_rows($result) > 0 && mysql_num_rows($result2) > 0) {
//if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
if ($firstOne == $row[1]) {
{
echo "<table border=1>";
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<tr>";
echo "<td>" . $row[1] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[2] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[3] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[4] . "</td>";
echo "</tr>";
}
}
}echo "</table>";
while($row2 = mysql_fetch_row($result2)) {
if ($secondOne == $row2[1]) {
{ echo "<table border=1>";
echo "<tr>";
echo "<td>" . $row2[0] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[1] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[2] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[3] . "</td>";
echo "</tr>";
echo "<td>" . $row2[4] . "</td>";
echo "</tr>";
}
}
}
echo "</table>";
}
else {
echo "No Results";
}[/CODE]
Thanks
Remove the </table> after the first loop.
Remove <table border=1> from both loops.
Add <table border=1 before the first loop.
Currently, you're defining a new <table border=1> each time you enter the loop. This will result in this HTML code:
<table ..>
<tr>...
<table ..>
..
<table>
..
Et cetera
</table>
<table ..>
Et cetera
</table>

Categories