wordpress template page error when select from database - php

I have take the page.php page and i have created a new template page named mytemplatepage.php
The page mytemplatepage.php works normally untill i try to add an sql query. when i add the bellow code the page gives http 500 error.
<?php
global $wpdb;
$sqlresults = $wpdb->get_results(
"SELECT id, CategoryName
FROM wp_SimParts"
);
?>
if i remove the code and just leave
<?php
?>
the page load normally. if i put an echo on the php code the page again crashes with http 500 error
<?php
echo "hello there";
?>
--EDIT Error Log--
I am taking this error from logs
PHP Parse error: syntax error, unexpected 'my_template_page' (T_STRING) in

I think you need to try like this.
You write below method on your template file.
function my_template_page() {
global $wpdb;
$query = "SELECT id, CategoryName FROM wp_SimParts";
$pageposts = $wpdb->get_results($query, OBJECT);
$page = (array) $pageposts ;
if(!empty($page)) {
return "Not Empty";
} else {
return "Empty";
}
}
echo my_template_page();
If you get error then either you did mistake on header file content-type OR you need to update your WordPress. Your template file is fine.
Thanks

error 500 is an internal server error, check all you closing tags in your mytemplatepage.php template page, also check your error log on the server.
It would help it you copy paste the error here

Related

Declare 404 error even if the page does exist

I'm trying "CheckBot" and I have an observation. The problem is that in my PHP code I have an if to check if the URL exists in a table called "seo_url" if it does not exist then we show the content of error.tpl
<?php
require_once 'app/config.php';
$url = isset($_GET["url"])?$_GET["url"]:'index';
$querytcseos = DB::queryFirstRow("SELECT * FROM tc_seos WHERE seo_url = %s LIMIT 1", $url);
require_once 'includes/header.tpl';
if (!$querytcseos['seo_file']) {
include 'includes/error.tpl';
} else {
include 'includes/'.$querytcseos['seo_file'].'.tpl';
}
require_once 'includes/footer.tpl';
?>
I see that this does not seem to be the right thing to do since "CheckBot" when you enter example.com/page-not-fount if you are finding content.
How do I get my error page taken as error 404?
http_response_code(404);
For best practice. You should respond it like that and let the server (Apache/Nginx/IIS) to handle the 404 page.

Variable via GET not working in wordpress

I am carrying a variable via $_GET to a page which processes the action, the variable carries over correctly but the database won't update and throws an error.
Here is my link with a varible;
echo "<td><a href='".plugins_url()."/myremovalsquote/inc/disable.php?id=".$active_partner->partner_id."' class='button-primary'>Disable</a></td>";
I then use the variable passed in my /disable.php
$id = $_GET['id'];
echo $id;
global $wpdb;
if ($commit = $wpdb->query("UPDATE partners SET active='no' WHERE partner_id='"'$id'"'")) {
echo 'Success';
} else {
echo 'Failed';
}
The echo outputs the correct string, but then I get this error message.
77
Fatal error: Call to a member function query() on a non-object in /home/myremovalsquote/public_html/wp-content/plugins/myremovalsquote/inc/disable.php on line 9
You are calling a PHP file directly without loading WordPress, so $wpdb and it's method aren't available to you.
You can fix this by including wp-load.php, however this is generally considered to be bad form, as explained in Don't include wp-load please.
A better solution would be to create an AJAX listener, and then call that from your link (it doesn't really have to be a JavaScript request or JSON response). You would need to pass in your action to call the right PHP method, along with your other variables.
// attach action "handler_33762965" to the handler_33762965() function for admin (wp_ajax_*) and front end (wp_ajax_nopriv_*)
add_action( 'wp_ajax_handler_33762965', 'handler_33762965' );
add_action( 'wp_ajax_norpriv_handler_33762965', 'handler_33762965' );
function handler_33762965(){
// make sure the ID is set before grabbing it
$id = isset( $_GET['id'] )? $_GET['id'] : 0;
// do stuff
exit();
}
Your link would look like the following.
link

basic php: redirect page using page id

I want to redirect if form update successfully, the page i edit
example:
<?php
header("Location: update_lbctn.php?order=2")
?>
my problem is in example above order=2 is dynamic it change depend on page ID
so I try like this
<?php
header("Location: update_lbctn.php?order=" . echo urlencode($current_id['id']) ." " ")
?>
but give me error: Parse error: syntax error, unexpected 'echo' (T_ECHO)
Try this -
header('Location:update_lbctn.php?order='.urlencode($current_id['id']));
exit;
There is no need to add the echo. Just concatenate the id.
In your Code you have used extra quotes. Just Put the following code. echo is not required while using header redirect as it is, itself a php function
header("Location: update_lbctn.php?order=" .urlencode($current_id['id']))
The header function will echo out the inner content so no need to write echo again.
For this you can write
<?php
header("Location: update_lbctn.php?order=".urlencode($current_id['id']);
?>
All you need to do is pass a string to the Location.
And with your code, it will redirect to the location specified by the string, not to print it.
So, in your code:
<?php
header("Location: update_lbctn.php?order=" . echo urlencode($current_id['id']) ." " ")
?>
The echo is unnecessary.
Why do it is giving syntax error:
Its because, you are putting echo just after the dot ..
echo is expected to come at the new line where other line ends or at the first line.
In short either after semi-colon : or right in the beginning of the file.
e.g.
$test = 'gg';
echo $test;
or
echo $_GET['DUMMY_VAR_FOR_TESTING'];
So, its producing syntax error.

Data wasn't able to retrieve from the previous page (PHP and MYSQL)

I researched here in stackoverflow trying to find whether someone is also encountering the same problem. I know it's kind of easy and even I really don't know what's the error because there's no problem with my query.
On the previous page, here's my code to retrieve the ID Number so I'll be able to select the data with that ID number:
<?php echo $row['place_name'];?>
I tried first to print the value of the place id and it works fine.
But when it was being called to the Package page, the data I want to show weren't displayed.
I look at the URL and it shows this after the package.php
place_id=
I don't know why it is blank, please check my code if there's missing or just wrong.
In my package page, here's the PHP code:
<?php
include("common/connect.php");
$place_id = $_GET['place_id'];
$result = mysql_query("SELECT * FROM package_items WHERE place_id = '$place_id'");
$row1 = mysql_fetch_array(mysql_query("SELECT place_name FROM packages WHERE place_id = '$place_id'"));
if($result === FALSE) {
die(mysql_error()); // for better error handling
}
?>
In HTML Code:
<h1><?php echo $row1['place_name'];?></h1>
<?php while($row=mysql_fetch_array($result)) {?>
<?php echo $row['item_title'];?>
<br>
Back
<?php } ?>
Please check my codes. Thanks.
You are not printing it.
Change
<?php $row['place_id'];?> // It will output nothing as no echo or print.
To
<?php echo $row['place_id'];?>
Rest of the code looks fine.
Three suggestions:
1)
$place_id = $_GET['place_id'];
Change to
$place_id = ! empty($_GET['place_id']) ? $_GET['place_id'] : ''; // To avoid any warning.
2) Don't feed variable from $_GET or $_POST to any SQL.
3) Don't use mysql_ functions as they are deprecated and will be remove in future versions of PHP.

PHP Session Function Debug Assist

I know how to include external PHP pages and how to start sessions etc, but I think there is something messed up with my logic on what I am working on. Hoping someone could take a look...
I have an html page that is a form that pulls up a PHP view page with the info it sends to it. I wanted to put my function in an external page, along with using sessions, but I keep getting a syntax error.
When I send my form it goes to the following:
<?php
session_start();
include 'functs.php';
if ($_POST && !empty($_POST['name'])) {
$_SESSION['name'] = $_POST['name'];
$_SESSION['time'] = $_POST['time'];
confirmed();
}
else {
print unconfirmed();
}
?>
My external page with the functions is this:
<?php
function confirmed() {
echo "<head>";
echo "<title>Confirmation Page</title>";
echo '</head>";
echo "<body>";
PRINT <<<HERE
if (isset($_SESSION['name'])) {
echo 'Thank you, '.$_SESSION['name']. ' your reservation is confirmed for ' . $_SESSION['time'] ;
}
else {
echo 'There seems to have been an error processing your reservation. Please ensure that you have cookies enabled and try your request again' ;
}
HERE;
echo "</body></html>";
?>
The error I am getting is Parse error: syntax error, unexpected 'name' (T_STRING), expecting ',' or ';' in E:\Program Files\xampp\htdocs\cis\w2\functs.php on line 10. If I insert the function internally, it works, so I know its something with how I am formatting the include page.
It's pretty obvious via the syntax highlighting what is wrong here:
echo '</head>";
//-----------^
This line has the incorrect quote mark, thus you never terminate the string, and it keeps going.
Edit:
But that isn't the only problem. You also never close your function with a right curly bracket: }.
The main problem is that you have mismatched quotes on this line:
echo '</head>";
However, I have to say I'm confused as to why you have the HEREDOC. Surely you just need the if statement alone?

Categories