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
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.
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
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?
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.
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
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
// 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);
// 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);