Not getting anything from GET request - php

if (isset($_GET['id'])) {
$movie_id=$_GET['id'];
$sql="SELECT * FROM movies WHERE movie_id='$movie_id'";
$run_sql=mysqli_query($connect,$sql);
while ($row=mysqli_fetch_assoc($run_sql)) {
$id=$row['movie_id'];
$name=$row['Name'];
$desc=$row['Description'];
echo "<div id='breadcrumb'><a href='index.php'>Home</a> > <a href='movies.php'>Movies</a> $name</div>
<div id='description'>
<h1>$name</h1>
<p>$desc</p>";
}
}
this is the code and its shows nothing except the url ID which is passing through GET no other details are showing no errors either ,

The main issue is with the URL.
Your URL is single.php?movie=3 and you're checking for $_GET['id']. So change your id by movie.
For the future, you have to code that is SQL Injection free and follow the Instruction given by #RiggsFolly.

In these situations you should add some error checking to your code. In fact you should always be error checking
<?php
// some people develop on LIVE servers so make sure
// all error reporting is on while developing a script
ini_set('display_errors', 1);
ini_set('log_errors',1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
function single_page(){
global $connect;
if (isset($_GET['id'])) {
$movie_id=$_GET['id'];
$sql="SELECT * FROM movies WHERE movie_id='$movie_id'";
$run_sql=mysqli_query($connect,$sql);
if ( ! $run_sql ) {
// output the error returned by the database
echo 'SQL ERROR: ' . mysqli_error($connect);
exit;
}
while ($row=mysqli_fetch_assoc($run_sql)) {
$id=$row['movie_id'];
$name=$row['Name'];
$desc=$row['Description'];
echo "<div id='breadcrumb'><a href='index.php'>Home</a> > <a href='movies.php'>Movies</a> $name</div>
<div id='description'>
<h1>$name</h1>
<p>$desc</p>
</div>";
}
}
}
This should at least tell you what the error is, if in fact it is generating an error in the compilation or execution of the query.

Related

Error not clarifying what is keeping my program from parsing

So I am running an error check, since my edit post function is not working, and this is the message I am getting
Fatal error: Uncaught mysqli_sql_exception: Unknown column 'trewfh' in 'field list' in C:\xampp\htdocs\Final\Forum\editpost.php:55 Stack trace: #0 C:\xampp\htdocs\Final\Forum\editpost.php(55): mysqli_query(Object(mysqli), 'UPDATE hw7_foru...') #1 C:\xampp\htdocs\Final\Forum\editpost.php(39): editpost(72, 'trewfh') #2 {main} thrown in C:\xampp\htdocs\Final\Forum\editpost.php on line 55
What a confusing error message! It is echoing out exactly what I want it to (the id =72 and the random string =trewfh) however nothing occurs in terms of updating my post. The $connectDB variable comes from including my connection file and universally works everywhere with no issues. I was hoping someone could see what I am not. Thanks for your time and assistance!!!
<?php
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(-1); mysqli_report(MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX);
include 'header.php';
include 'dbconnect.php';
//GATHER MY POST THAT MATCHES THE POST THE USER CLICKS TO EDIT AND DISPLAY TO THEM!
$sql = "SELECT forumpost_ID, forumpost_Text
FROM hw7_forumpost
WHERE forumpost_ID =" . mysqli_real_escape_string($connectDB, $_GET['eid']);
$result = mysqli_query($connectDB,$sql);
if(!$result){ echo 'Something went wrong, please try again later.'; }
else{
while($row = mysqli_fetch_array($result)){
echo'<form action="" method="post">';
echo'<div>';
echo'<textarea name="contents" rows="15" cols="50">'.$row[1].'</textarea>';
echo'</div><div>';
echo'<input type="submit" value="Add Post">';
echo'</div>';
}echo '</form>';
}
if(isset($_POST['contents'])){
$contents = trim($_POST['contents']);
editpost($_GET['eid'], $contents);
echo ('Your post has been updated!');
header('index.php');
die();
}
include 'footer.php';
/********************************************
edit function
**********************************************/
function editpost($id, $contents){
global $connectDB;
$id = (int)$id;
$contents = mysqli_real_escape_string($connectDB, $contents);
$sql = "UPDATE hw7_forumpost SET forumpost_Text=".$contents." WHERE forumpost_ID= ".$id;
$result = mysqli_query($connectDB, $sql);
}
?>
My update should be saying hey database Update hw7_forumpost SET forumpost_Text = 'Trewfh' WHERE forumpost_ID = The correct id it's Getting via $_GET. Its reading everything fine like I said when it's echoing. Something random is happening along the way through
"UPDATE hw7_forumpost SET `forumpost_Text` = -->'<--".$contents."-->'<-- WHERE `forumpost_ID` = ".$id;
It was a simple case of forgetting to place a ' ' in between calling contents as David has pointed out. Thank you David!!!!

PHP preg_match() steam link validation error

What's wrong with this preg_match() usage? I want to check steam lobby link and if it's matching then write to database. If not, just echo the error. I am doing this through ajax. Is it better to do this with ajax or $_SERVER["REQUEST_METHOD"] == "POST"?
<?php
require("../includes/config.php");
$lobby = "steam://joinlobby/730/109775243427128868/76561198254260308";
if (!preg_match("%^((steam?:)+(/joinlobby\/730\/)+([0-9]{17,25}\/.?)+([0-9]{17,25})/$)%i", $lobby)) {
echo "Lobby link isn't formatted correctly.";
}
else {
$rank = "Golden";
$mic = "No";
try {
$stmt=$db->prepare("INSERT INTO created_lobby (lobby_link, current_rank, have_mic) VALUES (:lobby_link, '$rank', '$mic')");
$stmt->execute(array(
':input_link' => $_POST['lobbyLink']
));
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
?>
My Problem:
When I execute this code, it will give me false.
Thank you for help.
This works:
$lobby = "steam://joinlobby/730/109775243427128868/76561198254260308";
if (!preg_match("%^(steam?:)+(//joinlobby/730/)+([0-9]{17,25}/.?)+([0-9]{17,25}$)%i", $lobby)) {
echo "Lobby link isn't formatted correctly.";
}
I changed /joinlobby to //joinlobby, and remove the / at the end. I also removed the unnecessary () around everything.
I suspect you also shouldn't have (...)+ around steam?: and //joinlobby/730/. They'll cause repeated uses of those prefixes to be accepted as correct, e.g. steam:steam:...

Use ISSET from some of the examples here on stackoverflow Error on the Code

I Dont think I wrote the code correctly. Can someone look at this? I am new to php!
<?php
// Create Connection
$connect = mysqli_connect('localhost','root','test123','joomla');
// Check Connection
if(mysqli_connect_errno($connect)) {
echo 'Failed to connect to DataBase| '. mysqli_connect_error();
}
?>
I added if(issets($_GET['rp_id'])) to the if rp_id does not exist in the url parameter - it just closes the database
<?php
if(issets($_GET['rp_id]')) {
$rp_id = htmlspecialchars($_GET["rp_id"]);
// open record with evdet_id =$rp_id
$result = mysqli_query($connect,"SELECT * FROM n0dap_jevents_vevdetail WHERE evdet_id =".$rp_id);
while($row = mysqli_fetch_array($result)):
$value = $row['google_map'];
echo $value;
endwhile;
}
$connect->close();
//if the rp_id does exist the go ahead and run the top otherwise close it.
} else {
$connect->close();
}
?>
if(issets($_GET['rp_id]))
Looks like you made a typo on isset().
Call to undefined function issets()
This means you're trying to call issets() as a function, which it is not.
The function that you are looking for is isset(). If you replace issets() with isset(), you will not longer get that error message.

PHP Function produces empty Results no errors

A little help if possible. I have a Page that pulls from two data tables (MySQL) and one function is providing empty results.
function ShowClient() {
global $agent;
$sql = 'SELECT * FROM nuke_bulletins WHERE user=\'' . $agent . '\' AND isActive="Y" ORDER BY id';
$client = mysql_query($sql) or die('ERROR: OOPS Something went wrong' . mysql_error());
echo '<center><p><b>Current Campaigns</b></p>';
// Pull the loop and display the data
while($row = mysql_fetch_array($client)) {
$agent = stripslashes($row['user']);
$campaign = stripslashes($row['id']);
$title = stripslashes($row['title']);
echo '<p><b>' . $title . '</b></p>';
}
echo '<p>Click the Campaign Title to get the Bulletin Code</p><p> </p>';
echo '<p align="center"><b>Return to All Client\'s</p>';
}
The $agent variable is pulled from a main function that creates a url based on the user ($agent).
What am I doing wrong here?
$agent is a global variable. Using global variables is generally considered bad practice, as this could be getting set or unset somewhere before this function is called.
Have you checked the PHP Error Log to see if you are getting any errors?
If no errors in log I would look to see if $agent conatins a value either by echoing to screen (if dev environment) or dumping the value in the error log file to see if it actually contains anything. http://www.php.net/manual/en/function.error-log.php
Then I would look at the SQL itself; do the Column headings in your table nuke_bulletins match the $row array keys exactly for instance are they the same case?
$row['title']
or
$row['Title']
Here we go...
Don't use the mysql extension. It is unmaintained and officially deprecated
Don't use globals. Relying on external state makes for smelly code
You're overwriting said global variable ($agent) in a loop. Terrible idea.
or die must die ~ http://www.phpfreaks.com/blog/or-die-must-die
I wouldn't recommend using echo within a function. Makes for spaghetti code
Your HTML is a bit of a mess
Here's my suggestion using the mysqli extension
function getCampaigns(mysqli $con, $agent) {
if (!$stmt = $con->prepare("SELECT id, title FROM nuke_bulletins WHERE user = ? AND isActive = 'Y' ORDER BY id")) {
throw new Exception($con->error, $con->errno);
}
$stmt->bind_param('s', $agent); // if the user column is a integer, use 'i' instead
if (!$stmt->execute()) {
throw new Exception($stmt->error, $stmt->errno);
}
$stmt->bind_result($id, $title);
$campaigns = []; // or array() if you're on PHP < 5.4
while ($stmt->fetch()) {
$campaigns[$id] = $title;
}
return $campaigns;
}
Now you can call this function like this...
<?php
// assuming you have a mysqli instance in a $con variable, eg $con = new mysqli(...)
// and an $agent variable
$campaigns = getCampaigns($con, $agent);
?>
<p><strong>Current Campaigns</strong></p>
<?php foreach ($campaigns as $id => $title) : ?>
<p>
<a href="bullies2.php?op=ShowCampaign&id=<?= $id ?>">
<strong><?= htmlspecialchars($title) ?></strong>
</a>
</p>
<?php endforeach ?>
<p>Click the Campaign Title to get the Bulletin Code</p>
<p> </p>
<p align="center"><strong>Return to All Client's</strong></p>
And, as always, your development environment should have the following properties set in your php.ini file
display_errors = On
error_reporting = E_ALL

Cannot modify header information [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
I am getting the following error from the following code, and I am not entirely sure why. If you could tell me how to fix it, that would be great. Thanks in advanced.
Warning: Cannot modify header information - headers already sent by (output started at...) on line 45.
<?php
// Initialization
$conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
mysql_select_db(DB_NAME, $conn);
// Error checking
if(!$conn) {
die('Could not connect ' . mysql_error());
}
// Localize the GET variables
$ref = isset($_GET['ref']) ? $_GET['ref'] : "";
// Protect against sql injections
// Insert the score
$retval = mysql_query("INSERT INTO $table(
site
) VALUES (
'$ref'
)",$conn);
if($retval) {
echo "Successfull";
} else {
echo "Unsuccessfull " . mysql_error();
}
mysql_close($conn);
?>
<?php
$url = $_GET['url'];
$loc = 'Location: '. $url;
header($loc);
exit;
?>
Take out the echo calls, you can't send information to the browser before the headers.
You can try something like this to still show if an error happens:
if(!$retval) {
echo "Unsuccessfull " . mysql_error();
}
If you change the headers you cannot output any text prior to to the header command otherwise the headers will already be sent.
ie.
if($retval) {
echo "Successfull";
} else {
echo "Unsuccessfull " . mysql_error();
}
Is outputting text before you change the headers.
Use Output Buffers: http://php.net/manual/en/function.ob-start.php
ob_start();
at the start and
ob_end_flush();
at the end.
What I generally recommend for situations like this, is save all output to the end, as gmadd mentioned, you can do the ob_start, but I prefer to store the data in a string without having to add the extra code (I know you can also designate this in the .htaccess file, I would go that route over adding the actual ob_start items).
What I would do:
$display = ""; // initiate the display string
// etc doe here
if($retval) {
$display .= "Successfull";
} else {
$display .= "Unsuccessfull " . mysql_error();
}
// end of the script right before ?>
echo $display;
?>
The ob_start method works and if you want to go that route, you can add this in the .htaccess file (given that allowoverride is set in your apache setup):
php_value output_buffering On
Again I still recommend the $display storage method, but that is my personal opinion.
Use:
<meta http-equiv="Refresh" content="0;url=http://www.example.com" />

Categories