Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-31-2012, 07:51 PM   PM User | #1
wiztech69
New Coder

 
Join Date: Mar 2012
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
wiztech69 is an unknown quantity at this point
PHP get data script query

Hello

I am using WAMP as my server. created a database with 2 tables, one for customers & the other for customer viewpoints

once a customer has registered they can login and then leave viewpoints on subjects. these then go into the other table. the other table has the following columns viewpoint id (PK), customer id, subject, opinion, time.

The form for leaving viewpoints has two areas; topic & viewpoint.
so when a viewpoint is left this must obviously be stored in the viewpoints table

but the code to get the data from this form to this viewpoint table im am a bit unsure about. I know it will be similar to what i used for register as seen below. Some parts have been changed to match viewpoint table

Code:
<?php
//error_reporting(E_ALL ^ E_NOTICE); 
include "config.php";

// Get values from form 
$subject = $_POST['subject'];
$comments = $_POST['comments'];

// Insert data into mysql 
$sql="INSERT INTO viewpoint(,customer id ,subject , opinion, time)VALUES('$', '$s', '$', '$')";
$result=mysql_query($sql);

// if successful insert data into database, displays message "completed". 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href=''>Back to page </a>";
}

else {
echo "ERROR";
}

// close connection 
mysql_close();
?>
I am not sure as 'time' needs to be inserted into database table aswell as customer id which i guess will be determined from once they are logged in.

Please advise with this if possible, Thanks

Last edited by wiztech69; 03-31-2012 at 07:51 PM.. Reason: title error
wiztech69 is offline   Reply With Quote
Old 03-31-2012, 09:19 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
This line:
$sql="INSERT INTO viewpoint(,customer id ,subject , opinion, time)VALUES('$', '$s', '$', '$')";

Needs variable names like:

$sql="INSERT INTO viewpoint(,customer id ,subject , opinion, time)VALUES('$id', '$subject', '$opinion', '$time')";

They have to be in the same exact order as the column names you listed ( .... )

$id ...
needs to be your customer id, like you said .. did they login?
Not sure how the login portion of your script is done.
I'm guessing a SESSION was set, but of course, we can see that.

$time ...
we don't know how you formatted your 'time' column in your database table.

security ...
You should 'sanitize' any variables that you are putting into your database.
See: http://php.net/manual/en/function.my...ape-string.php
mlseim is offline   Reply With Quote
Old 03-31-2012, 10:07 PM   PM User | #3
wiztech69
New Coder

 
Join Date: Mar 2012
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
wiztech69 is an unknown quantity at this point
Hi
yes sorry for misunderstanding, yes im aware of the variable naming in the correct order, just took this script from the register script i used.

Yes they have logged in. I can send you the login script i used just to make things clearer. Best way to contact you is through here?
Yes a session was set

In my mysql database in wamp the db was set as 'TIME'

as for the security part not to worry as just doing a basic little system.
hope to hear from you soon
Thanks
wiztech69 is offline   Reply With Quote
Old 03-31-2012, 10:12 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Here is how to use TIME:
http://www.tizag.com/mysqlTutorial/mysql-time.php

$id will be taken from a PHP SESSION that was probably created when they logged in.

$id=$_SESSION['id'];

sort of like that.
mlseim is offline   Reply With Quote
Old 04-01-2012, 04:58 PM   PM User | #5
wiztech69
New Coder

 
Join Date: Mar 2012
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
wiztech69 is an unknown quantity at this point
thanks for this, looks straightforward so will try and let you know.

Also as mentioned before i have various different scripts such as one for logging in and another for registering. Ideally I would like to put these in one class
and have the form call this class when needed

I have an idea of how to sort of lay this out as seen below

Code:
<?php

Class Register




public function ()
{
}

public function ()
{
}

public function ()
{
}

public function register()
{
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

}




?>
But I am unsure of how to transfer my code to the class, whether it needs to be re-written etc? Do all the variables used within the class have to be declared at the top of the class or do they need to be declared only above the related function?

Another query..if I have one class with different functions how does the form know which fucntion to call upon as there is more than 1 function?

Using the above class template above I would be very grateful if you could give me an example of how to put my existing script into a class?

Thanks
wiztech69 is offline   Reply With Quote
Old 04-01-2012, 07:42 PM   PM User | #6
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
This is really a hard one for me to answer ...
I thought of a way to explain, but found this using Google:
http://www.phpbuilder.com/columns/rod19990601.php3

I think decisions on what to put into the class, how much to
put in, and where it's used really depends on your whole website
and how everything relates.

I have never made a website large enough to use classes. Of course,
if you install something like Joomla, wordpress, etc. those will utilize
classes. Like you, I should actually do it, and learn it.
mlseim is offline   Reply With Quote
Old 04-01-2012, 08:14 PM   PM User | #7
wiztech69
New Coder

 
Join Date: Mar 2012
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
wiztech69 is an unknown quantity at this point
hi there i will for sure take a look at that and see if can work it out. My website is only small anyway, consists of like 4 pages

regard post #4 in our thread about using session/id for my second table

i rewrote the code and seems to be working as the data entered transfers to the database! But the user id (recorded by the session) does not seem to be going into the database please see my code to see if a mistake has been made somewhere?

Code:
<?php
//session_start();  
include "config.php";

// Get values from form 
$subject = $_POST['subject'];
$opinion = $_POST['opinion'];
$cust_id = $_session ['usr_id'];



// Insert data into table
$sql="INSERT INTO viewpoint (cust_id ,subject, opinion)VALUES('$cust_id', '$subject', '$opinion')";
$result=mysql_query($sql);

// if sucesful transfer data into db 
if($result){
echo "Successful";
echo "<a href=''>Previous</a>";
}

else {
echo "ERROR";
}

// close connection 
mysql_close();
?>
The session i used when a user logged in was: session_start();
do i also need this in this script i am writing?
Hope can resolve the id not being transferred into the table.
Thanks
wiztech69 is offline   Reply With Quote
Old 04-01-2012, 09:04 PM   PM User | #8
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Try changing this:
$cust_id = $_session ['usr_id'];

To this:
$cust_id = $_SESSION['usr_id'];
mlseim is offline   Reply With Quote
Old 04-01-2012, 09:16 PM   PM User | #9
wiztech69
New Coder

 
Join Date: Mar 2012
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
wiztech69 is an unknown quantity at this point
Error: Undefined variable: _SESSION ......(line 9)
FYI
line 9: $cust_id = $_SESSION['usr_id'];

tried changing to uppercase but did not make a difference. Received above error
??
wiztech69 is offline   Reply With Quote
Old 04-01-2012, 09:47 PM   PM User | #10
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
At the top of your script you have this:

<?php
//session_start();
include "config.php";

session_start(); is commented out.
You can leave it like that if it's already at the top of "config.php" ... is it?

I think you're getting a warning ... so try this:

<?php
//session_start();
include "config.php";

// Get values from form
$subject = $_POST['subject'];
$opinion = $_POST['opinion'];
$cust_id="";
if(isset($_SESSION['usr_id'])){
$cust_id = $_SESSION['usr_id'];
}
mlseim is offline   Reply With Quote
Old 04-01-2012, 10:01 PM   PM User | #11
wiztech69
New Coder

 
Join Date: Mar 2012
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
wiztech69 is an unknown quantity at this point
hi
i tried that but as it did not work i just got "Error" as instructed by my code?

Im pretty sure do not need to amend the insert part of my code just the part you mentioned with variables.
Now confused as to why this not working. No data at all saved to table after this change you recommended.??
Thanks
wiztech69 is offline   Reply With Quote
Old 04-01-2012, 10:41 PM   PM User | #12
wiztech69
New Coder

 
Join Date: Mar 2012
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
wiztech69 is an unknown quantity at this point
Hi
Its not at the top of config

so i have removed the comment out and will include it there, still no luck
wiztech69 is offline   Reply With Quote
Old 04-02-2012, 12:10 AM   PM User | #13
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
The error message said undefined variable: _SESSION

Did you forget the dollar sign .... $_SESSION ?

repost what you have now with the changes in place.
mlseim is offline   Reply With Quote
Old 04-02-2012, 12:26 AM   PM User | #14
wiztech69
New Coder

 
Join Date: Mar 2012
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
wiztech69 is an unknown quantity at this point
<?php
session_start();
include "config.php";

// Get values from form
$subject = $_POST['subject'];
$opinion = $_POST['opinion'];
$cust_id ="";
if(isset($_SESSION['usr_id'])){
$cust_id = $_SESSION['usr_id];
}


// Insert data into mysql table
$sql="INSERT INTO viewpoint (usr_id ,subject, opinion)VALUES('$usr_id', '$subject', '$opinion')";
$result=mysql_query($sql);
wiztech69 is offline   Reply With Quote
Old 04-02-2012, 12:41 AM   PM User | #15
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
I see a typo ... my mistake ...

missing single quote ... shown below in red.


<?php
session_start();
include "config.php";

// Get values from form
$subject = $_POST['subject'];
$opinion = $_POST['opinion'];
$cust_id ="";
if(isset($_SESSION['usr_id'])){
$cust_id = $_SESSION['usr_id'];
}


// Insert data into mysql table
$sql="INSERT INTO viewpoint (usr_id ,subject, opinion)VALUES('$usr_id', '$subject', '$opinion')";
$result=mysql_query($sql);
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:50 PM.


Advertisement
Log in to turn off these ads.