Changes to PHP script aren't executed - php

I'm building a website with a very basic blog-like "News" functionality. Basically, you enter the title, author, author's picture, date, and message of the news update to on a password-protected form, and then the magic of PHP grabs that stuff and sticks it in a MySQL database. Then, using more PHP, these little articles are dynamically loaded on a "News" page. The script that uploads the data to the MySQL database works fine. So does the script that displays the news on the webpage. Here's the problem: if I edit the "display" PHP script and upload the edited script onto the hosting server, those edits aren't reflected in the website. Here's my code:
<?php
include('dbconnect.php'); //connects to database
//select the table
$result = mysql_query("select * from newscms order by id desc limit 5");
//grab all the content from the table
while($r = mysql_fetch_array($result))
{
$id = $r['id'];
$title = $r['title'];
$date = $r['date'];
$user = $r['user'];
$icon = $r['icon'];
$message = $r['message'];
//displays the rows
echo "<img src='$icon' align='left'/><strong>$title</strong> <br/>
Posted on $date
Posted by: <strong>$user</strong><br />
$message <br />";
}
?>
Now if I changed the "echo" function to instead show something like:
echo "<img src='$icon' align='right' width='12' height='24'/><em>$title</em> <br/>
Posted on $date
Posted by: <strong>$user</strong><br />
$message <br /> I like turtles";
(Notice I have changed the alignment and size of the icon, changed <strong> to <em> for the title, and added the text "I like turtles" to the end)
with all these changes, absolutely nothing changes on the webpage!
Why? It's driving me insane! Is there some flaw in my script I can't see? Is it a server-side problem that I should contact my web host about? (And before you ask, yes, I cleared my browser cache.) Help please!

Step 1 create a new folder to your desktop called any name then download the updated file from your ftp folder to your local folder . then open that file you downloaded and see if really those changes you made exists . from there you might be able to trace what might be the cause of that problem . Also if I were you I would have deleted the that file in the ftp and I upload the current one so that I can be sure.

So.
It was a pathname problem. Sort of.
At first, I was implementing the display functionality by using a PHP include function to include a "display.php" file. Then, for reasons now forgotten, I commented out that function and instead copy-pasted the contents of the display.php file into the webpage file. Of course, I forgot that I did that, and have been editing the display.php file. Solution: stop being an idiot, uncomment that include, and delete the rest. All fixed. Thank you to everyone who tried to help.

Related

Fetching images from the server for a social media post

I am building a basic social media site for a university project and I am at the stage of displaying posts. Currently the posts display using a foreach which creates a series of div tags containing the information about each post from the db. I am now looking to add the images uploaded with these posts onto the page but I am not sure how to go about fetching them.
When the images are uploaded they are placed in a folder in the server called postImages, and are renamed to fit this format:
postID + image number
where image number is determined by how many images the user uploads, starting at 0.
My initial idea was to use scandir() to list all posts and explode their names to fetch the ID but this is much more complex than it had to be as there is another table in the DB called postImages which contains an ID for each post as well as the postID of the post it belongs to, so fetching which images are needed is no problem. However, I am not sure how to go from having the required IDs to actually fetching the image from the folder.
The current code for building the posts:
include 'config.php';
$postsSQL = "SELECT * FROM Post ORDER BY postTime DESC" ;
$result = mysqli_query($connection, $postsSQL);
foreach ($result as $row) {
echo "<div class = 'postContainer' id =".$row['postID'].">";
echo "<div id = 'postTitle' class = 'postTitle'>". "Post Title: ".$row['postTitle']. "</div>";
echo "<div id = 'postDesc' class = 'postDesc'>" . "Post Description: ".$row['postDescription']. "</div>";
echo "<div id = 'postLocation' class = 'postLocation'>" . "Post Location: ".$row['postLocation']. "</div>";
echo "<div id = 'postTime' class = 'postTime'>" . "Posted at: ".$row['postTime']. "</div>";
echo "<div id = 'UserID' class = 'userID'>".$row['UserID']. "</div>";
echo "</div>";
echo "<br>";
}
Thanks for taking a look, and sorry in advance if I've missed any details or this is a simple question.
Serve those images to users as static objects, not directly from PHP.
The first thing you do is make sure your images can be viewed in browsers. In other words, you should be able to use a URL something like
https://oddity.example.edu/uploads/postImages/72_0.png
You need to make that /uploads folder (or what ever you name it) visible to your system's web server.
Next, store the "slug" -- the partial URL -- for your image in your database. For the example above you might store 72_0.png.
Then, when writing out the HTML for your post page, include a tag like this:
<img src="https://oddity.example.edu/uploads/postImages/72_0.png" />
and the browser will retrieve and show the image.
You might be able to use PHP code like this to do that.
$imageRoot = 'https://oddity.example.edu/uploads/postImages/";
...
$imageurl = $imageRoot . $row['imageslug'];
echo '<img class="image" src="$imageurl" />;
Finally, people who store uploaded images for later viewing often use random hard-to-guess filenames for them to slow down web-scraping cybercreeps. That means assigning random file names upon uploads rather than using the naming scheme you mentioned.
Pro tip: avoid mixed-case table, column, and object names. Various file systems (including Windows's NTFS) do various different things with case-sensitivity, and you don't want to see your stuff stop working if you put in on a new server.

Can I write all my SQL queries to phpMyAdmin / HTML-file or do I need to use other programmes?

I have a database in phpMyAdmin that I have set up with XAMPP. I am working on a website that shows statistics from the user inputted scores in the database. Say that I would like to show the score percentile to the user after they submit their score: where do I write the query for that? In the HTML/PHP code? In phpMyAdmin? Somewhere else like a workbench or PopSQL?
I have successfully gotten the website to display the average score in any given table by writing this code to the HTML file:
<?php
$sql = "SELECT AVG(score) AS score FROM $input_subject";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_object($result) ;
echo nl2br("Average score for $input_subject: \n \n"
. round($row->score));
?>
It works, but when I search for tutorials for example for the percentile query or something like the CHECK function (to not accept anything less than 0 or more than 120) it seems that the queries are always written somewhere else than the HTML file.
Also, when I try to write the SQL code in phpMyAdmin, it always shows a bunch of error messages, even though I copy/pasted it in (changing the table names etc., of course).
So, do I need to look into some other programmes were to write the queries in or can I just write them into the HTML-file or in the phpMyAdmin? I'm a total newbie with this so anything helps!
Yes, you can write SQl queries(PHP) before your html code and also in between your html code, but make sure to change file extension to .php from .html otherwise PHP code will be printed on browser as it is.
"it seems that the queries are always written somewhere else than the HTML file."
we do this while using AJAX. we send data to server (a PHP file where we process data received) and server will give response. All this happens behind the scenes without page reloading.
in your case you can create a new PHP file lets say process.php, add your PHP code into it.
process.php
<?php
$sql = "SELECT AVG(score) AS score FROM $input_subject";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_object($result) ;
echo nl2br("Average score for $input_subject: \n \n"
. round($row->score));
?>
send user inputted scores from HTML file through ajax to PHP file (process.php) and response received from that file can be displayed in html file without page reloading.
you can go through the ajax api by following link jQuery-AJAX

Save SQL data to PHP file

I've faced a problem that I can't solve. Thing I want to do, to "save" all output SQL data to file (which should renew at timed interval ex. 5 mins) and print the file out on the website.
This is my example script, which shows data... but it makes connection each time web is loaded.
<?php
mysql_connect($dbserver, $dblogin, $dbpass);
mysql_select_db($dbname);
mysql_query("SET NAMES 'utf8'");
$rektanrekt = mysql_query("SELECT * FROM characters WHERE (accesslevel < '1') order by pkkills desc LIMIT 10");
$i = 1;
echo '<table id="top_table"><tr id="table_title"><td></td><td> </td><td>Nick</td><td></td><td>Kills</td></tr>';
while($row = mysql_fetch_array($rektanrekt))
{
echo '<tr><td id="skaiciai">' . $i . '.</td><td></td><td id="nickas"><font>';
echo $row["char_name"];
echo '</font></td><td> </td><td id="kills"><font>';
echo $row["pkkills"];
echo '</font></td></tr>';
$i++;
}
echo '</table>';
?>
And it does the job done -> http://prntscr.com/a3hpmx
But is it possible to make "backup" of this file, and show it if SQL is offline ... or even better - if SQL is ON update the file each time interval, if SQL is OFF just show latest one?
Saving output data to file:
The simplest way is to output the results in a text file using code similar to the following:
$timeStamp = time();
$pathName = "/gameLog/".$timeStamp.".txt";
$myFile = fopen($pathName, "w");
//+++++++++++++++++++++++++++++++++++
/*here you place your previous code, and inside the while
loop you use the fwrite() function to include the information
you want to save. For example:
fwrite($myFile, ($info."\n"));
where $info is the data you want to include in the file, and
"\n" is the line break.
*/
//+++++++++++++++++++++++++++++++++++
fclose($myFile);
Note: I used $timeStamp = time(); as the name of the file you are saving with the information you wanted because it will consistently generate a new value, hence you won't be overwriting your log files. Also it can help track at what time this log was saved. You could make this cleaner of course.
Doing this in intervals:
As jeroen mentioned, a cron job is probably your best solution to doing so. If you are using CPanel (linux), when you go to the CPanel homepage of your webhosting account (I am assuming your have an account with godaddy or another provider), there should be an option called "Cron Jobs" or something similar. What a Cron Job does is that it is a "Scheduled Event" that is told to execute some file in an interval you specify (you also specify the directory of your file). I found an example on youtube for you, however the video is a bit out of date, though the process should be similar:
https://www.youtube.com/watch?v=bmBjg1nD5yA
Edited: I noticed you are working on L2Aria, if you need any help, let me know! :)

How to read a blob in Base64 using PHP and MySQL?

I am a newb with 6 months experience, self-taught via StackEx/books/etc. Created a pretty decent website with login/register and storing some info via mySQL. I have been through every single BLOB post here and I have some decent output.
I think I, like most newbs, know enough to be dangerous, don't have the greatest foundation laid out so when it gets to serious understanding of built-in functions, arrays and passing arguments we can lose the flow and I basically think I have dug a hole by using includes to call some navbar so that I can't just use a header to output the damn image as I echo the user name after login so it has already outputted lines and it will be a monster to undo. Three levels of nav, unauthenticated, authenticated and admin.
The database connection and write to/read from is OK. I can store the BLOB and I can even read it back and store the array in a variable and then debug see the binary but I can't get it to display on an HTML page.
Here is the fun:
$stmt = $dbc2->query("SELECT * FROM equip1");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$name = $row['equipname1'];
$desc = $row['equipdesc1'];
$img = $row['equipimg1'];
}
<div class="col-lg-12">
<h1><?php echo $name ?></h1>
<p><?php echo $desc ?></p>
<pre>
<?php print_r($img); ?>
</pre>
<?php echo "<img src='data:image/jpeg;base64," . base64_encode( $img ) . "' />"; ?>
</div>
$dbc2 connects
equip1 table is two simple varchar cols, one BLOB col.
$name and $desc echo out ok, the $img displays broken link.
I check the array via pre code and is matching, per what is stored in dB during upload.
From what I have read and gone through, seems like you can't do this at same time unless via data URI which I have done but still broken link. Not making sense to me at all. I try the header and of course output already started but I can see also outputs the binary.
Here is screenshot of the URI method:
Since both methods "seem" to get into and out of the dB but do not display I'm going round in circles. Please help me out to display the image on an html page. I would like to echo it anywhere and then I can just style the page after that. Thanks!
Try Like this
<?php echo '<img src="data:image/png;base64,'.base64_encode($img).'">';?>
Uhh NEVER MIND. Never thought reading someones answer would make for "fresheyes". I noticed I was encoding but I already encoded it when I stored it. Mea culpa. DOH! :)

Creating a Registration and Login Page

I'm currently in the process of making a Registration and Login Page. My first page asks you if you want to create an account or login. What we have to do is take the information from the form that the user enters information into, and place it into a text file. I've got this working roughly. I understand that this is not good practice for security reasons, but this is an assignment for class and I MUST put the information into the text file. I have the information going into the text file, however I am having trouble comparing the posted username to all of the usernames inside of the database. Here is my code for the newaccount.html page, and the register.php file that the form submits to.
newaccount.html
<html>
<head>
</head>
<body>
<h3>Hello new user! Choose a user name and password ^_^</h3><br>
<form action = "register.php" method = "POST" >
Username: <input type = "text" name = "username"><br><br>
Password: <input type = "password" name = "pass"><br>
<input type = "submit" value = "Create Account" name = "submit"><br>
</form>
<form method = "LINK" action = "Proj2_practice.html">
<input type = "submit" value = "Home Page" name = "submit2">
</form>
</body>
</html>
register.php
<?php
$username = $_POST['username'];
$password = $_POST['pass'];
$userAndPass = $username.",".$password;
$userNames = 'usernames.txt'." ";
$passwords = 'passwords.txt'." ";
$fh_users = fopen($userNames, 'a+')or die("sorry, we couldnt open the file");
$fh_passwords = fopen($passwords, 'a+')or die("sorry, we couldnt open the file");
fwrite($fh_users, $username." ");
fwrite($fh_passwords, $password." ");
$allUserNames = fread($fh_users, filesize('usernames.txt'));
echo $allUserNames;
?>
The usernames and passwords are being sent to the text files correctly, At the end of the code, it is not echoing the variable. As of right now, there is no information in the text files, I don't know if that is the reason that nothing is being echoed. Is my approach correct here? What I'm trying to do here is send each name and password to a username and password text file.Then, Im planning on
exploding each of those text files by a space between them, which is why I add one after writing them to the text file, and then comparing the usernames and the passwords by their respective array elements. Am I overcomplicating this as much as I think I am? Please share your comments with me, I'm just trying to get better ^_^
Thank you in advance!
if you open the file with the "a+" flag it will place the file pointer on the end of the file. so when you're reading then you won't get anything as you're already on teh end of teh file.
just go back to the beginning of the file before reading
fseek ($fh_users, 0);
or close and open it again with flag "r"
$fh_users = fopen($userNames, 'r')
or simply do
$dataString = file_get_contents($userNames);
or
$dataArray = file($userNames);
If this is a class assignment, can I suggest a method as well as a solution?
If things like this don't work, try to reduce the complexity of what you're trying to do and build step by step. You say yourself that your file is empty; if it's empty you're not going to be able to read from it so your first problem is figuring out how to correctly write to a file and read stuff back from it. Forget about all of the rest of your task, focus on that first.
Now, if you open a resource (such as a file), you must close it again when you no longer need it. So you definitely need an fopen / fclose pair to begin with.
$username = "Penguin";
$userNames = 'usernames.txt'; // Why did you have a space after this?
$fh_users = fopen($userNames, 'a+');
fwrite($fh_users, $username." ");
fclose($userNames);
I haven't tested this, but at this point, you should have a file that contains "Penguin ". If you run the code multiple times, you should see the file contents grow.
If you want to read things back from this file, realise that there is a file pointer that determines where you are going to read or write. You can open the file again in such a way that the file pointer is moved to the front (and so you can read what you need) or you can explicitly move it back to the front using fseek or rewind.
Once you have gotten this writing and reading to work, then add code to handle your form and so on.

Categories