All my jQuery with Ajax functions are stop working after inserting following PHP codes,
<?php
//includes data base configuration file
include_once "config.php";
$order = "SELECT * FROM tblUser";
$result = mysql_query($order);
if(!$result){
//if result not success exit from script
die("Error:".mysql_error());
}
//more codes here...
?>
Related
I have made an "api", I have converted data from my sql table into JSON and that is echoed onto my file "api.php", I have "get.php" and I would like to get specific bits of data from the JSON on get.php. It's not working and just throwing an error however
This is for a contract, I have already tried cURL but it doesn't work
api.php (not included login variables)
$dblink = new mysqli($servername, $username, $password, $dbname);
if ($dblink->connect_errno) {
printf("Failed to connect to database");
exit();
}
$result = $dblink->query("SELECT * FROM updates LIMIT 3");
$dbdata = array();
while ( $row = $result->fetch_assoc()) {
$dbdata[]=$row;
}
echo json_encode($dbdata);
and this is what I see on api.php, the JSON is corrected echod i just can't access it.
[{"id":"1564343527","title":"title","type":"Server","overview":"overview","added":"a:2:{i:0;s:6:\"added1\";i:1;s:6:\"added2\";}","removed":"a:2:{i:0;s:8:\"removed1\";i:1;s:8:\"removed2\";}","changed":"a:1:{i:0;s:0:\"\";}","date":"2019\/07\/28","time":"09:52:07pm"}]
get.php
<?php
$strJsonFileContents = file_get_contents('api.php');
var_dump($strJsonFileContents); // show contents
?>
error (on get.php)
string(610) "connect_errno) { printf("Failed to connect to database"); exit(); } $result = $dblink->query("SELECT * FROM updates LIMIT 3"); $dbdata = array(); while ( $row = $result->fetch_assoc()) { $dbdata[]=$row; } echo json_encode($dbdata); ?> "
Expected result is for it to echo the page (api.php) so i can pick apart the JSON, actual result is an error.
Not sure about the extent of your code, but I think this can be done simply by including api.php in get.php. There is no need to read the contents of api.php to parse the data.
get.php:
<?php
require 'api.php';
// api.php is included so $dbdata is in scope here
var_dump(json_encode($dbdata));
I am creating a website where a user logs in, enters information that is submitted to a database, and then can call information from the database that will be inserted into an html text element. I have separate html files for each website page, and multiple php files. They are all contained and linked properly on the same server, under the same FTP account. EDIT: THIS WAS MY PROBLEM. I HAD SEPARATE PHP AND HTML FILES. ONCE I COMBINED THEM, I NO LONGER NEEDED THE JAVASCRIPT AND EVERYTHING WORKED FINE. My code has been checked by multiple sources, and I can have the user correctly input information into the database. When it comes to retrieving the information to be displayed on client-side, however, the php will not change the html via javascript.
I am currently using 000webhost.com, but I tried plugging the files into dreamweaver and the files still aren't communicating properly. I'm wondering whether this is a software limitation (aka something is going wrong with 000webhost.com) or if there is a key piece of info I am missing, such as a command that will link the files together.
Here is the example php and html for one of the retrieve information functions:
$servername = "localhost";
$username = "-";
$password = "-";
$database = "-";
$link = mysqli_connect($servername, $username, $password, $database);
if (!$link) {
die(mysqli_error());
}
$sql = "SELECT UserClassName FROM UserInfo1 WHERE UserEmail = '". mysqli_real_escape_string($link, $_SESSION['useremail']) . "'";
if (!$sql) {
die(mysqli_error());
}
$result = mysqli_query($link, $sql);
if (!$result) {
die(mysqli_error());
}
?>
<script>
function getclassname() {
return document.getElementById("UserClassName").innerHTML = "<?php echo
$result ?>";
}
</script>
<html>
<body onload="getclassname(); getcurrentlevel();">
<h1 class="text-center" id="UserClassName" name="UserClassName"><b>Welcome Enviroquest Research Team!</b></h1>
</body>
</html>
I think the only problem is that you put the javascript code outside the html. It should be placed before the closing body tag.
This is what your html should look like :
<?php // your php code here ?>
<html>
<body onload="getclassname(); getcurrentlevel();">
<h1 class="text-center" id="UserClassName" name="UserClassName"><b>Welcome Enviroquest Research Team!</b></h1>
<script>
function getclassname() {
document.getElementById("UserClassName").innerHTML = "<?php echo $result; ?>";
}
function getcurrentlevel() {
// this function should be remove if not used
}
</script>
</body>
</html>
my php web service is not giving me json result from my database when i run it in web browser or i try to connect to it from my android app. When i test it through postman it gives me back results i json.
Here is my php code:
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
include 'connection.php';
showStudent();
}
function showStudent()
{
global $connect;
$query = " Select * FROM demo; ";
$result = mysqli_query($connect, $query);
$number_of_rows = mysqli_num_rows($result);
$temp_array = array();
if($number_of_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$temp_array[] = $row;
}
}
header('Content-Type: application/json');
echo json_encode(array("demo"=>$temp_array));
mysqli_close($connect);
}
?>
here is my result from database through postman:
whil in web browser im not getting any of this:
can anybody see any problem with this, im actually not a php developer i tried to wrote this on my own for a project.
Thanks
You are checking for the method of submission first which is POST. while when you hit direct url it work as GET. So for getting data in web change POST to GET.
<?php
if($_SERVER["REQUEST_METHOD"]=="GET"){
include 'connection.php';
showStudent();
}
I want after register the page should continue with session. for that i have create following code.
if($_POST['action']=='finder_quick_reg')
{
extract($_POST);
print_r($_POST);
$result=$dbg->exec("INSERT INTO `login_register`(`email`, `password`, `reg_date`, `complite_register`, `type`) VALUES ('$client_mail','$client_password', now(),'0','c_finder')") or die("Insert Failed ".mysql_error());
$lastId = $dbg->lastInsertId();
$results=$dbg->exec("INSERT INTO `care_finder`(`postal_code`,`user_pic`, `login_id`,`f_name`, `l_name`, `gender` ) VALUES ('$postal_code', 'default.jpg','$lastId', '$f_name', '$l_name', '$gender')")or die("Insert Failed ".mysql_error());
}
else{echo 'error';}
if($results){
include_once 'connection.php';
$con=new connection();
$dbg=$con->db;
$sql="SELECT * FROM `login_register` WHERE `login_id`='$lastId'";
$stmt=$dbg->query($sql);
$rows=$stmt->fetch(PDO::FETCH_ASSOC);
session_start();
//echo $lastId;
$_SESSION['Uname']=$rows['email'];
$_SESSION['pword']=$rows['password'];
$_SESSION['Utype']=$rows['type'];
$_SESSION['Uid']=$rows['login_id'];
}
after this db.php page it will go to user_index.php
following code is using in user_index.php
<?php
session_start();
if (!isset($_SESSION['Uname'])) {
//header("location:index.php");
echo 'no sesseion';
}
else {
......
......
}
can some one help me
You're outputting before starting the session
if($_POST['action']=='finder_quick_reg')
{
// ...
print_r($_POST);
// ...
}
else { echo 'error'; }
And from the title I'd assume that error_reporting is turned off on the production server, otherwise you'd have seen an error message along the lines of
Warning: Cannot modify header information - headers already sent...
You should start the session exactly like in you're doing in your other file - at the very beginning of it, before all other statements.
On a side note, have a look at the point notes in the documentation for export() function, there it's written why/that it's a bad idea to use it on untrusted data.
session_start();
should come from login page on.
I'm having trouble passing two variables into an included file.
Viewable page:
<?php
$sqlCount = "This is a working MySQL query.";
$sqlQuery = "This is also a working MySQL query.";
include("include.php");
?>
include.php:
<?php
$result = mysql_query($sqlCount, $conn) or trigger_error("SQL", E_USER_ERROR);
//$result populates variables to be used in second portion of code
//don't need $result anymore
$result = mysql_query($sqlQuery, $conn) or trigger_error("SQL", E_USER_ERROR);
//rest of my code
?>
The code works in the viewable PHP page when I don't include it. It also works when I move $sqlCount and $sqlQuery into include.php.
The only real solution I've been able to find was to declare them as global variables, but when I tried it didn't do anything.
Edit: I figured it out. $sqlQuery had variables inside the query that weren't being created until inside the include.php file.
did you try
include.php:
<?php
echo $sqlCount; die(); // just to be sure you have what you expect?
$result = mysql_query($sqlCount, $conn) or trigger_error("SQL", E_USER_ERROR);
A better approach, in my point of view would be rewriting the code in "include.php" as a function that you can then call from your "viewable page":
include.php
<?php
function included_function($sql_count, $sql_query)
{
$result = mysql_query($sql_count, $conn) or trigger_error("SQL", E_USER_ERROR);
//$result populates variables to be used in second portion of code
//don't need $result anymore
$result = mysql_query($sql_query, $conn) or trigger_error("SQL", E_USER_ERROR);
//rest of my code
}
?>
"viewable page"
<?php
include("include.php");
$sqlCount = "This is a working MySQL query.";
$sqlQuery = "This is also a working MySQL query.";
included_function($sqlCount, $sqlQuery);
?>