Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.

  • #5
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.

  • #7
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Try changing this:
    $cust_id = $_session ['usr_id'];

    To this:
    $cust_id = $_SESSION['usr_id'];

  • #9
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    ??

  • #10
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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'];
    }

  • #11
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #12
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi
    Its not at the top of config

    so i have removed the comment out and will include it there, still no luck

  • #13
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    The error message said undefined variable: _SESSION

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

    repost what you have now with the changes in place.

  • #14
    New Coder
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <?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);

  • #15
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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);


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •