www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Not Posting to database

  1. #1
    Join Date
    Feb 2011
    Posts
    177

    Exclamation Not Posting to database

    PHP Code:

    <?php
    $to 
    ".......@.......com" ;

    $from $_REQUEST['Email'] ;

    $name $_REQUEST['Name'] ;

    $headers "From: $from";
    $subject "Appointment Setup";

    $fields = array();
    $fields{"Name"} = "Name";
    $fields{"Email"} = "Email";
    $fields{"Phone"} = "Phone";
    $fields{"Comments"} = "Comments";

    $body "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s %s\n",$b,$_REQUEST[$a]); }

    $headers2 "From: .......@......com";
    $subject2 "Thank you for contacting us"$autoreply "Thank you for setting up an appointment. We will contact you shortly to confirm your appointment.  We look foward to meeting your family."; if($from == '') {print "You have not entered an email, please go back and try again";} else { if($name == '') {print "You have not entered a name, please go back and try again";} else { $send mail($to$subject$body$headers);
    $send2 mail($from$subject2$autoreply$headers2);




    if(isset(
    $_POST['submit']))
    {
    mysql_connect("..........""............""..........");
    mysql_select_db("..........");
    $sql mysql_query("INSERT INTO appointment('$_POST[Name]', '$_POST[email]', '$_POST[Phone]', '$_POST[Comments]', ]')") or die(mysql_error());

    mysql_close();

    } else {
        
    if(
    $send)
    {
    header"Location: http://www.............net/thankyou.html" );} else {print "We encountered an error sending your mail, please notify ........s@g.......l.com"; }} }
    }
    ?>
    It will do everything but post to the database and I can not figure out why. Ive double checked the database information to make sure columns match, Ive checked the connection information and database name everything matches up so where is the issue?

  2. #2
    Join Date
    Aug 2007
    Location
    Leeds, Yorkshire, England
    Posts
    387
    What's this at the end of your query:
    Code:
    , ]'

    Also, try adding a space after:
    Code:
    INSERT INTO appointment
    Then, if that doesn't work:
    Code:
    echo $sql;
    Copy then paste into phpMyAdmin and see what error you get

  3. #3
    Join Date
    Feb 2011
    Posts
    177

    Exclamation Still Not working

    I did all that you had suggested even added the echo still doesn't work.

    PHP Code:

    if(isset($_POST['submit']))
    {
    mysql_connect("---------""------------""----------");
    mysql_select_db("***");
    $sql mysql_query("INSERT INTO appointment ('$_POST[Name]', '$_POST[Email]', '$_POST[Phone]', '$_POST[Comments]',)") or die(mysql_error());

    mysql_close();

    } else {

        echo 
    $sql;
        
    if(
    $send

  4. #4
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    PHP Code:
    $sql mysql_query("INSERT INTO appointment  (`Name`, `email`, `Phone`, `Comments`) VALUES ('$_POST[Name]', '$_POST[email]', '$_POST[Phone]', '$_POST[Comments]',)") or die(mysql_error()); 
    Posting form data directly into a database is dangerous.

  5. #5
    Join Date
    Feb 2011
    Posts
    177

    Exclamation Why

    Why is it dangerous and what should I do instead?

    Still not adding to my database.

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    Your echo won't spit out what you wanted so you can run it directly or see what is happening:
    if(isset($_POST['submit']))
    {
    mysql_connect("---------", "------------", "----------");
    mysql_select_db("***");
    $sql = mysql_query("INSERT INTO appointment ('$_POST[Name]', '$_POST[email]', '$_POST[Phone]', '$_POST[Comments]',)") or die(mysql_error());
    echo "INSERT INTO appointment ('$_POST[Name]', '$_POST[email]', '$_POST[Phone]', '$_POST[Comments]',)";
    mysql_close();

    } else {



    if($send)

  7. #7
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by criterion9 View Post
    Your echo won't spit out what you wanted so you can run it directly or see what is happening:
    I'm also sure you'll find that the extra comma is causing the query to fail. If you have error reporting on it should be telling you that.

  8. #8
    Join Date
    Mar 2011
    Location
    Ponca City, OK
    Posts
    42
    I don't understand why you are using an array with field names to output the submitted information, but that's besides the point.

    You don't want to insert form data directly into your database as this allows for SQL injection and unwanted content, or data lost from your database.

    Code:
    if(isset($_POST['submit']))
    {
    mysql_connect("---------", "------------", "----------");
    mysql_select_db("***");
    
    //helps stop SQL injection by escaping quotes
    $name = mysql_real_escape_string($_POST['Name']);
    $email = mysql_real_escape_string($_POST['email']);
    $phone = mysql_real_escape_string($_POST['Phone']);
    $comments = mysql_real_escape_string($_POST['Comments']);
    
    $sql = mysql_query("INSERT INTO `appointment` (`name`,`email`,`phone`,`comments`) VALUES ('$name','$email','$phone','$comments');");

  9. #9
    Join Date
    Feb 2011
    Posts
    177

    Angry Still Not working

    PHP Code:
    <?php
    if(isset($_POST['submit']))
    {
    include
    'storescripts/connect_to_mysql.php';
    $sql mysql_query("INSERT INTO appointment ('$_POST[Name]', '$_POST[Email]', '$_POST[Phone]', '$_POST[Comments]')") or die(mysql_error());
    echo 
    "INSERT INTO appointment ('$_POST[Name]', '$_POST[Email]', '$_POST[Phone]', '$_POST[Comments]')";
    mysql_close();

    } else {

    if(
    $send
    {
    header"Location: http://................/thankyou.html" );} else {print "We encountered an error sending your mail, please notify ..............@............."; }}

    ?>
    I keep getting this error

    We encountered an error sending your mail, please notify e.......@........

    Ive copyed and pasted the exact phrasing from the database to both the form and the php its still giving me issues so here is the form maybe its something there.

    HTML Code:
    <form action="appointmentsetup.php" method="post">
        <p>
          <label for="Name">Name</label>
          <input type="text" name="Name" id="Name" />
        </p>
        <p>Email
          <label for="Email"></label>
          <input type="text" name="Email" id="Email" />
        </p>
        <p>Phone 
          <label for="Phone"></label>
          <input type="text" name="Phone" id="Phone" />
        </p>
        <p>      Comments 
          <br />
          <label for="Comments"></label>
          <textarea name="Comments" cols="100" rows="10" id="Comments"></textarea>
        </p>
        <p>
          <input type="submit" name="button" id="button" value="Submit" />
          <br />
        </p>
      </form>

  10. #10
    Join Date
    Mar 2011
    Location
    Ponca City, OK
    Posts
    42
    I posted a reply but it said it needed moderator approval, yet I just posted in the HTML forum.
    At the top of your file put this:
    Code:
    <?php
    error_reporting(E_ALL);
    ?>
    Run this:
    Code:
    $name = mysql_real_escape_string($_POST['Name']);
    $email = mysql_real_escape_string($_POST['email]);
    $phone = mysql_real_escape_string($_POST['Phone']);
    $comments = mysql_real_escape_string($_POST['Comments']);
    
    $sql = mysql_query("INSERT INTO appointment  (`Name`, `email`, `Phone`, `Comments`) VALUES ('$name','$email','$phone','$comments');");

  11. #11
    Join Date
    Feb 2011
    Posts
    177

    Exclamation mikeroq

    When I add the error_reporting(E_ALL); I get a window with 0 that is all the information I have.

  12. #12
    Join Date
    Jul 2007
    Location
    Wisconsin
    Posts
    468
    You've got the same questions, on (apparently) the same project in at least two, if not three different posts... it's getting hard to remember what was answered where.

    But to repeat my answer from a previous thread:

    PHP Code:
    $sql mysql_query("INSERT INTO appointment ('$_POST[Name]', '$_POST[email]', '$_POST[Phone]', '$_POST[Comments]')") or die(mysql_error()); 
    You're setting the mysql_query to the $sql var, but you don't do anything with it before closing your mysql connection.

    You need to parse the variable after setting it, in order for it to run, something akin to:

    PHP Code:
    if (!$sql)
    {
    echo 
    "<p>There was an error with the database insert: " mysql_error() ."</p>";

    Also, as mikeroq said, you need to sanitize your database inputs with, at the very least, mysql_real_escape_string()

  13. #13
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    If you don't specify the fields you are inserting data to, then you must enter data to all of the fields. Since we don't know if your insert includes all of the fields or not that may be an issue. For example, if there is a field "ID" even if it is auto-increment it should be named or have data added too it even if it is null data.

    There should be no ; within the query string.
    PHP Code:
    $sql mysql_query("INSERT INTO `appointment` (`name`,`email`,`phone`,`comments`) VALUES ('$name','$email','$phone','$comments'))"
    since you don't do anything with $sql this may be preferred;

    PHP Code:
    $query =  "INSERT INTO `appointment` (`name`,`email`,`phone`,`comments`) VALUES ('$name','$email','$phone','$comments')";

    $result mysql_query($query,$link);

    if (!
    $result) {die(mysql_errno($link). "  " .mysql_error($link)); } 

    // $link created in your connect function. 
    If there is an auto-increment field then that must be included in your insert data such as

    PHP Code:
    $query =  "INSERT INTO `appointment` (`id', `name`, `email`, `phone`, `comments`) VALUES ('', '$name', '$email', '$phone', '$comments')"

  14. #14
    Join Date
    Feb 2011
    Posts
    177

    resolved I give up

    Thank you all for the help. I'm giving up for now Im going to close all threads subject.

  15. #15
    Join Date
    Mar 2011
    Location
    Ponca City, OK
    Posts
    42
    Shutterbug, view my contact info and add me on msn, yahoo, or aim, I'll try to help you to get this working.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles