php executing code from previous version of file - php

I'm a newbie using phpmyAdmin on a NAS drive. I have a simple html page which calls a php file (CommitteeExtract.php) to retrieve data from a mySQL database on the server. While developing I created a number of versions of the php file. I eventually got it working on version 8 (CommitteeExtract8.php). I now want to rename it to CommitteeExtract.php. However, when I do so the generated html file looks and works like the original (failed) verison. I have noticed renaming the file to any previous version give the result from that version, even if the code in the file is version 8.
Is the server caching the old versions and ignoring the current content? How do I get it to recognise only the current version.
I hope this is clear, any help would be appreciated:
CommitteeExtract.php looks like this:
<html>
<head>
<link rel="stylesheet" href="Style_Main.css" >
<title>Details</title>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Birthday</th>
<th>Email</th>
<th>Street</th>
<th>Town</th>
<th>MobilePhone</th>
</tr>
<?php
$servername = "xxx.xxx.x.xxx";
$username = "User";
$password = "1234";
$dbase = "mydatabase";
// Create connection
$conn = mysql_connect($servername, $username, $password) or die("Cannot ccnnect to server");
mysql_select_db($dbase) or die("Could not connect to $dbase database");
clearstatcache();
$query="SELECT FullName, DateOfBirth, EmailAddress, Street, Town, City, County, PostCode, MobilePhone FROM mydatabase'";
$result = mysql_query($query) or die("Query Data Extract failed");
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['FullName'] . "</td>";
echo "<td>" . $row["DateOfBirth"] . "</td>";
echo "<td>" . $row["EmailAddress"] . "</td>";
echo "<td>" . $row["Street"] . "</td>";
echo "<td>" . $row["Town"] . "</td>";
echo "<td>" . $row["MobilePhone"] . "</td>";
echo "</tr>";
}
mysql_free_result($result);mysql_close($link);
?>
</table>
</body>
<p>There's a button down here! Waheyyyy</p>
<button type="Button" onclick="ExportToExcel()">Export to Excel</button>
</html>
it is called from a menu item in a list by this
<li class="dropdown">Export Data
<div class="dropdown-content">
Gmail Contacts
Committee Extract
Produce Forms
</div>
</li>
When the href contains CommitteeExtract8.php, and the file is called CommitteeExtract8.php it works fine, returning my data.
However If rename the file to CommitteeExtract.php and change the href to CommitteeExtract.php it fails with same errors as the original CommitteeExtract.php, even though the code is completely different.
I'm sorry, I don't know how to view and post logs

Related

No database connection with no errors

I have Ubuntu 16.10 x86_64 x86_64. I installed LAMP to program in PHP and to create databases. In my php program I want to connect to my local database for creating a table ( in HTML ) with the data of any row of the table.
The problem is that when I open the php file( localhost/file.php ) through firefox ,the browser doesn't charge anything. If thare were been an error during the connection with the database, It would have printed something in the browser.
Here the code:
<!DOCTYPE html>
<html>
<head><title> SQL & PHP </title></head>
<body>
<?php
$db = mysql_connect("localhost", "root", "password")
or die ("Non riesco a creare la connessione");
mysql_select_db("scuola")
or die ("Non trovo il DB");
$sql = "SELECT id_utente, nome_utente, password_utente, conta_pres FROM utenti WHERE conta_pres <> 0";
$ris = mysql_query($sql) or die ("Query fallita!");
echo "<TABLE><TR><TH>ID utente <TH> Nome utente <TH>Password<TH>Contatore visite</TR>";
while ($riga= mysql_fetch_array($ris))
{
echo ("<TR>");
echo "<TD>" . $riga["id_utente"];
echo "<TD>" . $riga["nome_utente"];
echo "<TD>" . $riga["password_utente"];
echo "<TD>" . $riga["conta_pres"];
}
mysql_close();
?>
</body>
</html>
I checked the syntax (using a website) of the code and thare aren't problems,even because I copied this one by a book. I read that mysql_connect has been deprecated, so I replaced it with new mysqli_connect but the error still remains: white page. I tried to put 2 echo, one before the connecting function and one after that. Only the first echo is printed on the screen. I tried to type in the terminal sudo apt-get install php5-mysql but there is an error:
The "php5-mysql" packet has not run to install
Can someone help me, please?
First of all use mysqli instead of mysql.
I think I have found the problem. When you call mysqli_select_db, it expects 2 parameters and you only specified one. Even though you have set the $db database connection, you need to specify which database you want to select the database name from.
So mysqli_select_db($db, "scuola") should do the trick.
And at the bottom close the connection specifying which connection to close. In your case it is: mysqli_close($db);
<!DOCTYPE html>
<html>
<head><title> SQL & PHP </title></head>
<body>
<?php
$db = mysqli_connect("localhost", "root", "password")
or die ("Non riesco a creare la connessione");
mysqli_select_db($db, "scuola") // see this line
or die ("Non trovo il DB");
$sql = "SELECT id_utente, nome_utente, password_utente, conta_pres FROM utenti WHERE conta_pres <> 0";
$ris = mysql_query($sql) or die ("Query fallita!");
echo "<TABLE><TR><TH>ID utente <TH> Nome utente <TH>Password<TH>Contatore visite</TR>";
while ($riga= mysql_fetch_array($ris))
{
echo ("<TR>");
echo "<TD>" . $riga["id_utente"];
echo "<TD>" . $riga["nome_utente"];
echo "<TD>" . $riga["password_utente"];
echo "<TD>" . $riga["conta_pres"];
}
mysqli_close($db); // see this line
?>
</body>
</html>

Make PHP run after form submitted without redirecting to another PHP page to process the data

I'm not sure if this is a duplicate question due to the fact that I'm not even sure how to properly form it (That's why the title of this question makes no real sense). I am beginning to learn PHP so I can utilize SQL databases in my webpages.
My question is, how would I be able to make it so a user types in a password into an input box, which is then used by a PHP script as the password to a MySQL login to display the contents of a table in an HTML table? Now, I have most of this figured out, the only problem is displaying the table on the same page which the password is entered and the submit button is hit. Would I need to use AJAX? Because I don't know the first thing about it. Here is what I have so far:
Index.php:
<DOCTYPE! html>
<html>
<head>
<title>Guy's Random Project</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<script src="scripts.js"></script>
</head>
<body>
<div>
<form action="" method="POST">
<font size="4">Password: </font><input type="text" id="pass" name="pass"/><br>
<input class="buttonGreen" type="submit" value="Submit" id="1" onMouseDown="mouseDown(1)" onMouseOut="mouseUp(1)"/>
</form>
</div>
<table><thead><th>ID</th><th>Name</th><th>Type</th><th>Rating</th></thead><tbody>
<?php include 'getTableData.php'?>
</tbody></table>
</body>
</html>
getTableData.php:
<?php
$servername = "localhost";
$username = "root";
$password = $_POST['pass'];;
$dbname = "testing";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, type, rating FROM crap_food";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"] . "</td><td>" . $row["name"] . "</td><td>" . $row["type"] . "</td><td>" . $row["rating"] . "</td></tr>";
}
} else {
}
$conn->close();
?>
I understand the fact that PHP is run server side, and is only run before any HTML is read by the client, so making a function run when a button is hit wouldn't exactly be possible just using PHP and HTML, but this would be similar to what I'm looking for. I don't want to redirect the user to another page via the form action (Which is why it's blank), I want to keep this all within index.php. Thanks (And sorry for the horrible forming of this question, I'm just a bit confused).
You Use AJAX for this.
$.ajax({
url: 'xxx.php',
data:formData,
cache: false,
contentType:false,
processData:false,
type: 'post',
success: function(response)
{
//do something
}
try this :
$password =$_POST['pass'];
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username,$password);
$sql = 'SELECT firstname FROM users';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
while ($r = $q->fetch()):
echo ($r['firstname']);
echo "<tr><td>" . ($r['firstname']) . "</td></tr>";
endwhile;
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" .$pe->getMessage());
}
?>

Error 500 on webserver but not local host

I have a website that is working on my local machine and works on one set of webhosting but when I move it to the secure intranet that it needs to be placed on I get a server 500 error.
If I simplify the problem the server is not handling includes properly so I have put all content into the php page and I directly go to the GET part that is broken I put https://192.168.2.252/searchbysurname.php?q=morris in the address bar. This works on my normal localhost. I cant access the console logs on the server.
Search by Surname.php
<?php
session_start();
$q = ($_GET['q']);
# Connect
$dbc = #mysqli_connect ( '*******', '*********', '*******', '********')
OR die ( mysqli_connect_error() ) ;
mysqli_set_charset( $dbc, 'utf8' ) ;
$q1="SELECT employees.employeeid, employees.firstname, employees.surname FROM employees where UCASE(employees.surname) LIKE UCASE('%".$q."%')";
$r1 = mysqli_query($dbc,$q1);
echo "<table class = 'CSSTableGenerator'>";
while($row1 = mysqli_fetch_assoc($r1))
{
echo "
<tr><td>" . $row1['firstname'] . "</td><td>" . $row1['surname'] . "</td>
<td><input type='button' value='View' onclick='viewresults(" . $row1['employeeid'] . ")'></td>
<td><input type='button' value='Edit' onclick='editresults(" . $row1['employeeid'] . ")'></td>
</div></td></tr>
";
}
echo "</table>";?>
<div id="mainpart"><b></b></div></div>
the server in the intranet can have a different configuration?
Does it have mysqli for instance?
try to run a simple script with <?php phpinfo(); ?> both on local and intranet server and compare...

Query PHP output suddenly only shows code, no values?

I recently had my eureka moment when I finished my first database connection. After closing my browser and reopening the html form, the output suddenly changed to code instead of the database values?
HTML form:
<form action="formulier3.php" method="post">
Hoogte: <input type="text" name="height"><br>
Breedte: <input type="text" name="width"><br>
<input type="submit">
</form>
PHP Page:
<?PHP
$user_name = "root";
$password = "root";
$database = "addressbook";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "SELECT * FROM price WHERE height = " . $_POST["height"] . " AND width = " . $_POST["width"] . "";
$result = mysql_query($SQL);
while ( $db_field = mysql_fetch_assoc($result) ) {
print $db_field['ID'] . "<BR>";
print $db_field['value'] . "<BR>";
print $db_field['height'] . "<BR>";
print $db_field['width'] . "<BR>";
}
mysql_close($db_handle);
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}
?>
This is my output:
"; print $db_field['value'] . "
"; print $db_field['height'] . "
"; print $db_field['width'] . "
"; } mysql_close($db_handle); } else { print "Database NOT Found "; mysql_close($db_handle); } ?>
Does anyone knows what's going on here?
Thank you in advance!
Somehow your server has stopped processing your php pages through the php processor (apache module or fastcgi or whatever).
What you see is the effect of presenting your php code as html. The fact that you don't see all your code but rather a small part of it, it is because the part from the first < (in <?php) until the first > (in print $db_field['ID'] . "<BR>"; is being parsed by the browser as an html tag and so it is not printed. If you look at the page source you'll see the full php code.
So there has been some server-side change that has produced that php files are directly server to the browser instead of parsed by the php engine.
One possible cause, is that you are developing in your local computer and when it worked you typed in your browser something like http://localhost/your_page.php but now you are opening the php file directly from the filesystem, so the browser shows something like file:///xampp/htdocs/your_page.php. You should always open your php pages through the web server (ie. using http://localhost/....) and never by double-clicking on the file in the file explorer.

Can't Connect to MySQL DB

I'm trying to conntect to a mysql database for the first time. Can you see what's not working correctly below?
I get an error on line 3.
What would my server address look like from Godaddy to my database? I found that address in my control panel.
Thanks for any help. Never programmed using PHP before.
<body>
<?php
$con = mysql_connect("MydbName.db.3924516.hostedresource.com ","Userid","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Gallerys");
echo "<table border='1'>
<tr>
<th>Thumb Url </th>
<th>Gallery Url</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['THUMBURL'] . "</td>";
echo "<td>" . $row['GALLERYURL'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
Try check back the authenication settings, you might use the wrong host, username or password.
<?php
$con = mysql_connect("usually it is localhost","your MYSQL username","your MYSQL password");
// Checking the login details.
// Example of default xampp login details: $con = mysql_connect("localhost","root","");
// Xampp MYSQL default does not have password.
if (!$con) // If the login details are wrong, it will should an error.
{
die('Could not connect: ' . mysql_error());
}
?>
Without the exact error it is hard to be sure but on line 3 you have an extra space after hostedresource.com. Try removing the space between the end of the hostname and the quatation mark. Like so:
$con = mysql_connect("MydbName.db.3924516.hostedresource.com","Userid","password");

Categories