Unable to forward $_GET variable target PHP file [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I am working on homework assignment that requires making of a simple news site with the ability to list news from different categories, to allow logged in users to comment articles and delete them.
I managed to do all of the mentioned except deletion. I get this error message "Parse error: syntax error, unexpected 'if' (T_IF) in C:......\delete.php on line 4".
Here is my show_comments.php file code:
<?php
require_once("konektor.php");
$article_id = $_GET['id'];
$qCommentShow = "
SELECT
`komentari`.`id` as `kom_id`,
`komentari`.`komentar`,
`komentari`.`korisnik_id`,
`komentari`.`sajt_id`,
`korisnici`.`username`
FROM `komentari`, `korisnici` WHERE `sajt_id` = $article_id
GROUP BY `komentari`.`id`
ORDER BY `komentari`.`id` DESC ";
$komentarPrikazi = $konektor->query($qCommentShow);
echo "<h3>Komentari korisnika</h3>";
$sviKomentari = $komentarPrikazi->fetchAll(PDO::FETCH_OBJ);
foreach($sviKomentari as $b){
echo "<div style='margin:30px 0px;'>
<p>Ime: ". $b->username ."</p><p>Komentar:</p><textarea> ". $b->komentar ."</textarea><br>
</div>";
echo "<a href ='index.php?opcija=delete&id=". $b->kom_id ."'>Izbrisi komentar</a>";
echo "<hr>";
}?>
and my delete.php file code:
<?php
require_once("konektor.php")
if (isset($_GET['id'])){
$qBrisi = "
DELETE FROM `komentari` WHERE `id` = '" .$_GET['id']. "'; AND `korisnik_id` =
'" .$_SESSION['id']. "';
";
$komentarBrisi= $konektor->query($qBrisi)
}
else {
echo "error";
include ("location: kategorije.php");
}
?>
If required, I can also post my category.php code where I list all the categories with established dynamic paging.
I have tried looking around the internet what I could be doing wrong but everywhere I checked, the code is almost identical to mine so I am really scratching my head here what I am doing wrong.
P.S I know my code is still unfinished and vulnerable to attacks but right now, I have to make a really basic and functional version of the site.

You forgot ; on line 4 in delete.php :
require_once("konektor.php")

Related

Form doesn't send properly [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 4 years ago.
I've been trying to make a form in Wordpress to post data to the database and check if the data matches and if so then it proceeds to the next form but I can't seem to get it to work properly, been testing having the form placed in the themes folder to allow straighter testing, the php looks like this:
<?php
if(isset($_POST['submit'])) {
global $wpdb;
$ordernumber = $_POST['ordernmbr'];
$orderfirstname = $_POST['firstname'];
$orderpostnumber = $_POST['postnmbr'];
// Sanitizing
$ordernumber = stripslashes_deep($ordernumber);
$orderfirstname = stripslashes_deep($orderfirstname);
$orderpostnumber = stripslashes_deep($orderpostnumber);
$sql = "SELECT * FROM wp_postmeta WHERE 'post_id' = %d";
$sql = $wpdb->prepare($sql, array($ordernumber));
$res = $wpdb->get_results($sql);
if ($res > 0) {
wp_redirect(admin_url('http://localhost/wordpress/index.php/shop/'));
die();
} else {
$error = "Not like this";
echo $error;
}
print_r($res);
}
?>
<?php
get_footer();
?>
}
The problem is that when I try to post data it gives me an error saying
Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/wordpress/wp-includes/class.wp-styles.php:225) in /Applications/XAMPP/xamppfiles/htdocs/wordpress/wp-includes/pluggable.php on line 1219
how do I go on about fixing this?
You can't redirect the browser after the content has already begun being served.
That piece of code needs to be included before the theme starts loading, so it needs to be in a plugin.
When setting up the plugin, be sure to give it a high priority, and choose the right filter/action to have it load early (before any content is rendered): WP Action Reference

PHP Simple Post - Post Parameters empty [duplicate]

This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
How do I get PHP errors to display?
(27 answers)
Closed 4 years ago.
I am making a simple review application. The below image is my DB schema:
Here is the PHP I am using to accept the post request:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = ($_POST["name"]);
$title1 = ($_POST["phone"]);
$title2 = ($_POST["myphone"]);
// check if name only contains letters and whitespace
$image = ($_POST["stars"]);
// check if name only contains letters and whitespace
mysql_query("insert into `PeerReview`
( `ReviewerPhoneNumber`, `RevieweePhoneNumber`,
`RevieweeName`, `Comments`, `Stars` )
values ( '".$title2."', '".$title1."', '".$name."', '".$image."', '".$image."')");
if (mysql_error()) {
die(mysql_error());
echo '
<div class="post" style="border-bottom:1px black solid;">
<h3>Thank You!</h3>
</div>
</body>
</html>';
die();
}
}
I am using Postman to send the post request:
The result is a new row being added to my DB with an Auto-incremented ID, but blank values for every other column. This is incorrect because as you can see in Postman I am accurately adding the parameters. I have made many PHP applications before using this same technology stack. I would appreciate any direction.

PHP - Check if user is an admin or not [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
How to include php file in a php file
(1 answer)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 5 years ago.
So i have a page where there is a button when clicked it takes u to an admin page.
if u click it first takes u to a admincheck code before bringing u to the adminpage. In that admincheck page it should let admins go through and normal users redirected back. It doesnt work i dont know why
here is the admincheck code:
<?php
include "dbconnection.php";
include "login.php;"
$query = "SELECT * FROM gebruikers WHERE gebruikerID = $userid";
$result = mysqli_query($conn, $query);
$count = $conn->affected_rows;
if($row['admin']=='Ja'){
header("location: contactregister.php");
}
else{
$message = "You shall not pass!";
echo "<script type='text/javascript'>alert('$message');</script>";
header("location: home.php");
}
?>
Did i do anything wrong in that code??

Updating data Php pdo [duplicate]

This question already has answers here:
Why does this PDO statement silently fail?
(2 answers)
Closed 6 years ago.
Im trying to update my data using php but it doesnt work, any ideas?
This is the code, this isnt the full code (its not done) but even the username cant be updated.
<?php
session_start();
include "dbconfig.php";
require "check.php";
if(!empty($_POST['user_name']) || !empty($_POST['user_email'])){
$user_name = trim($_POST['user_name']);
$user_email = trim($_POST['user_email']);
$count=$db_con->prepare("SELECT * FROM users WHERE user_id=:userid");
$count->bindParam(":userid",$_SESSION['user_session'],PDO::PARAM_STR,15);
$count->execute();
$row = $count->fetch(PDO::FETCH_OBJ);
$sql=$db_con->prepare("update users set user_name=:username where user_id='$row->user_id'");
$sql->bindParam(':username',$user_name,PDO::PARAM_STR, 32);
if($sql->execute()){
echo "Successfully updated Profile";
}
else{
print_r($sql->errorInfo());
}
else {
echo "No data inserted!"
}
include "home.php";
?>
I guess a syntax error in this Line
$sql=$db_con->prepare("update users set user_name=:username where user_id='$row->user_id'");
Corrected
$sql=$db_con->prepare("update users set user_name=:username where user_id="$row->user_id);

Cannot modify header information - related to MySQL query [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
I fixed this problem, but since I don't understand what caused it, I can't be sure it's really fixed.
My PHP site displays the latest activity on the home page, before you log in. I recently modified the logic to include more types of activity. It looked like it worked, but I got the following error upon logging in:
Warning: Cannot modify header information - headers already sent by
(output started at header.php:75)
in index.php on line 26
I think this error message is misleading, because the way I fixed it was by changing "LIMIT 10" to "LIMIT 9" in the MySQL query that gets the activity to be displayed on the home page.
public function getLatestActivity()
{
$sql = "SELECT 'Debate' AS Type, d.Debate_ID AS ID, CONCAT('debate.php?debate_id=', d.Debate_ID) AS URL, d.Title, d.Add_Date
FROM debates d
UNION SELECT 'Discussion' AS Type, d.Discussion_ID AS ID, CONCAT('discussion.php?discussion_id=', d.Discussion_ID) AS URL, d.Title, d.Add_Date
FROM discussions d
UNION SELECT 'Petition' AS Type, p.Petition_ID AS ID, CONCAT('petition.php?petition_id=', p.Petition_ID) AS URL, p.Petition_Title AS Title, p.Add_Date
FROM petitions p
ORDER BY Add_Date DESC
LIMIT 9";
try
{
$stmt = $this->_db->prepare($sql);
$stmt->execute();
$activity = array();
while ($row = $stmt->fetch())
{
$activity[] = '<span style="font-size: x-large"><strong>Latest activity</strong></span><br /><span style="font-size: large">' . $row['Type'] . ': <span style="color: #900">' . $row['Title'] . '</span></span>';
}
$stmt->closeCursor();
return $activity;
}
catch(PDOException $e)
{
return FALSE;
}
}
And here's what I'm doing with the data returned by that function. It loops through the array and shows a new item every 4 seconds.
<?php $latest_activity = $pdb->getLatestActivity(); ?>
<script type="text/javascript">
var activity = <?php echo json_encode($latest_activity); ?>;
var index = -1;
$(function()
{
getLatestActivity();
});
function getLatestActivity()
{
index = (index + 1) % activity.length;
var div = document.getElementById('divLatestActivity');
if (index < activity.length)
{
div.innerHTML = activity[index];
}
setTimeout("getLatestActivity()", 4000);
}
</script>
Why did changing "LIMIT 10" to "LIMIT 9" fix the "cannot modify header information" problem?
In PHP, if you use the header() function (i.e. header("Location:login.php"); to redirect to the login.php page), you must do it before any other code that might output text to the browser.
//this will CAUSE a warning
echo "Login now";
session_start()
header("content-type:text/html");
header("cache-control:max-age=3600");
However...
//this will NOT CAUSES a warning
header("content-type:text/html");
header("cache-control:max-age=3600");
session_start();
echo "Login now";
So comb through your code that executes any session_start() or header() directives and make sure there are no echo ""; before them. If you have any MySQL warnings that are thrown out onto the page before your session_start() or header() that will also cause this warning.
You need to check if some output has been done in the form of echo, print or HTML. If this has been done, then header("LOCATION: login.php") will throw an error.
A way to over come this is using output buffering.
ob_start();

Categories