#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Problem inserting variable on submit


    hi
    anyone have any idea what im doing wrong with my code below? i got it to grab the variable and show the subject, message and date fine, but what i'm trying to do is get it to add the subject from the variable (a new custom message from a textarea) and date from the variable to the database.

    PHP Code:
    <?php
    require("db.php");

    $query_params = array(
      
    ':id' => $_GET['id'],
      
    ':username' => $_SESSION['user']['username']
    );
    $query "SELECT id, subject, message, date FROM tickets
                  WHERE username = :username And id= :id"
    ;
    $stmt $db->prepare($query);
      
    $result $stmt->execute($query_params);
      
    $result $stmt->setFetchMode(PDO::FETCH_NUM);
    while (
    $row $stmt->fetch($result)) {
        echo 
    $row['subject'];
        echo 
    $row['message'];
        echo 
    $row['date'];
        
        
    PROBLEMS WITH THE CODE BELOW>>>>
        
    if(!empty(
    $_POST))
    {
    if(empty(
    $_POST['message']))
    {
    $error="Please enter a Message.";
    }
    else
    {
    $query "
    INSERT INTO supporttickets (
    username,
    subject,
    message,
    date
    ) VALUES (
    :username,
    :subject,
    :message,
    date
    )
    "
    ;
    $query_params = array(
    ':username' => $_SESSION['user']['username'],
    ':subject' => $_POST['subject'],
    ':date' => $_POST['date'],
    ':message' => $_POST['message']
    );
       
    $stmt $db->prepare($query);
       
    $result $stmt->execute($query_params);
      }
     }
    }
    ?>
    <form action="" method="post" name="form">
    <textarea name="message" cols="71" rows="9" id="message"></textarea>
    <input type="submit" value="Submit" />
    </form>
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    tell us the concrete problem. What happens? What error do you get?

    The crystal ball tells me that the unknown issue might be caused by the missing colon in front of the date parameter (in the VALUES part). This should throw a fatal error with a detailed message. Not getting any message? Then your PDO is misconfigured. Use this:

    PHP Code:
    $db_options = array(
        
    PDO::ATTR_EMULATE_PREPARES => false                     // important! use actual prepared statements (default: emulate prepared statements)
        
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION           // throw exceptions on errors (default: stay silent)
        
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC      // fetch associative arrays (default: mixed arrays)
    );
    $database = new PDO('mysql:host=localhost;dbname=YOURDB;charset=utf8''YOURUSER''YOURPW'$db_options);    // important! specify the character encoding in the DSN string, don't use SET NAMES 
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    11
    Rep Power
    0
    hi its not adding anything to the database on hitting the submit button, i'm not getting and errors thanks.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Neither "subject" nor "date" are elements on your form.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo