SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can't get data to post to Mysql

    What am I doing wrong with this code? I cant get my field values to post to my database. Can someone please help me. thanks.

    PHP Code:
    // Create an empty array to hold the error messages.
    $arrErrors = array();
    //Only validate if the Submit button was clicked.
    if (!empty($_POST['Submit'])) {
        
    // Each time there's an error, add an error message to the error array
        // using the field name as the key.


        
    if (count($arrErrors) == 0) {
            
    // If the error array is empty, there were no errors.
            // Insert form processing here.

    $host " "
    $user " "
    $pass " "
    $dbname " ";

    $connection mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>"); 
    mysql_select_db($dbname);

    $sql_query mysql_query("INSERT INTO trip_log(triptype, author) VALUES ('$triptype', '$author')"
     or die (
    mysql_error()); 

    header("Location: /index.php?page=Trip_Log");

        } else {

            
    // The error array had something in it. There was an error.
            // Start adding error text to an error string.
            
    $strError '<div class="formerror"><p><img src="/uploads/images/triangle_error.gif" width="16" height="16" hspace="5" alt="">Please check the following and try again:</p><ul>';
            
    // Get each error and add it to the error string 
            // as a list item.
            
    foreach ($arrErrors as $error) {
                
    $strError .= "<li>$error</li>";
            }
            
    $strError .= '</ul></div>';
        }
    }

    echo 
    '<style>';
    echo 
    'label {

    }'
    ;

    echo 
    '.formerror {
      border: 1px solid red;
      background-color : #FFCCCC;
      width: auto;
      padding: 0px 0;
    }'
    ;

    echo 
    '.errortext {
      padding-left: 80px;
      font: bold smaller sans-serif;
    }'
    ;
    echo 
    '</style>';

    echo 
    '<form method="post" action="',$_SERVER['PHP_SELF'],'?page=Test"';
    echo 
    '<input type="hidden" name="triptype" value="ATV" />';
    echo 
    '<p';if (!empty($arrErrors['author'])); echo '>';
    echo 
    '<label for="Trip Report Author">Trip Report Author:</label><br />';
    echo 
    '<input name="author" type="text" id="author" size="29" maxlength="50" value="',$_POST['author'],'" />';
    if (!empty(
    $arrErrors['author'])) echo '<img src="/uploads/images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br />';
    echo 
    '<input type="submit" name="Submit" value="Submit">';
    echo 
    '</form>'
    Last edited by outdoorxtreme1; Mar 21, 2006 at 10:33.

  2. #2
    SitePoint Addict
    Join Date
    Dec 2004
    Posts
    240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does your code show any MySQL errors ?

    Try to echo your query before actually inserting the record:
    PHP Code:
    $connection mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>"); 
    mysql_select_db($dbname,$connection);
    $query "INSERT INTO trip_log (triptype, author) VALUES ('$triptype', '$author')";
    echo 
    $query;
    mysql_query($query,$connection) or die (mysql_error()); 

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is your register_globals turned off in your php.ini file? You may need to change your sql script to this:

    PHP Code:
    $query "INSERT INTO trip_log (triptype, author) VALUES ('$_POST[triptype]', '$_POST[author]')"

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The method tdawg described worked for the author field but not the hidden triptype. Why is this?

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo '<form method="post" action="',$_SERVER['PHP_SELF'],'?page=Test"'
    This html tag is not complete, maybe that has something to do with it.

    Try this:

    PHP Code:
    echo '<form method="post" action="',$_SERVER['PHP_SELF'],'?page=Test">'

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Eep!

    Please don't use PHP_SELF - use REQUEST_URI.

    If I request http://yoursite.com/ your form will look like:

    <form action="http://yoursite.com/">

    If I request http://yoursite.com/?"><script>alert('1')</script><span%20a=" then, using PHP_SELF it will look like this:

    <form action="http://yoursite.com/?"><script>alert('1')</script><span a="">

    With REQUEST_URI

    <form action="http://yoursite.com/index.php">

    Imagine, if instead of writing a simple javascript alert, I wrote a script that automatically sent your session ID to a remote script on my server...
    I blog, I teach and I like php and open-source.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. I got all of that to work right. Thanks. Now I have a drop down menu date and it doesn't post right to the database. Here's what I have. Anyone see anything wrong? Again, thanks for your help.

    PHP Code:
    $today getdate(); 
    $day $today['mday'];
    $month $today['mon']; 
    $year $today['year'];
    $mtharr = array("January","February","March","April","May","June","July","August","September","October","November","December");

    // Create an empty array to hold the error messages.
    $arrErrors = array();
    //Only validate if the Submit button was clicked.
    if (!empty($_POST['Submit'])) {
        
    // Each time there's an error, add an error message to the error array
        // using the field name as the key.

        
    if (count($arrErrors) == 0) {
            
    // If the error array is empty, there were no errors.
            // Insert form processing here.

    $host " "
    $user " "
    $pass " "
    $dbname " ";

    $connection mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>"); 
    mysql_select_db($dbname);

    $startdate mktime(000$TSpickMonth$TSpickDay$TSpickYear); 
    $enddate mktime(000$TFpickMonth$TFpickDay$TFpickYear);

    $sql_query mysql_query("INSERT INTO trip_log(startdate, enddate) VALUES ('$_POST[startdate]', '$_POST[enddate]')"
     or die (
    mysql_error());

    header("Location: /index.php?page=Trip_Log");

        } else {

            
    // The error array had something in it. There was an error.
            // Start adding error text to an error string.
            
    $strError '<div class="formerror"><p><img src="/uploads/images/triangle_error.gif" width="16" height="16" hspace="5" alt="">Please check the following and try again:</p><ul>';
            
    // Get each error and add it to the error string 
            // as a list item.
            
    foreach ($arrErrors as $error) {
                
    $strError .= "<li>$error</li>";
            }
            
    $strError .= '</ul></div>';
        }
    }

    echo 
    '<style>';
    echo 
    'label {

    }'
    ;

    echo 
    '.formerror {
      border: 1px solid red;
      background-color : #FFCCCC;
      width: auto;
      padding: 0px 0;
    }'
    ;

    echo 
    '.errortext {
      padding-left: 80px;
      font: bold smaller sans-serif;
    }'
    ;
    echo 
    '</style>';

    echo 
    '<form method="post" action="',$_SERVER['REQUEST_URI'],'">';
    echo 
    '<label for="trip start">Trip Start:</label><br />';
    echo 
    '<select name="TSpickMonth" size="1">';
    for (
    $i=1;$i<=12;$i++) { echo '<option value="$i"'; if ($i == $month) echo ' selected'; echo '>',$mtharr[$i-1],'</option>'; }
    echo 
    '</select>';
    echo 
    '<select name="TSpickDay" size="1">';    
    for (
    $i=1;$i<=31;$i++) { echo '<option value="$i"'; if ($i == $day) echo ' selected';   echo '>',$i,'</option>';  }
    echo 
    '</select>';
    echo 
    '<select name="TSpickYear" size="1">';
    for (
    $i=2002;$i<=2010;$i++) { echo '<option value="$i"'; if ($i == $year) echo ' selected'; echo '>',$i,'</option>';  }
    echo 
    '</select>';

    echo 
    '<label for="trip finish">Trip Finish:</label><br />';
    echo 
    '<select name="TFpickMonth" size="1">';
    for (
    $i=1;$i<=12;$i++) { echo '<option value="$i"'; if ($i == $month) echo ' selected'; echo '>',$mtharr[$i-1],'</option>';  }
    echo 
    '</select>';
    echo 
    '<select name="TFpickDay" size="1">';
    for (
    $i=1;$i<=31;$i++) { echo '<option value="$i"'; if ($i == $day) echo ' selected';   echo '>',$i,'</option>';  }
    echo 
    '</select>';     
    echo 
    '<select name="TFpickYear" size="1">';
    for (
    $i=2002;$i<=2010;$i++) { echo '<option value="$i"'; if ($i == $year) echo ' selected';   echo '>',$i,'</option>';  } 
    echo 
    '</select>';
    echo 
    '</p>';
    echo 
    $strError;
    echo 
    '<p>';
    echo 
    '<input type="submit" name="Submit" value="Submit">';
    echo 
    '</p>'
    Last edited by outdoorxtreme1; Mar 21, 2006 at 10:32.

  8. #8
    Sean N Pixel Inception's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please post your code like this {PHP}yourcodehere{/PHP} change {} to []
    Sean @ Pixel Inception, Inc. www.pixelinception.net
    Web Design & Web Development

  9. #9
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    How do you know that the code isn't just like what is show - i.e. no indents?
    Ian Anderson
    www.siteguru.co.uk

  10. #10
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I look in Mysql the data for the startdate and enddate fields are 0
    Last edited by outdoorxtreme1; Mar 21, 2006 at 10:34.


Bookmarks

Posting Permissions

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