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.
Results 1 to 8 of 8

Thread: form and php

  1. #1
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts

    form and php

    I tried to make a form that allows user to add their first and last name to my db... it keeps telling me that the query failed. Help please!

    PHP Code:
    <html>
    <head>
    <title>Add New MySQL User</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <?php
    if(isset($_POST['add']))
    {
    include 
    'config.php';
    include 
    'opendb.php';

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

    $query "INSERT INTO Member (mfname, mlname, insert_priv, insert_ priv) VALUES ('$fname', '$lname', 'Y', 'Y')";
    mysql_query($query) or die('Error, insert query failed');

    $query "FLUSH PRIVILEGES";
    mysql_query($query) or die('Error, insert query failed');

    include 
    'closedb.php';
    echo 
    "New MySQL user added";
    }
    else
    {
    ?>
    <form method="post">
    <table width="400" border="0" cellspacing="1" cellpadding="2">
    <tr> 
    <td width="100">First Name</td>
    <td><input name="fname" type="text" id="fname"></td>
    </tr>
    <tr> 
    <td width="100">Last Name</td>
    <td><input name="lname" type="text" id="lname"></td>
    </tr>
    <tr> 
    <td width="100"> </td>
    <td> </td>
    </tr>
    <tr> 
    <td width="100"> </td>
    <td><input name="add" type="submit" id="add" value="Add Me"></td>
    </tr>
    </table>
    </form>
    <?php
    }
    ?>
    </body>
    </html>

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Add mysql_error() onto the statement, as well:
    mysql_query($query) or die('Error, insert query failed ' . mysql_error());

    Good luck;

  • #3
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    mfnamemlnameinsert_privinsert_ priv 
    Why do you have insert_ priv twice?

  • #4
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have mfname, mlname, and memail field in my Member table. I'm pretty newbie to php and mysql... so I thought maybe 2 insert privs mean that I let user insert to those 2 particular fields mfname and mlname..... but it never got inserted. I don't know what's wrong with that.

  • #5
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You only need to specify the fields and values that you want to change

    PHP Code:
    $query "INSERT INTO Member (mfname, mlname) VALUES ('$fname', '$lname')"
    or

    PHP Code:
    $query "INSERT INTO Member (mfname, mlname, memail) VALUES ('$fname', '$lname', '$email')"
    http://www.w3schools.com/sql/sql_insert.asp

  • #6
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I got error that insert query failed, what's wrong? I did like what you told me. but the row indeed is added to .

  • #7
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    problem is solved but why there are "????" under my form? and the email address is not saved to the Mysql
    Last edited by cgibie; 02-20-2006 at 11:01 AM.

  • #8
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    problem is solved


  •  

    Posting Permissions

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