I have my tables set and the design I wanted...the only thing in my way of creating a sexy like/dislike system for my site is the actual PHP it would take (dont worry i wrote a bunmch of the code, its just not sending)
My problem is that my code isnt sending to any of the tables, so my question to you is how would I actually get it to send to the db?
Here's the code I have in place so far (along with the button)
Button
<form action="up.php">
<input type="image" value="upBtn" name="upBtn" id="upBtn" src="images/add.png"> Like
</form>
Actual code (up.php)
<?php
require 'connect2.php';
if (isset($_POST['upBtn'])) {
mysql_query("INSERT INTO votes (id, user, upvote, downvote) VALUES ('', '$username', '+ 1', '+ 0')");
mysql_query("UPDATE searchengine SET rel = rel '+ 1' WHERE id = '$id'");
}
?>
$user name def (on the top of the page all of this code is on)
if (isset($_SESSION['id'])) {
$userid = $_SESSION['id'];
$username = $_SESSION['username'];
$id was already defined on the page since all of this is on the page I wanted the votes to appear
You should include the "id" as a hidden field in the form, as the id in the session may not be the id they are voting one, for instance, if they opened another post in a separate tab, and then voted on one that had been loaded before. Also, are you sure the session variables are being saved correctly? Depending on your PHP configuration, sessions are not started by default, in order to conserve resources, and you have to call session_start() at the top of each page to actually start the session.
Try adding method="post" to the form tag. If it still doesn't work, you should put some debugging code inside the if statement to see if your queries are actually being run.
Related
So on this application I'm working on, it searches for some code in my database, grabs it, opens a new page and places the code (where necessary) onto the new page by getting its id.
Now within that code (from the database) there is another form that is processed based on user's input. The problem that I am getting is that the new form is not processed on the same page. It redirects to a new url that doesn't specify the code from the database. For example...
When the new page is generated with the code from the database, the url looks like this...
localhost/newpage.php?id=1
Then when I submit the form within the code from the database it changes to this...
localhost/newpage.php?input1=blah&input2=blah
But I want something like this...
localhost/newpage.php?id=1&input1=blah&input2=blah
Just FYI this code needs to be dynamic. For example, let's say I don't know what id the user is looking for and within that id I don't know how many input fields there are.
If you guys need some explicit code (obviously I left out the unnecessary things)...
This is searchpage.php which searches db and displays all relevant items. Then the user selects an item which contains an id and generates newpage.php with that id...
//there is a search query then puts all elements in an associative array
$rows = items->fetch_all(MYSQL_ASSOC);
foreach ($rows as $row) {
$id = $row['id'];
echo "<a href='newpage.php?id=$id>User clicks to generate a newpage</a>";
}
This is the form retrieved from database and placed in newpage.php...
<form action="newpage.php" method="GET">
<input name="input1"></input>
//there can be an x amount of input tags here depending on what id is pulled
</form>
This is the php at the top of newpage.php when it is generated...
if (isset($_GET['id'])) {
//retrieves data from specified id from database
if(isset($_GET['inputs'])) {
//do something with user inputs
}
}
Is there any way to achieve this? All help is appreciated! Thanks :)
SOLUTION
For this to work, I had to create a session which stores the id. Consequently i had to change all my $_GET varaibles (that dealt with id info) to $_SESSION variables. But it works as long as you're not storing critical/sensitive info!
Here's the code changes...
Changes in search.php ...
//there is a search query then puts all elements in an associative array
$rows = items->fetch_all(MYSQL_ASSOC);
foreach ($rows as $row) {
$id = $row['id'];
$_SESSION['current_id'] = $id;
echo "<a href='newpage.php?id=$id>User clicks to generate a newpage</a>";
}
Changes in newpage.php ...
<form action="newpage.php?id=" method="POST">
<input name="input1"></input>
//there can be an x amount of input tags here depending on what id is pulled
</form>
Changes in php of newpage.php ...
if (isset($_SESSION['current_id'])) {
//retrieves data from specified id from database
if(isset($_POST['inputs'])) {
//do something with user inputs
}
}
ALL THANKS TO...
Sathik Khan
You can get the query string "id" in referred header. Either you can attach that query string in client or in server.
Please use $_SERVER['HTTP_REFERER'] to get referer url and from there you can get the id. If it's id of logged in user, I would recommend store them in session for security reason.
Based on your comments,
You can take any one of these actions,
1. Keep the ID in your form as hidden variable
2. Update your url with id as one of the query string and send back to client for error correction.
3. Keep the ID in session, if applicable
4. Use post and perform form validation before redirecting
Thanks.
So I'm a real noob to SQL. I only just managed to get my head around sending data from a html form to a table in a database.... Please bear with me.
I have a very large form on which I have broken down into 6 sections for each user to fill out. The project I am working on is a career progression based website. It has sections called "About Me", "Employment History", "Education" etc.
The about me page is the first form a use will complete and it will generate a new user account number in the database under the "About Me" table.
I want to take that number and use it on all other forms for the same applicant behind the scenes. How can I do that?
I hope that question made sense and thanks in advance for any help you can offer me.
What I have tried so far is this:
<?php
$ApplicantNo = SELECT ApplicantNo from aboutme WHERE EmailAddress='$EmailAddress' AND Firstname='$FirstName';
<input type="hidden" name='ApplicantNo' value='" . $ApplicantNo . "'>"; ?>
I would recommend adding this id to the session variable. Then it can be used on any page in the application.
It may be even better to have them create an account so the id can always be accessed even if session is ended. you would set the id in the session on each login
$_SESSION['uid']
Well to do that you have to use the SESSION variable..
First of all you have to execute the mysql query correctly
<?php
session_start();
$ApplicantNo = "SELECT ApplicantNo from aboutme WHERE EmailAddress='$EmailAddress' AND Firstname='$FirstName'";
$query=mysql_query($ApplicantNo);
$query_row=mysql_fetch_assoc($query);
$_SESSION['app_no']=$query_row['ApplicantNo'];
echo "<input type=\"hidden\" name='ApplicantNo' value='\" . $_SESSION['app_no'] . \"'>";
?>
So you can use it in this way in other pages and don't forget to write session_start() in begining of every page you use the SESSION variable.
This might a little noobish but I don't know where to start with my problem. A friendly nudge in the right direction would be greatly appreciated.
I am trying to have a gallery of material, the images of the materials are stored in a database, now i want to make the users click on a specific image of the material that they like, this would lead them to next page that would calculate the price.
So i guess my question is, how do i carry this variable of their choice to the next page from the gallery?
Any help greatly appreciated
btw this is my code for getting the images displayed from the database:
<?php
mysql_connect("localhost","root","");
mysql_select_db ("imagez");
$res=mysql_query("select * from imgz");
while($row=mysql_fetch_array($res))
{
echo "<a href=\"page.php?id={$row['id']}\" />
<img src=\"{$row['img']}\> </a>";
}
?>
you can hand over values to another page by...
(1) QUERYSTRING (URL)
page.php?do=show&id=5
create those by pulling the unique id of a record from your db and create a link:
echo "<a href=\"page.php?id={$row['id']}\" />{$row['name']}</a>";
assuming that your table imgz has the fields id, name.
in page.php, you get those variables with
$_GET['do']; $_GET['id'];
pro: quick & easy
contra: can be easily manipulated by evil users - they change the querystring and get your script to do weird things.
(2) POST
create a <form method="post" action="page.php">with a hidden field named id containing the 'id' you want to pass. Add a <submit>-button to send the data to page.php.
in page.php, you get those values by:
$_POST['id'];
pro: safe, because evil user can't fiddle with POST
contra: overhead
(3) SESSION:
call session_start(); on top of every page, write your data into the session by...
$_SESSION['id'] = $id; $_SESSION['do'] = "show";
these variables will be kept between pages, in page.php you can simply use S_SESSION['id'] and have that value - if you called session_start().
pro: easy and safe, PHP will do the work for you (e.g. handling cookies etc.)
contra: will put load on your webserver if traffic goes up on your site.
You can set URL parameters, so when they click it, you would have a parameter like;
http://example.com/pricecheck.php?photoid=435
Then on the page you would do;
$photo = $_GET['photoid'];
Then you would have the ID of that photo on the page
Instead of passing the variable via URL, you can use cookie to store the variable and doing the calculation to check up.
<?php
$expire=time()+60*60*24*30;
setcookie("price", "$54", $expire);
echo $_COOKIE['price'];
?>
Now that variable $price would be available for each your webpages, and finally don't forget to clear the cookie `setcookie("hp","", $expire);
SAME PAGE RESULTS: (xmain.php)
//FORM (working good)
<form name="search" action="xmain.php" method="post">
code,code,code,
// QUERY (working good)
code,code,code,
<input type="submit" name="doSearch" value="doSearch">
//These are last 2 lines of my 15 line query - I have skipped the rest to save space:
$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM $tableName $qryWhere LIMIT $start, $limit;";
$result = mysql_query($sql);
// Table query results here.....
OK, although all code is working correctly ( Form is submitting variables, database results are correct, etc, there are 2 nagging problems:
1 - ALL database results are echoed when the page is first loaded. The page is refreshing from top to bottom without a stop in between - I would like for NO RECORDS to be shown at first page arrival.
2. Also, because of the top to bottom nature of this page,my option choices are being reset upon form submission. I would like to maintain selections until the RESET button -
that I have on the page for that purpose is clicked.
I realize that the form by always refreshing upon itself, is causing the above problems.
Any suggestions would be greatly appreciated! - see working sample here:
http://www.symbioticmusicpublishing.com/database3/xmain.php
Only show something when something is actually posted, so surround with if(isset($_POST['doSearch']){.. or something.
echo a value <input .... value="'.(isset($_POST['thisinputname'])?htmlspecialchars($_POST['thisinputname']:'')">
On more elaborate forms a session could be handy, not needed for simpler ones/forms always getting the post though. BTW, I prefer using GETs for listing data, makes it easier to share/link. POSTs for alterations, GETs for retrieving (and a few other methods for real REST).
Echo the retrieved values inside a if statement like Wrikken suggested. Check that your form submission button is actually clicked and only then echo the results.
If you're unfamiliar with the syntax take a look at ternary operator.
http://www.php.net/manual/en/language.operators.comparison.php
Also use GET if you are getting data from database. And POST when you are putting data into database. I guess the reason why goes beyond the naming convention but still it's a nice reminder: GET for getting and POST for posting/putting.
UPDATE
Sorry I didn't think this one through. Put the sql query inside a if statement. Put also the echoing of results inside a if statement.
if (isset[$_POST['doSearch']]){
//make the query
}
if ($sql){
//echo the results
}
Newby here.
Could someone show me an example of the code needed to do the following:
User pushes a button on my web site (there is no information for him to input, and no form, he just clicks on a button). I have found the following code on another post, but don't know if it is correct (I am also getting a syntax error on it):
<form action="php_file.php"><input type="submit" value="Click"></form>
The author of the above code said "Insert your PHP-Code into the file php_file.php and click the button, your file will be opened. Insert header("Location: html_file.html"); at the end of your php-file to get back to the page."
This click of the button needs to instigate the programming to grab the current URL and previous URL and insert them into the mysql database on my server. I have "PHP_SELF" and "HTTP_REFERER", but still need to get the results into mysql.
I would like to do this using only html, PHP and mysql, if possible.
Thanks to everyone for any help!
if your first file happen to be a PHP one, write this HTML form there.
<form action="php_file.php" method="POST">
<input type="hidden" name="previous" value="<?=urlencode($_SERVER['REQUEST_URI'])?>">
<input type="submit" value="Click">
</form>
and then in the php_file.php
<?
$current = $_SERVER['REQUEST_URI'];
$previous = $_POST['previous'];
though both variables will contain only partial url, without host name, schema and, possible, port. it's usually enough but if you need these absent parts, you'll have to add them manually.
as for the writing info into database and particular PHP syntax rules you have to find yourself a tutorial, because this site is devoted to answering questions, not online education nor doing someone's job for free.
With PHP, you can manage it with cookie session, first thing you'll need to do is start a session and then define the space where you'll store the URL information e.g: $_SESSION["url"]
session_start();
$_SESSION["url"]=$_SERVER['REQUEST_URI'];
And whenever you want to go to that particular page, add the header:
header('location: ' .$_SESSION["url"]. '');
Current:
$currentUrl = $_SERVER["PHP_SELF"];
Previous:
$previousUrl = $_SERVER['HTTP_REFERER'];
Note that some users may have browser preferences set that keep $_SERVER['HTTP_REFERER'] from being set, so it's possible that it would come back empty.