Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: newbie question on php and forms

  1. #1
    Join Date
    Aug 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default newbie question on php and forms

    I have the below code which is suppose to post back to itself but I can't get it to get the data from the form to be inserted into the database correctly and then get the data that was just inserted into the database and displayed it in a table.. I keep getting Query error. Can someone help me out?
    PHP Code:
    <?php

    //Database Connection
     
    $msdb mysql_connect("localhost""user""pass");
    $db mysql_select_db("test"$msdb) or die(mysql_error());



    $fname $_POST['fname'];
    $lname $_POST['lname'];
    $phone $_POST['phone'];

    if (!isset(
    $_POST['send'])) 
    {

    ?>

    <html>
    <title></title>
    <head></head>

    <body TOPMARGIN="0" LEFTMARGIN="0"  MARGINWIDTH="0" MARGINHEIGHT="0">
    <form  id = "maintenanceForm" Name = "maintenanceForm"  action="<?=$_SERVER['PHP_SELF']?>" method="POST" >
    <fieldset>
    <input type="hidden" id ="send" name="send" value="true">
    <table cellpadding="5" cellspacing="0" border="0" align="center">
    <tr>
    <td align="right" class="formLabel"><B>First Name</B>:</td>
    <td align="left"><INPUT id ="fnamel" NAME="fname" VALUE=""><BR></td>
    </tr>
    <tr>
    <td align="right" class="formLabel"><label for="name"><B>Last Name</B></label>:</td>
    <td align="left"><INPUT id = "lname" NAME="lname" VALUE=""><BR>
    </div></td>
    </tr>
    <tr>
    <td align="right" class="formLabel"><B>Phone</B>:</td>
    <td align="left"><INPUT id ="phone" NAME="phone" VALUE=""><BR>
    </td>
    <td align="right">&nbsp;</td>
    <td align="left"><INPUT TYPE='submit' id = "submit" NAME='submit' VALUE='Send'></td>
    </tr>
    </table>
    </form>
    <?php

    }
    else
    {


    $insertSql "INSERT INTO members (firstname, lastname, phone) VALUES ('$fname', '$lname', '$phone')";
     
    $result mysql_query($insertSqL)  or die("Insert Query failed: " mysql_error());
    $selectSQL "SELECT FROM memebers where id = "mysql_insert_id();
    $result mysql_query($selectSQL)
                          or die(
    "Select Query failed: " mysql_error());
    echo 
    "<TABLE BORDER='1'>";
                     echo 
    "<TR>";
                     echo 
    "<TH>First Name</TH><TH>Last Name</TH><TH>Phone</TH>";
                     echo 
    "</TR>";

                     while (
    $row mysql_fetch_array($result))
                     {
                         echo 
    "<TR>";
                         echo 
    "<TD>"$row['fname'], "</TD><TD>"$row['lname'], "</TD><TD>"$row['phone'], "</TD>";
                         echo 
    "</TR>";
                     }

                     echo 
    "</TABLE>";

                     
    mysql_close($connection);
    }                 
    ?>

    </body>
    </html>

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Change your PHP into:
    PHP Code:
    <?php 
    //Database Connection 
     
    $msdb mysql_connect("localhost""user""pass"); 
    $db mysql_select_db("test"$msdb) or die(mysql_error()); 
    $fname $_POST['fname'];
    $lname $_POST['lname'];
    $phone $_POST['phone'];
    if (isset(
    $_POST['send']))  
    {
    $insertSql "INSERT INTO members (firstname, lastname, phone) VALUES ('".$fname."','".$lname."','".$phone."')"
    $result mysql_query($insertSql)  or die("Insert Query failed: " mysql_error()); 
    $selectSQL "SELECT * FROM members where id = "mysql_insert_id(); 
    $result mysql_query($selectSQL) or die("Select Query failed: " mysql_error()); 
    echo 
    "<TABLE BORDER='1'>"
                     echo 
    "<TR>"
                     echo 
    "<TH>First Name</TH><TH>Last Name</TH><TH>Phone</TH>"
                     echo 
    "</TR>"
                     while (
    $row mysql_fetch_array($result)) 
                     { 
                    echo 
    "<TR>"
                    echo 
    "<TD>"$row['firstname'], "</TD><TD>"$row['lastname'], "</TD><TD>"$row['phone'], "</TD>"
                    echo 
    "</TR>"
                    } 

                     echo 
    "</TABLE>"

                     
    mysql_close($msdb); 
    }                  
    ?>
    Hope that helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear Luverboy (haha):

    I know this was not your question, but just so you know, this is a perfect example of when tables are NOT needed. You can easily replace your table with a <div> and use css to position your form. The fields will all line up nicely if you simply assign a width to the label in your css. Using tables when not necessary is a bad habit to avoid.

    Just my 2 cents.

  4. #4
    Join Date
    Aug 2008
    Location
    Smiths, AL
    Posts
    164
    Thanks
    30
    Thanked 5 Times in 5 Posts

    Default

    kay I have a code on the site now that works but it's only for a username and password.

    I have modified it to add other fields. I don't get any error codes when creating a member but it's not writing the data to the database anymore. Only thing I changed was this info:

    PHP Code:
     "INSERT INTO users (first_name, last_name, email, gender, city, state, country, username, password) 
    VALUES ('"
    .$_POST[first_name]."','".$_POST[last_name]."','".$_POST[email]."','".$_POST[gender]."','".$_POST[city]."','".$_POST[state]."','".$_POST[country]."','".$_POST[username]."','".$_POST[pass]."','".$_POST[pass2]."')"
    //VALUES ('".$_POST['username']."', '".$_POST['pass']."')"; //reference Code 
    $add_member mysql_query($insert); 
    Got any clues as to what I did wrong?

  5. #5
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    You left the single quotes out...

    Code:
      $_POST['first_name']
    Last edited by kuau; 08-13-2008 at 04:36 AM. Reason: added highlight

  6. #6
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    You should make use of mysql error reporting.
    Try to add highlighted everytime you run a query so as to realize where/what the error is:
    Code:
    $add_member = mysql_query($insert) or die(mysql_error());
    It is your responsibility to die() if necessary….. - PHP Manual
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  7. #7
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear Rangana: How do you add the yellow highlight? I tried using the A with the line under it and it didn't work. Thanks. e

  8. #8
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    It's this image: .

    You can however make use of the bbcode: [icode][/icode].

    Hope that helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  9. The Following User Says Thank You to rangana For This Useful Post:

    kuau (08-13-2008)

  10. #9
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Oh, cool! I thought that #i was for italics. Thanks

  11. #10
    Join Date
    Aug 2008
    Location
    Smiths, AL
    Posts
    164
    Thanks
    30
    Thanked 5 Times in 5 Posts

    Default

    Quote Originally Posted by rangana View Post
    You should make use of mysql error reporting.
    Try to add highlighted everytime you run a query so as to realize where/what the error is:
    Code:
    $add_member = mysql_query($insert) or die(mysql_error());
    When I run the error report I get this phase error
    "Parse error: parse error, unexpected T_LOGICAL_OR in..."

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
  •