PHP query won't work from HTML href link - php

Am creating a webpage which allows me to search for locations by county. I am trying to create an href link on my basic homepage which will call to a Php function and then display results from my Sql database.
Not sure why but when I click on the href link the homepage just goes blank, no values returned. Have been over code and database, have shown to a few friends and can't figure out what's wrong. Any help appreciated!
This is my index.php main homepage code (with href about halfway down)
<!DOCTYPE HTML>
<?php
include("includes/db.php");
include("getAntrim.php");
include("functions/functions.php");
?>
<html>
<head>
<link rel = "stylesheet" href="styles/styles.css" media = "all"/>
<title>Location Scout</title>
</head>
<body>
<!-- Main container starts -->
<div class ="main_wrapper">
<div id="form">
<form method="get" action="results.php" enctype="multipart/form-data">
<input type="text" name="user_query" placeholder="Search for location"?>
<input type="submit" name="search" value="search"/>
</form>
</div>
<div class ="content_wrapper">
<div id ="left_sidebar">
<div id="sidebar_title">Search by County</div>
Antrim<br></br>
<div id ="content_area">
<div id ="products_box">
<!-- THIS IS WHERE FETCHED DATABASE INFO WILL GO -->
<?php
getAntrim();
?>
</div>
</div>
</div>
<!-- Main container ENDS -->
</div>
</div>
</body>
</html>
This is my getAntrim.php function, which should sort through sql database and then return stated values.
<?php
include ("includes/db.php");
if (isset ($_GET['getAntrim'])) {
$get_loc_co = "select * from locations where county='Antrim'";
$run_loc_co = mysqli_query($db, $get_loc_co); //Gets data from db and presents on main page
$count = mysqli_num_rows($run_loc_co);
if ($count==0) {
echo "<h2>No locations found in this category.";
}//if
while ($row_loc_co=mysqli_fetch_array($run_loc_co)) {
//variable to store values
$loc_id = $row_locations['loc_id'];
$loc_name = $row_locations['loc_name'];
$town = $row_locations['town'];
$county = $row_locations['county'];
$productions = $row_locations['productions'];
$disabled = $row_locations['dis_access'];
$parking = $row_locations['parking'];
$visitor = $row_locations['vis_facs'];
$transport = $row_locations['public_trans'];
$cost = $row_locations['cost'];
$accom = $row_locations['accom'];
$latitude = $row_locations['latitude'];
$longitude = $row_locations['longitude'];
$description = $row_locations['loc_desc'];
$keyword = $row_locations['loc_keyword'];
$loc_image = $row_locations['loc_img'];
echo "
<div id= 'single_location'>
<h3>$loc_name</h3>
<img src = 'Admin_area/location_images/$loc_image' width='180' height = '180'/><br>
<p><b>Productions: $productions </b></p>
<p><b>Description: $description </b></p>
</div>
";
}//while
}//if
?>
Am first time poster, so hopefully have posted this ok! Any advice appreciated.

you're checking for a query parameter, which you're NOT passing in:
Antrim<br></br>
if (isset ($_GET['getAntrim'])) {
^^^^^^^^^
$_GET contains url query parameters, e.g. example.com?foo=bar (the foo=bar portion). Since you have NO query parameters in your href, the isset() properly returns false, and your entire db code section is simply ignored.
You probably want
Antrim<br></br>
^^^^^^^^^^
to make this work as-is.

Related

How to post multiple div data to sql

So I Have The following code, I am trying to post the multiple data in the div (cart) to my sql database,how do I get the right code to post the following to my sql database?
If its not possible for multiple names on the match, how do i get it to insert multiple matches at once?
I am beginner level on php, please help, if you can draft for me even a different but relatable model
most of the answers i find online can only post but this one is a cart so I only need the data from the div to be posted not edited
here is my code
<!DOCTYPE html>
<html>
<head>
<title>Add Records in Database</title>
</head>
<body>
<?php
include "dbConn.php"; // Using database connection file here
if(isset($_POST['submit']))
{
$match = $_POST['match'];
$selection = $_POST['selection'];
$odd = $_POST['odd'];
$totalOdds = $_POST['totalOdds'];
$stakeAmount = $_POST['stakeAmount'];
$payout = $_POST['payout'];
$insert = mysqli_query($db,"INSERT INTO `receipts`(`Match`, `Selection`, `Odd`, `Total Odds`, `Stake Amount`, `Payout`)
VALUES ('$match','$selection','$odd','$totalOdds','$stakeAmount','$payout')");
if(!$insert)
{
echo mysqli_error($db);
}
else
{
echo "Records added successfully.";
}
}
mysqli_close($db); // Close connection
?>
<div class="cart">
<div class="title">Bet Slip</div>
<div id="box" class="boxlit" >
<form action="" method="post">
<div class="box" data-id="4" name="Match">Lanus - Godoy Cruz<div name="selection">Draw</div><div class="crtTotal" name="odd">3.05</div></div>
<div class="box" data-id="8" name="Match">Deportivo Pasto - Envigado<div name="selection">Away</div><div class="crtTotal" name="odd">4.70</div></div>
<div class="box" data-id="9" name="Match">Union Manabita - Emelec<div name="selection">Home</div><div class="crtTotal" name="odd">8.25</div></div>
<div class="box" data-id="12" name="Match">New Jersey Copa - FC Motown II<div name="selection">Home</div><div class="crtTotal" name="odd">3.35</div></div></div>
<br>
<div>Total Odds: <b id="ct1" name="totalOdds" >475.42</b></div><br>
<div>(N$)Stake: <input id="stake" type="number" name="stakeAmount" value="5"> NAD</div><br>
<div>Payout: N$ <b id="payout" name="payOut" >2377.10</b></div>
<input type="submit" name="submit" value="Bet">
</form>
<div class="footer"></div>
</div>
</body>
</html>

Share $_POST variables to another page meaning the main page to the ajax response page?

Here's my situation I have a perfect membership system I realized it's been easier to just use a form with some hidden input with predetermined values created from previous button clicks from previous pages to send the user id and the other user id aka the other member to other pages which the post data becomes variables eventually in each page deeper in each code document which are eventually use in SQL statements. My method has been working but now I have a new problem now I have a private messaging system page where users can send private messages to each other so I found this really cool script which I later semi modify it to work on my membership system but it consist of two main pages that are used to make it work completely the first page consist of pretty much the private messaging system primary components the second page aka chat.php is a AJAX response page designed to show new messages. So how can I share the main page post data variables with the AJAX response page. I tried to put the ajax response page code on the same page of the main page but it looked strange and it gave me strange results. Note in this situation the partner_id is aka the other user id and since the first include of each page consist of the login member id already aka messenger_id it's just the other user id i'm concern of aka the partner_id and just know that the SQL column call message belongs to the messenger_id the partner_id in the SQL database is designed to show who the login member aka messenger_id talked to (partner_id) Here's the code
MAIN PAGE
<?php
include("0/instructions/php/session.php");
$messenger_id = $user_id;
$partner_id= $_POST['partner_id'];
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<meta charset="UTF-8">
<title>Chat System in PHP</title>
<link rel="stylesheet" href="style.css" media="all"/>
<script>
function ajax() {
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState == 4 && req.status == 200) {
document.getElementById('chat').innerHTML = req.responseText;
}
}
req.open('GET', 'chat.php', true);
req.send();
}
setInterval(function() {
ajax()
}, 1000);
</script>
</head>
<body onload="ajax();">
<div class="main_container">
<div id="container">
<div id="chat_box">
<div id="chat"></div>
</div>
</div>
<form method="post" action="">
<input type="text" name="messenger_id" placeholder="messenger_id" value="<?php echo $messenger_id; ?>"/>
<textarea name="message" placeholder="enter message"></textarea>
<input type="text" name="partner_id" placeholder="partner_id" value="<?php echo $partner_id; ?>"/>
<input type="submit" name="submit" value="Send it"/>
</form>
<?php
if(isset($_POST['submit'])) {
$messenger_id = $_POST['messenger_id'];
$message = $_POST['message'];
$partner_id = $_POST['partner_id'];
$query = "INSERT INTO messages_x (messenger_id,message,partner_id) values ('$messenger_id','$message','$partner_id')";
$run = $connect->query($query);
if($run) {
echo "<div id='hide_audio'><embed loop='false' src='chat.mp3' hidden='true' autoplay='true'/></div>";
}
}
?>
</div>
</body>
</html>
THE AJAX RESPONSE PAGE AKA CHAT.PHP
<?php
include("0/instructions/php/session.php");
$messenger_id = $user_id;
$partner_id = $_POST['partner_id'];
$query= "SELECT * FROM messages_x WHERE messenger_id='$messenger_id' AND partner_id='$partner_id' ORDER BY messenger_id DESC";
$run = $connect->query($query);
while($row = $run->fetch_array()) :
$messenger_id = $row['messenger_id'];
?>
<div id="chat_data">
<span style="color:green;"><?php echo $row['messenger_id']; ?></span> :
<span style="color:brown;"><?php echo $row['message']; ?></span>
<span style="float:right;"><?php echo formatDate($row['date']); ?></span>
</div>
<?php
endwhile;
$query_other= "SELECT * FROM messages_x WHERE messenger_id='$partner_id' AND partner_id='$messenger_id' ORDER BY messenger_id DESC";
$run_other = $connect->query($query_other);
while($row_other = $run->fetch_array()) :
?>
<div id="chat_data">
<span style="color:gold;"><?php echo $row_other['messenger_id']; ?></span> :
<span style="color:purple;"><?php echo $row_other['message']; ?></span>
<span style="float:right;"><?php echo formatDate($row_other['date']); ?></span>
</div>
<?php
endwhile;
In this example the hidden form inputs are shown for simple explanation here and as a result I want it to look like this.

Delete button for SQL

I have a member/account site that posts "notes" and saves them in your account. When someone post's a note, each note gets an edit and a delete button. I want the delete button to delete the corresponding post, however its deleting whichever note is on top. I assume the problem lies within either the else when the delete button is pressed or during the while when the button is created. Wouldn't I need a way to assign the buttons an id?
Thanks in advance.
<!-- connections.php connects to the database -->
<?php require 'connections.php'; ?>
<!-- check to make sure the user is logged in,
if not then redirect them to login.php -->
<?php session_start();
if(isset($_SESSION["UserID"])){
} else {
header('Location: Login.php');
die();
}?>
<!-- $result is a query containing all the notes
of the current user -->
<?php $UserID = $_SESSION["UserID"];
$result = mysqli_query($con, "SELECT * FROM notes WHERE UserID = '$UserID'");
?>
<!-- when you click 'save' upload the new note
to the database and refresh the page.
when you click 'delete' remote the note
that goes with that button from the database -->
<?php if(isset($_POST['save'])) {
session_start();
$note = $_POST['note'];
$UserID = ($_SESSION["UserID"]);
$sql = $con->query("INSERT INTO notes (UserID, note)Values('{$UserID}','{$note}')");
header('Location: Account.php');
} else if (isset($_POST['delete'])){
$result = mysqli_query($con, "SELECT * FROM notes WHERE UserID = '$UserID'");
$row = mysqli_fetch_array($result);
$noteID = $row['noteID'];
$UserID = ($_SESSION["UserID"]);
$sql = $con->query("DELETE FROM notes WHERE noteID = '$noteID'");
header('Location: Account.php');
} else if (isset($_POST['edit'])){
}?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>My Account</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<h1 class="titleAct">Welcome</h1>
<form action="" method="post" name="notesubmit" id="notesubmit">
<div>
<textarea name="note" cols="50" rows="4" form="notesubmit" id="noteinput">New Note
</textarea>
</div>
<input name="save" type="submit" class="button" id="save" value="Save">
<!-- Whenever a note is saved, print out the
note with timestamp followed by the edit
and delete buttons for each note -->
<?php while ($row = mysqli_fetch_array($result)): ?>
<?php $note = $row['note'];?>
<?php $date = $row['time'];?>
<div id="note">
<p class="note"><?php echo $date; ?></br> ---------------- </br><?php echo $note; ?></p>
</div>
<input name="edit" type="submit" class="button" id="edit" value="Edit">
<input name="delete" type="submit" class="button" id="delete" value="Delete">
<?php endwhile; ?>
</form>
<div>
<a class="link" href="Logout.php">Logout</a>
<div>
<a class="link" href="Deactivate.php">Deactivate My Account</a>
</div>
</div>
</body>
</html>
You have a logical error... Your not sending back the ID of the note you want to delete anywhere, you just say you want to delete something, then fetches the list of notes from the database, and deletes the first that pops in...
What I would do, is change the delete button to a link, and sett a query parameter ($_GET['noteID']) to select which ID to delete...
Then I would select the NoteID from the database, but also make sure the user tries to delete his own post, and not someone elses...
Good luck :)

PHP delete current 'note' not working

I'm generating a delete button for each 'note' the user creates. However, no matter which delete you click, its deleting the most recently saved note, not the one corresponding to the note. I assume that something is wrong with my hidden field 'deleteID'.
<!-- connections.php connects to the database -->
<?php require 'connections.php'; ?>
<!-- check to make sure the user is logged in,
if not then redirect them to login.php -->
<?php session_start();
if(isset($_SESSION["UserID"])){
} else {
header('Location: Login.php');
die();
}?>
<!-- $result is a query containing all the notes
of the current user -->
<?php $UserID = $_SESSION["UserID"];
$result = mysqli_query($con, "SELECT * FROM notes WHERE UserID = '$UserID'");
?>
<!-- when you click 'save' upload the new note
to the database and refresh the page.
when you click 'delete' remote the note
that goes with that button from the database -->
<?php if(isset($_POST['save'])) {
session_start();
$note = $_POST['note'];
$UserID = ($_SESSION["UserID"]);
$sql = $con->query("INSERT INTO notes (UserID, note)Values('{$UserID}','{$note}')");
header('Location: Account.php');
} else if (isset($_POST['delete'])){
$deleteID = $_POST['deleteID'];
$sql = $con->query("DELETE FROM notes WHERE noteID = '$deleteID'");
header('Location: Account.php');
} else if (isset($_POST['edit'])){
}?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>My Account</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<h1 class="titleAct">Welcome</h1>
<form action="" method="post" name="notesubmit" id="notesubmit">
<div>
<textarea name="note" cols="50" rows="4" form="notesubmit" id="noteinput">New Note
</textarea>
</div>
<input name="save" type="submit" class="button" id="save" value="Save">
<!-- Whenever a note is saved, print out the
note with timestamp followed by the edit
and delete buttons for each note -->
<?php while ($row = mysqli_fetch_array($result)): ?>
<?php $note = $row['note'];?>
<?php $date = $row['time'];?>
<?php $noteID = $row['noteID'];?>
<div id="note">
<p class="note"><?php echo $date; ?></br> ---------------- </br><?php echo $note; ?> </p>
</div>
<input name="deleteID" type="hidden" id="hidden<?php echo $noteID;?>" value="<?php echo $noteID; ?>">
<input name="delete" type="submit" class="button" value="Delete">
<?php endwhile; ?>
</form>
<div>
<a class="link" href="Logout.php">Logout</a>
<div>
<a class="link" href="Deactivate.php">Deactivate My Account</a>
</div>
</div>
</body>
</html>
I am thinking its not the delete at all - but the div with the note in it - each has the same id since they are in the while loop and therefore every note div has the id of "note"
<div id="note">
its should be
<div id="note<?php echo $noteID;?>";
The error seems to be in the name of the input. Should be the same as the value you set in the ID part.
Like this :
<input name="hidden<?php echo $noteID;?>" type="hidden" id="hidden<?php echo $noteID;?>" value="<?php echo $noteID; ?>">
Otherwise, all your inputs have the same name, and it takes the last one to do the query.
EDIT
Put the name of the value you want to delete directly in the submit button like this :
<input name="delete-<?php echo $noteID;?>" type="submit" class="button" value="Delete">
Then you can get the value in $_POST array and explode() the value on - separator to get the ID value.
you also have multiple submit inputs for the one form - one to trigger the save and one each for each echo-ed delete note, so triggering the delete submit function is going to first trigger the save submit - therefore $_POST['save'] will be acted on before the $_POST['delete']. This could be the issue behind the action of altering the last input note rather than the one that was the trigger action.
I would suggest splitting off the add note function to a different form than the delete note functions. You can have the same if else logic and this should work as expected in the new version since you are actually posting the information you are expecting to.

Run different query based on a dynamic HTML dropdown list

I made some PHP code to generate this page. I successfully get all the items from a column into a HTML dropdown list (it's a dynamic list). I want to write some code so that when user selects an item from the list and hit submit, it will take user to a new page contains corresponding information on it. I have no idea what kind of code would be included in. Please help. Thanks!
For instance, if user select 50A-1, it will populate a table has all the items located at 50A-1.
Two pieces of code I wrote, first is the page gives you the dropdown list and the submit button. The second is the result page, but it only shows the whole inventory so far, it doesn't have a way to connect to the dropdown list option.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Inventory</title>
</head>
<body>
<div>
<a>SQL Connection test</a>
<form action="connect.php" method="POST">
<div class="center">
<input type="submit" value="Connect to MySQL" />
</div>
</form>
</div>
<div>
<section>
<article>
<p>
<select name="dropdown">
<?php query() ?>
</select>
<?php close() ?>
</p>
</article>
</section>
<div>
<input type="submit" value="Submit" />
</div>
</div>
</body>
</html>
Second page
<?php
include_once 'db.inc.php';
// connect
function connect() {
// Connect to the MySQL server
mysql_connect(DB_HOST,DB_USER,DB_PASS) or die ('Could not connect to server!' . mysql_error());
mysql_select_db(DB_NAME);
}
// close
function close() {
mysql_close();
}
// query
function query() {
$myData = mysql_query("SELECT DISTINCT * FROM sheet0_100 GROUP BY location");
while($record = mysql_fetch_array($myData)) {
echo '<option value="' . $record['location'] . '">' . $record['location'] . '</option>';
}
}
?>
That's the purpose of HTML forms :)
You need to create a form to encapsulate that select:
<form action="process.php" method="get">
<select name="inventory_id">
<!-- Here all options -->
</select>
<button type="submit">See items</button>
</form>
Then in process.php you need to get the selected element and query the database, for example (I assume that you're using PDO):
<?php
$inventory_id = $_GET['inventory_id'] // The name attribute of the select
// Then you prepare the query
$query = "SELECT * FROM sheet0_100 WHERE id = :inventory_id";
// Execute the query and show the data...
use Sessions
example:
on your first page
session_start();
$_SESSION['your-dropdown-list-value'] = 'Root';
on your new page
//error_reporting(E_ALL);
session_start();
if(isset($_SESSION['your-dropdown-list-value'])) {
echo "Your dropdown selection " . $_SESSION['your-dropdown-list-value'];
}

Categories