require_once including old file, latest file values not considered - php

<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
require_once('dbConnect.php');
$result = mysql_query("SELECT count(*) from TableName;");
echo mysql_result($result, 0);
mysqli_close($con);
}
?>
In the above code sample, I can see that require_once is using older values of the file "dbConnect.php". The login credentials present in dbConnect.php is of older values, and not the latest one I updated recently. I can see this in the error message.
Please suggest how to overcome this error.

Related

Simple php code not printing to screen

This might seem stupid, but I have this simple php code, and it is not echoeing the message to the screen, neither is redirecting the page. I am not sure why. I tested the db connection and it is working, also the user and password used for test exist in the db (of course info changed here for security). What am I doing wrong? What am I missing?
<?php
session_start();
include_once("C:/webroot/connect.php");
if (isset($_POST['submit'])){
//$user=$_POST["httpd_username"];
$user="usernameXYZ";
//$pass=$_POST["httpd_password"];
$pass="passXYZ";
$query= "SELECT * FROM regtrack_users WHERE user_name='$user' and password='$pass'";
$result =pg_query($query) or die ("Unable to connect to db");
$numrows=(pg_num_rows($result));
if($numrows>0){
$row=pg_fetch_assoc($result);
$dbuser=$row['user_name'];
$dbpass=$row['pass'];
echo "$dbuser and $dbpass";
}
header("Location:login.php");
}
?>
It might because of this part:
if($numrows==1){
$row=pg_fetch_assoc($result);
$dbuser=$row['user_name'];
$dbpass=$row['pass'];
echo "$dbuser and $dbpass";
}
You should check if the $numrows variable really contains 1, or you should try $numrows>0 instead of $numrows==1
Root of the problem lies in 3rd line, you are providing a path to a file to include in wrong way. Read here about how properly you should be providing paths to files in php code:
http://yagudaev.com/posts/resolving-php-relative-path-problem/

How do I pull data from a database from a logged in user?

I will try to keep this post as simple as possible while including as much information as I can to assist you in helping me. Just as a forewarning, I am extremely new to PHP/Mysqli and over the course of the last week put together an internal data site for our company to make our jobs easier.
I have completed the user login process and it seems to be working quite well. If the user is not logged in, they can not see the PHP pages of the site and are redirected accordingly. Now that I have this part working. I am trying to incorporate a site identifier that shows their first and last name as well as a photo of them. The placeholders are already setup but I have not been very successful in getting this to work. I did, however, add a "Welcome "Email" message at the time by pulling the session data.
I will list the code that I am currently using below:
LOGIN PAGE:
<?php
require('../inc/db.inc.php');
session_start();
// If form submitted, insert values into the database.
if (isset($_POST['user_email'])){
$email = stripslashes($_REQUEST['user_email']); // removes backslashes
$email = mysqli_real_escape_string($con,$email); //escapes special characters in a string
$password = stripslashes($_REQUEST['user_pwd']);
$password = mysqli_real_escape_string($con,$password);
//Checking is user existing in the database or not
$query = "SELECT * FROM `users` WHERE user_email='$email' and user_pwd='".md5($password)."'";
$result = mysqli_query($con,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
if($rows==1){
$_SESSION['user_email'] = $email;
header("Location: ../index.php"); // Redirect user to index.php
}else{
echo "<div class='form'><h3>Username/password is incorrect.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
}
}else{
?>
DB PAGE:
<?php
$con = mysqli_connect("localhost","root","","dbadmin");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
SECURITY ON EACH PAGE:
<?php
include("pages/auth.php"); //include auth.php file on all secure pages
?>
AUTH PAGE:
<?php
session_start();
if(!isset($_SESSION["user_email"])){
header("Location: login.php");
exit(); }
?>
CODE USED TO DISPLAY EMAIL: ( only code I was able to get working)
<p>Welcome <?php echo $_SESSION['user_email']; ?>
I hope that one of you Guru types see that and know exactly how to get what I am looking for, or at least point me in the right direction. I have been searching the internet fir what seems like days reading all of the tutorials that I can find but I have not been able to successfully make anything work. I have found the following code but each time I try to add it to my code, it breaks the site.
$row = mysql_fetch_array($result);
Please assist if you are able. Thanks in advance.
Go to your login page where you check if row is equal 1, do this, change this
$_SESSION['user_email'] = $email;
To this
$_SESSION['user'] = $result->fetch_assoc();
Then do this
<p>Welcome <?php echo $_SESSION['user']['firstname']; ?></p>
Not a direct answer to your questions, but if are new to php, I would advise you to use one of the popular framework of php that already provide what you are looking for, and more, in a secure way, with secured libraries.
for example laravel or symfony.
Those are very well documented, and you will learn much more checking their documentation and tutorial than trying to reinvent the wheel here.

PHP mySQL Update doesn't work

I currently have a very big problem with PHP and mySQL. I moved a System I coded to a new Server. And while everything worked fine on the old Server, I had some problems on the new Server. Especially with mySQL. While I solved nearly all of them, I have one which I can't seem to get a hold on. And after 2 hours of trying i searched on the Internet for another two hours and updated my Syntax several times. But nothing seems to work. So now I'm here. I get a Connection to the database without a problem, but I can't update the values. I hope you can help me.
//Connect to mySQL Database
$verbindung = mysql_connect($server, $username, $passwort);
if (!$verbindung) {
echo "Couldn't connect: " . mysql_error();
}
$name=$_POST['fuehrer'];
$ident=$_POST['id'];
//Debugging
echo $name;
echo $ident;
$sql_befehl_0="UPDATE 'olgatermine' SET fuehrer = '".$name."' WHERE ID = '".$ident."';";
if (!mysql_query($verbindung, $sql_befehl_0)){
echo "Couldn't write to database";
}
//Close connection
mysql_close ( $verbindung );
What version of php use? Because in the newest versions of php the mysql functions are deprecated/removed, use instead mysqli.
Try to echo a mysqli_error at the end of the code, also mysql_error if your version of php accepts mysql functions.
If not version of php is the problem check this:
Wrong things what i see in your code..:
$sql_befehl_0="UPDATE 'olgatermine' SET fuehrer = '".$name."' WHERE ID = '".$ident."';"; // wrong
should be:
$sql_befehl_0="UPDATE `olgatermine` SET `fuehrer` = '".$name."' WHERE ID = '".$ident."';";
You need to run mysql_select_db('dbname') below line you do the mysql connection.
You can set at the first line of file:
ini_set('display_errors',1);
error_reporting(E_ALL);
to show all errors.

PHP - require_once in multiple pages

I am using LAMP stack to develop a PHP application. I am using require_once to include class files. I need to use the functions in those class files in more than one PHP page. So, I am including those class files in all the required PHP pages using require_once. But, if I include those class files in more than one page, the PHP file goes blank. It displays nothing. View source also displays nothing.
Files: test.php, process.php and class.test.php
test.php has
<?php
session_start();
require_once 'classes/class.test.php';
.
Few more classes
.
.
?>
<html>
<form name = "myForm" method="POST" action="process.php">
<input type = "text" name="username" value=""/>
<input type = "submit" value="Submit" />
</form>
</html>
process.php
<?php
session_start();
require_once 'classes/class.test.php';
$obj_test = new test();
$obj_test->test();
?>
class.test.php
<?php
session_start();
require_once 'class.misc.php';
require_once 'config.php'; //DB connection details
function test()
{
$obj_misc = new misc();
$id = $obj_misc->random_ID();
$username = $_POST['username'];
$query = "INSERT INTO test_table VALUES ('$id','$username',NOW());
mysql_query($query);
}
?>
Now, it returns a blank page. If I comment out the require_once in process.php, the test.php page displays the form, but on submitting the form the process.php throws an error "class test not found".
I am struggling with this problem for the past 2 weeks. :( It was working fine before that. I don't understand what went wrong. Please help.
You have an error in the PHP code for process.php; you are missing a semicolon:
require_once 'classes/class.test.php'
should be:
require_once 'classes/class.test.php';
If that doesn't fix it, then there is probably some other error somewhere in your code. Without access to the full source, we won't be able to do much.
For future reference, if a page goes blank, there is usually a problem with the PHP source code (ie, some type of interpreting error). As part of good debugging tactics, look into display_errors and error_reporting
Try out with
error_reporting(E_ALL);
Sounds like your error reporting is turned off. You should check your error logs to see what exception is being thrown when it's failing silently (that will give you a little more insight).
Additionally, you may want to add this at the top of your process script:
error_reporting(E_ALL);
Require once is a function ...
try adding the parens ...
require_once( 'classes/class.test.php' );
looks like you are missing a closing qoute on the sql query
$query = "INSERT INTO test_table VALUES ('$id','$username',NOW());
mysql_query($query);
should be
$query = "INSERT INTO test_table VALUES ('$id','$username',NOW())";
mysql_query($query);

Could anyone with MySQLi experience help me with this short SELECT code?

I have researched my question but there just isn't much help out there for MySQLI functions. I'm also new here so, please bear with me.
I have a test database named "website" and it contains a table called "users" which has data for all of the members of my website. In a separate file I have the php code that connects to my localhost and selects the database "website." I'm using the newest functions of MySQL so instead of the connection string containing mysql_connect it contains mysqli_connect. The registration process works great and I can find no error in any of the other code that uses the mysqli functions.
My include file (connect.php)
<?php
$link = mysqli_connect("localhost", "useracc", "useracc1", "website");
?>
I set up a test script for the login section.
<?php
include('connect.php');
$user = $_POST['user'];
$query = "SELECT birthday FROM users";
$result = mysqli_query($link, $query) or die ("RESULT ERROR");
echo "$user";
echo "$result";
?>
There is a password box on the previous page but for the test script I left that out and so far, only the $user is shown on the page. I get no error even when I replace "RESULT ERROR" with mysqli_error($link). I've tried interchanging ' for " in every part of the code that uses quotes but that didn't work. I've tried rewording the $query line but that also had no effect. I'm really new to MySQL and php so if you guys could tell me where I'm going wrong I'd really appreciate it.
Thanks for reading my question. Have a great day.
Add the following lines to the connect.php after you connect line
if (mysqli_connect_errno()) {
printf("Can't connect Errorcode: %s\n", mysqli_connect_error());
exit;
}
this will ensure the connection is made without any errors and return them if there are ...

Categories