Multiple Variables in PHP URL with HTTP Referrer - php

I'm trying to create a custom URL with HTTP_Referrer (where they came from), the date, and an incrementing traffic URL
mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_select_db('database');
mysql_query("INSERT INTO table (TrafficID, ClickURL, CreateDate) VALUES('".mysql_real_escape_string($_GET['TrafficID'])."', '".mysql_real_escape_string($_GET['ClickURL'])."', '".mysql_real_escape_string($_GET['CreateDate'])."')");
mysql_query("UPDATE table_name SET out = out + 1 WHERE ID = $TrafficID")or die(mysql_error());
$data = mysql_query("SELECT * FROM table_name WHERE ID = $TrafficID") or die(mysql_error());
$info = mysql_fetch_array($data);
$variable['TrafficID'] = "id";
$variable['ClickURL'] = "url";
$variable['CreateDate'] = "date";
$value['TrafficID'] = "mysql_fetch_array($data);";
$value['ClickURL'] = "$_SERVER['HTTP_REFERER']";
$variable['CreateDate'] = "$today";
$link = "http://www.example.com/page.php";
$link .= "?";
$link .= $variable['TrafficID'] . "=" . $value['TrafficID'];
$link .= "&";
$link .= $variable['ClickURL'] . "=" . $value['ClickURL'];
$link .= "#";
$link .= $variable['CreateDate'] . "=" . $value['CreateDate'];
echo "<a href='" . $link . "'>Click here!</a>";`
Ideally the URL would look like http://www.example.com/page.php?TrafficID&ClickURL#date and this would all be stored in the database, however I can't seem to get it to work, can anyone point where I made a mistake?
UPDATE
$value['ClickURL'] = "$_SERVER['HTTP_REFERER']";
$today = date("Ymd");
$variable['CreateDate'] = "$today";
$link = "http://www.example.com/page.php";
$link .= "?";
$link .= $variable['TrafficID'] . "=" . $value['TrafficID'];
$link .= "&";
$link .= $variable['ClickURL'] . "=" . $value['ClickURL'];
$link .= "&";
$link .= $variable['CreateDate'] . "=" . $value['CreateDate'];
echo "<a href='" . $link . "'>Click here!</a>";`
Changed # to & and reformatted my date value

Related

Get all rows from MySQL Query

The PHP Code:
<?php
//Server Information
$servername = "localhost";
$dbusername = "USERNAME";
$password = "TOTALLYSECUREPASSWORD";
$dbname = "DEFINITELYADATABASE";
//Query Information
$guid = $_POST['GUID'];
$username = $_POST['USERNAME'];
$admin_username = $_POST['ADMIN_USERNAME'];
$ban_reason = $_POST['BAN_REASON'];
$ip = $_POST['IP'];
//Create Connection
$connection = mysqli_connect($servername, $dbusername, $password, $dbname);
//Check the Connection
if ($connection->connect_error){
die("Connection failed: " . $connection->connect_error);
}
//$sql = "SELECT DATE, DBUSERNAME, GUID, IP, USERNAME, BAN_REASON FROM bans";
//$result = $connection->query($sql);
$sql = "SELECT * FROM bans WHERE";
$types = json_decode($_POST['QUERY_TYPE'], true);
if (in_array("query_admin_username", $types)) {
$sql = $sql . " DBUSERNAME = " . "\"" . $admin_username . "\"" . " &&";
}
if (in_array("query_guid", $types)) {
$sql = $sql . " GUID = " . "\"". $guid . "\"" . " &&";
}
if (in_array("query_ip", $types)) {
$sql = $sql . " IP = " . "\"" . $ip . "\"" . " &&";
}
if (in_array("query_username", $types)) {
$sql = $sql . " USERNAME = " . "\"" . $username . "\"" . " &&";
}
if (in_array("query_ban_reason", $types)) {
$sql = $sql . " BAN_REASON = " . "\"" . $ban_reason . "\"" . " &&";
}
$sql_query = substr($sql, 0, -3);
echo ($sql_query);
$result = $connection->query($sql_query);
while ($connection->query($sql_query)) {
}
if (!$result) {
die("Invalid Query: " . mysqli_error());
}
$row = $result->fetch_array(MYSQLI_NUM);
while ($row = mysqli_fetch_assoc($result)) {
echo ($row);
}
mysqli_close($connection);
?>
As weird as all that looks, it works just how I want it to (I think).
My issue:
I want to be able to get the data from each row and export it as one large String, something along the lines of:
[DATE] DBUSERNAME banned USERNAME (GUID / IP) for BAN_REASON.
I just have absolutely no idea how to go about this. I've tested the Query and it's returning everything it should, however I was using "echo ($row[0])" etc to display them, which is pretty impractical if it's going to return a large amount of rows.
Sorry if something doesn't make sense, my brain is fried at the moment. Please let me know if I forgot anything.
You could concatenate the columns like this if the rest of your script works:
SELECT CONCAT('[',DATE,'] ',DBUSERNAME,' banned ',USERNAME,'(',COALESCE(GUID, IP),),') for ', BAN_REASON) AS your_columns_in_one_line FROM your_table WHERE .....;
See this link for reference to CONCAT

Records don't load into edit page after update query

I'm stumped on this one... I'm using the following code to load a record into a form for editing. The record loads fine into the fields etc. I click submit and the record doesn't reload. If I use edit_link.php?link_pk=50 in the url, the record doesn't load. If I change the value to an unedited record it loads fine into the form, but if I edit that record, the same thing happens. The data looks exactly the same in the database as it did before it was edited (ie I'm not changing anything before I submit):
$link_pk = $_GET['link_pk'];
$author_pk = $_GET['author_pk'];
$title = $_POST['title'];
$url = mysql_real_escape_string($_POST['url']);
$url_for_link = $_POST['url'];
$alt = $_POST['alt'];
$credit = $_POST['credit'];
$sub_discipline_fk = $_POST['sub_discipline'];
$link_category_fk = $_POST['category'];
$icon = $_POST['icon'];
$query_link = "SELECT * FROM link, sub_discipline, link_category, link_icon WHERE link.sub_discipline_fk = sub_discipline.sub_discipline_pk AND link.link_category_fk = link_category.link_category_pk AND link.link_icon_fk = link_icon.link_icon_pk AND link.link_pk = '$link_pk'";
$result_link = mysql_query($query_link, $connection) or die(mysql_error());
$row_link = mysql_fetch_assoc($result_link);
switch ($icon) {
case '1':
$link = mysql_real_escape_string("<a class='text' href='" . $url_for_link . "' target='_blank' alt='" . $alt . "' >" . $title . "</a>");
break;
case '2':
$link = mysql_real_escape_string("<a class='video' href='" . $url_for_link . "' target='_blank' alt='" . $alt . "' >" . $title . "</a>");
break;
case '3':
$link = mysql_real_escape_string("<a class='interactive' href='" . $url_for_link . "' target='_blank' alt='" . $alt . "' >" . $title . "</a>");
break;
case '4':
$link = mysql_real_escape_string("<a class='microscope' href='" . $url_for_link . "' target='_blank' alt='" . $alt . "' >" . $title . "</a>");
break;
}
if(isset($_POST['submit'])){
$query = "UPDATE link SET link_title = '$title', url = '$url', link = '$link', alt = '$alt', credit = '$credit', sub_discipline_fk = '$sub_discipline_fk', updated = NOW(), updated_by = '$author_pk', link_category_fk = '$link_category_fk', link_icon_fk = '$link_icon_fk' WHERE link_pk = '$link_pk'";
$result = mysql_query($query, $connection) or die(mysql_error());
if($result){
$query_link = "SELECT * FROM link, sub_discipline, link_category, link_icon WHERE link.sub_discipline_fk = sub_discipline.sub_discipline_pk AND link.link_category_fk = link_category.link_category_pk AND link.link_icon_fk = link_icon.link_icon_pk AND link.link_pk = '$link_pk'";
$result_link = mysql_query($query_link, $connection) or die(mysql_error());
$row_link = mysql_fetch_assoc($result_link);
$message = '- The link has been updated';
}
}
Please do not remind me that the above is depricated, I'm aware of that.
Thanks
Stupid mistake...I changed a variable name and didntt change it in the query... link_icon_fk = '$link_icon_fk' should have been link_icon_fk = '$icon' - trying to work too fast...

jQuery Autocomplete get feeds from mysql DB

Do you have an idea how to pull data from mysql, put it in an array then feed it in an autocomplete field?
I have tried hardcoded the values but what I'm thinking is when I add a new record, I have to re-code again the array. I'm a newbie in PHP so I beg your pardon.
Kindly check what I've tried so far:
protected function jsGenerateResourcesAutocomplete(){
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT employee_name FROM employee" ;
mysql_select_db('test', $conn);
$retval = mysql_query($sql);
$row = mysql_fetch_assoc($retval);
if(!$retval )
{
die('Could not select data: ' . mysql_error());
}
$employeeNames = $this->employeeNames;
$html = ""; $html .= 'var employeenames = [' . PHP_EOL;
foreach ($employeeNames as $employeeName) {
$html .= '"' . $employeeName-> $array . '",' . PHP_EOL;
}
$html .= '];';
$html .= '$(".resource-input input").autocomplete({source: employeenames});' .PHP_EOL;
$html .= '});' . PHP_EOL;
$html .= '</script>' . PHP_EOL;
return $html;
}
I believe something in this line:
$html .= '"' . $employeeName-> $array . '",' . PHP_EOL;
I have to put forth the array but I have no idea how to do it. Any help is truly appreciated. Thanks.
Try this way:
$sql = "SELECT employee_name FROM employee" ;
mysql_select_db('test', $conn);
$retval = mysql_query($sql);
if(!$retval )
{
die('Could not select data: ' . mysql_error());
}
$data = array();
while($row = mysql_fetch_array($retval)){
$data[] = $row['employee_name'];
}
$html = "<script>";
$html .= 'var employeenames = '.json_encode($data);
$html .= '$(".resource-input input").autocomplete({source: employeenames});';
$html .= '});';
$html .= '</script>';
return $html;

Using multiple tables in a search engine?

To clean and tidy-up my code, I want to add multiple tables with the fields:
id
title
description
keywords
link
but I also want them in sections so in MySql I want different tables with categories such as: "News", "Social Networking" and "Shopping", but how can I get that? This is my code:
<?php
if( count($terms) == 0){ // If no terms entered, stop.
echo "No Search Terms Entered.";
}else{
// connect
$connect = mysql_connect("XXX", "XXX", "YYY") or die('Couldn\'t connect to MySQL Server: ' . mysql_error());
mysql_select_db("theqlickcom_774575_db1", $connect ) or die('Couldn\'t Select the database: ' . mysql_error( $connect ));
/* Query Statement Building - Terms together */
$query = " SELECT * FROM scan WHERE ";
$terms = array_map('mysql_real_escape_string', $terms);
$i = 0;
foreach ($terms as $each) {
if ($i++ !== 0){
$query .= " AND ";
}
$query .= "keywords LIKE '%{$each}%'";
}
$query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error( $connect ));
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo '<h2><a class="ok" href="' . $link . '">' . $title . '</a></h2>' . PHP_EOL;
echo '<p class="kk">' . $description . '<br><br><span class="keywords">' . PHP_EOL;
echo '<p><a class="okay" href="' . $link . '">' . $link . '<br><br><span class="keywords">' . PHP_EOL;
}
} else {
/* Query Statement Building - Terms Separate */
$query = " SELECT * FROM scan WHERE ";
$terms = array_map('mysql_real_escape_string', $terms);
$i = 0;
foreach ($terms as $each) {
if ($i++ !== 0){
$query .= " OR ";
}
$query .= "keywords LIKE '%{$each}%'";
}
$query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error( $connect ));
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo '<h2><a class="ok" href="' . $link . '">' . $title . '</a></h2>' . PHP_EOL;
echo '<p class="kk">' . $description . '<br><br><span class="keywords">' . PHP_EOL;
echo '<p><a class="okay" href="' . $link . '">' . $link . '<br><br><span class="keywords">' . PHP_EOL;
}
} else {
echo "No results found for \"<b>{$k}</b>\"";
}
}
//disconnect
}
?>
If you have to search multiple table with one query
1.use mysqli
2.use UNION ALL in query
Join table
use mysql procedure

receiving newline inside a json object

i am trying to bring a set of texts from a PostgreSQL database (field type is text) with ajax into my webpage. The problem i encouter is the newline, it works great when there is no newlines into the text in database, but when i use new lines in the text inside database, the json doesnt give me the data it stored.
function selectCalendarEvents(user_id, event_datestart, event_datestop, callback) {
var request;
if(window.XMLHttpRequest)
request = new XMLHttpRequest();
else
request = new ActiveXObject("Microsoft.XMLHTTP");
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
if(request.responseText.substr(0, 6) == "error ")
alert(errorName[request.responseText.substr(6)]);
else {
jsonCalendarEvents = $.parseJSON(request.responseText);
selectEvents(user_id, event_datestart, event_datestop, callback);
}
}
}
request.open("GET", "php/calendar.php?action=selectCalendarEvents&user_id=" + user_id + "&event_datestart=" + event_datestart + "&event_datestop=" + event_datestop, true);
request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
request.send();
}
this is my javascript function which requests the data set
if($action == "selectCalendarEvents") {
$user_id = $_GET["user_id"];
$event_datestart = "'" . $_GET["event_datestart"] . "'";
$event_datestop = "'" . $_GET["event_datestop"] . "'";
require_once("connect.php");
$query = "SELECT event_id, event_name, event_datestart, event_datestop, event_timestart, event_timestop, calendar_events.calendar_group, calendar_color, event_info FROM calendar_events LEFT JOIN calendar_calendars ON calendar_events.calendar_group = calendar_calendars.calendar_group WHERE user_id = " . $user_id . " AND calendar_show = true AND event_datestart <= ". $event_datestop . " AND event_datestop >= " . $event_datestart;
$result = pg_query($connect, $query);
if(!$result)
die("error 1"); // query error
$comma = '';
$json = '{"events":[';
while ($row = pg_fetch_row($result)) {
$json .= $comma . '{';
$json .= '"event_id":"' . $row[0] . '",';
$json .= '"event_name":"' . $row[1] . '",';
$json .= '"event_datestart":"' . $row[2] . '",';
$json .= '"event_datestop":"' . $row[3] . '",';
$json .= '"event_timestart":"' . $row[4] . '",';
$json .= '"event_timestop":"' . $row[5] . '",';
$json .= '"calendar_group":"' . $row[6] . '",';
$json .= '"calendar_color":"' . $row[7] . '",';
$json .= '"event_info":"' . $row[8] . '"';
$json .= '}';
$comma = ',';
}
$json .= ']}';
echo $json;
}
this is my php which returns the dataset. If row[8] aka event_info stores text with newlines in it .. when i try to populate my calendar with the events, it seems the json doesnt store any data in it or smt like that.
I found out that i should replace \r\n with <br/> and it will work. im using like this replace(/\r\n/g, "<br/>") when i give the response string to json
jsonCalendarEvents = $.parseJSON(request.responseText.replace(/\r\n/g, "<br/>"));

Categories