Author Topic: SQL&PHP question (another one)  (Read 1439 times)

  • Offline zpyder

  • Posts: 6,946
  • Hero Member
SQL&PHP question (another one)
on: February 18, 2010, 15:45:02 PM
So, I have a database which is now working correctly, storing the values from the google api and 2 others that are entered at the same time. However Id like to add some more data to the database in the form of user details (name, email address, that sort of thing). Another lecturer has said that using the same kind of process that I already am using, I should be able to store this in the memory.

The page etc is here:

www.zpyder.co.uk/tess/mapping/db/welcome.php

The problem is that the code I am trying doesnt break the system, but also doesnt do anything. IE: its still saving the google data but is leaving the email address field blank. See below for the code...

This is the welcome page as a test run, I want the user to enter an email address which will be used as their user ID. The email address field is called email and is a 50 charcter varchar in the database.
Code: [Select]
 


Email:




   


This is the userprocess.php file. I couldnt figure out how to get it to load the mapping page after sending the form data above apart from using the header value. I dont know if this is the correct way/if this is responsible for the issue.

Code: [Select]
header(Location: test.html);

//connect to dbase
require("dbconn.php");

//obtain email field
$email=$_POST[email];
?>


The mapping page has grown hugely since the last thread/set of questions. I think it can probably be pretty much ignored here.

Code: [Select]
 

   
   

Thank you for taking part in section 4 of the Ecosystem services
questionnaire.

The map below can be zoomed in and out using the controls on the left, and it
is possible to move around the map by clicking and dragging.

You may place up to 6 markers on areas which you value for each of 3 value
types (Recreation, Aesthetics, or Culture).

To place a marker simply click on an area of value within the red boundary, and a
marker will appear. This can be moved by clicking and dragging the icon.

When placing a marker you are required to enter the Value type in the first
text box, followed by the score, or how much you value that area.

Once you are happy with the information that has been entered, click
submit.
A marker will not be saved until submit is
pressed. If you make a mistake, refresh the page before clicking submit.


Please ensure that for each value type the total score of the combined
markers is 100.




   
 

   
 


The 2 other php files that are called are below. I think the problem is somewhere in the transfer of the $email value between the files or something?

myserver.php
Code: [Select]
//lat&lng are decimal numbers, value is text, score is an integer 0-100

$lat = $_REQUEST["lat"];
$lng = $_REQUEST["lng"];
$values = $_REQUEST["values"];
$score = $_REQUEST["score"];
$email = $_POST["email"];

//Connection to database
require("dbconn.php");

//Creates sql session connection routine using the above values
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die  (Error connecting to mysql);

//Selects the database and then runs the session connection routine
mysql_select_db($dbname,$conn);

//Defines the data to insert into the data table
$sqlinsert = "INSERT INTO zpyder_tess.google (lat,lng,value,score,email) VALUES($lat,$lng,$values,$score,$email)";

//Runs the insert routine
mysql_query($sqlinsert,$conn);
?>


genxml.php
Code: [Select]
require("dbconnxml.php");

// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

// Opens a connection to a MySQL server
$connection = mysql_connect($dbhost, $dbuser, $dbpass) or die  (Error connecting to mysql);

// Set the active MySQL database
$db_selected = mysql_select_db($dbname, $connection);
if (!$db_selected) {
  die (Can\t use db :  . mysql_error());
}

// Select all the rows in the markers table
$query = "SELECT * FROM google WHERE 1";
$result = mysql_query($query);
if (!$result) {
  die(Invalid query:  . mysql_error());
}

// Iterate through the rows, adding XML nodes for each

while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("lat", $row[lat]);
  $newnode->setAttribute("lng", $row[lng]);
  $newnode->setAttribute("value", $row[value]);
  $newnode->setAttribute("score", $row[score]);
  $newnode->setAttribute("email", $row[email]);
}

echo $dom->saveXML();
?>

  • Offline zpyder

  • Posts: 6,946
  • Hero Member
Re:SQL&PHP question (another one)
Reply #1 on: February 19, 2010, 09:54:33 AM
So Ive checked things out by making a new table in the dbase and running a script that when the form is submitted inserts the data into the table. The data is there as the $email value. However its still not saving it in the other table when a new marker is made?

I was under the impression when using _POST etc the value stays in the memory till overwritten, or does it get removed when it is called the first time?

Im essentially wanting to create a basic session control so that the user enters their email address which will stay in the browser memory for that session so every time they create a new marker the email address is recovered from memory and added to the database for that marker. Im hoping this would allow for some basic post-processing of individual users data.

  • Offline shofty

  • Posts: 847
  • Hero Member
Re:SQL&PHP question (another one)
Reply #2 on: February 19, 2010, 11:28:03 AM
Quote from: zpyder
So Ive checked things out by making a new table in the dbase and running a script that when the form is submitted inserts the data into the table. The data is there as the $email value. However its still not saving it in the other table when a new marker is made?

I was under the impression when using _POST etc the value stays in the memory till overwritten, or does it get removed when it is called the first time?

Im essentially wanting to create a basic session control so that the user enters their email address which will stay in the browser memory for that session so every time they create a new marker the email address is recovered from memory and added to the database for that marker. Im hoping this would allow for some basic post-processing of individual users data.


post info is a one off. its only in the request.post the time the page is submitted. reload the page and the post is gone. (not refresh, cos that triggers a resend which will create a new but similar post.)

Matt

  • Offline shofty

  • Posts: 847
  • Hero Member
Re:SQL&PHP question (another one)
Reply #3 on: February 19, 2010, 11:29:05 AM
if youre dead set on this, you should look into a framework to handle the auth for you. zend is a well thought of framework for pho that ive never used so cant recommend.

Matt

  • Offline zpyder

  • Posts: 6,946
  • Hero Member
Re:SQL&PHP question (another one)
Reply #4 on: February 19, 2010, 12:29:42 PM
If I use post and then $_SESSION[email] wont that make it universal till I end the session?

  • Offline zpyder

  • Posts: 6,946
  • Hero Member
Re:SQL&PHP question (another one)
Reply #5 on: February 22, 2010, 12:57:30 PM
Sorted. For some reason the sites I was using to get tutorials/examples said about errors if you have multiple session starts, and then one I read said each page needs to have the syntax at the start. Tried it and hey presto its working. Now Im onto a new problem.

So the site is now accepting multiple values through a form (first & last names, age, and email) I want to use the email as a unique key. Ive got the site sorted so that markers are only shown that were made by people with the same email address which is stored in the session variable.

The problem is I want to avoid having 2 entries in the users table. I figure the easiest way will be to do an IF statement to see if there are any email addresses that are the same as the session value, and if there is to not add a new entry. (Or rather, make sure that the session value does not equal anything in the table and if it doesnt, add a new one)

Problem is Im struggling with the code, see below:

Code: [Select]
IF (zpyder_tess.users email != $_SESSION[email]) mysql_query("INSERT INTO zpyder_tess.users (fname,lname,age,email) VALUES($_SESSION[fname],$_SESSION[lname],$_SESSION[age],$_SESSION[email])");

Ill know doubt figure it out at some point but if anyone can say off the top of their head what the correct code is, itll save a bit of time and headaches!

EDIT:
Yup, knew Id suss it. Just made the email field the primary key. Getting there:D

The current incarnation of the system is here: http://www.zpyder.co.uk/tess/mapping/db2/ - every time I start on a new chunk of the thing I make a new folder in case I break things beyond repair ><

  • Offline shofty

  • Posts: 847
  • Hero Member
Re:SQL&PHP question (another one)
Reply #6 on: February 22, 2010, 14:18:54 PM
looks to be working well mate.

  • Offline zpyder

  • Posts: 6,946
  • Hero Member
Re:SQL&PHP question (another one)
Reply #7 on: February 22, 2010, 14:33:07 PM
Its certainly getting there. Im greatful for the help youve given me too. If anyone had said 2 weeks ago Id be doing this Id have laughed. It really has been a case of jumping in at the deep end, as is pretty obvious, Im not a coder :D This has all been a case of trial and error and sheer force of will in trying different things till something works!

My current challenge is to get the below SQL code into the map page to basically show the user the total for each value so they know how close to 100 theyre getting. Ive just realised I need to modify it as well as I need to add another criteria to only sum the values for the current email address/user.

Code: [Select]
SELECT value, sum( score ) AS total_score
FROM `google`
WHERE email = zzz
GROUP BY value

  • Offline zpyder

  • Posts: 6,946
  • Hero Member
Re:SQL&PHP question (another one)
Reply #8 on: February 23, 2010, 15:25:07 PM
Yay, ive gotten out of that problem by changing the format to a ranking system of 1-5 :D

New problem/question:

how do I go about checking a variable against records in a tables column? Nothing I try seems to work at the moment.

Current code:

Code: [Select]
//Connection to database
require("dbconn.php");

//Creates sql session connection routine using the above values
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die  (Error connecting to mysql);

//Selects the database and then runs the session connection routine
mysql_select_db($dbname,$conn);

$querycheck = mysql_query("SELECT code FROM users WHERE code=$code");
$num_query = mysql_num_rows($querycheck);

if($num_query == "0"){
echo "moobaa";
}
else{
echo "moo";
}


The above was me simply trying to get it to echo anything at all, but to no avail :/

  • Offline zpyder

  • Posts: 6,946
  • Hero Member
Re:SQL&PHP question (another one)
Reply #9 on: March 12, 2010, 11:23:34 AM
So, Im back to this gubbins again.

Im trying to find info on how to call an SQL delete function. My searches just return info on the SQL syntax...

...What I want to know is whether it is possible to make a text "delete" link that when clicked does the following:

Code: [Select]
DELETE FROM MarkerTable
WHERE MarkerID=CurrentMarker


I know I could do something that passes on to a php page which runs the code, but I dont want the page to navigate away from the map. Can I not include the php on the map page so it runs automatically, or structure the link using the java or whatever the google map API uses to call the SQL function?

0 Members and 1 Guest are viewing this topic.