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 15 of 15
  1. #1
    New Coder
    Join Date
    Oct 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem w/ HTML form

    I am trying to insert data from this html form into a mysql database.. however whenever i hit submit I only receive a blank page.... and when I check phpmyadmin there are no rows added....

    heres my html doc:

    Code:
    <html>
    <body>
    
    		  <form method="post"  action="test.php">
    <fieldset> 
    <legend> Reserve a Copy </legend> 
    
    <label for="name">Name:<input type="text" name="name" id="name" /></label><br />
    <label for="address">Address:<input type="text" name="address" id="address" /></label><br />
    <label for="street">Street:<input type="text" name="street" id="street" /></label><br />
    <label for="state">State:<input type="text" name="state" id="state" /></label><br />
    <label for="zip">Zip:<input type="text" name="zip" id="zip" /></label><br />
    <label for="email">Email:<input type="text" name="email" id="email" /></label><br /> 
    <label for="tel">Telephone:<input type="text" name="tel" id="tel" /></label><p><br /></p><p>
    <label for="comments">Comments:<textarea name="comments" id="comments" /></textarea></label><br />
    </p><p>&nbsp;</p><p>&nbsp;</p><p><input type="submit" value="Submit" class="submit" /> 
    
       </TD>
    </TR>
    <TR>
    </fieldset> 
    
    
    
    </table>
    </form>
    </html>
    </body>
    and here is the php doc.. 'test.php'


    Code:
    	<?php
    	//Change the user and pw to mySql name and password
    $hostname = 'localhost'
    $user = 'root'
    $password = 'xxxx
    $database = 'derbyworld'
    $table = 'info'
    
    //connect to db, and select db
    mysql_connect($hostname,$user,$password) or die(Could not connect to $database);
    mysql_select_db($database) or or die(mysql_error());
    
    //convert all the posts to variables
    $name = $_POST['name'];
    $address = $_POST['addres'];
    street = $_POST['street'];
    $state = $_POST['state'];
    $zip = $_POST['zip'];
    $email = $_POST['email'];
    $tel = $_POST['tel'];
    $comment = $_POST['comments'];
    
    //table = derbyworld
    //table columns = name,address,street,state,zip,email,tel,comments,id
    $sqlquery = mysql_query("INSERT INTO $table
    	values'$_POST[name]', '$_POST[address]', '$_POST[street]', '$_POST[state]', '$_POST[street]', '$_POST[zip]', '$_POST[email]',
    	'$_POST[tel]', '$_POST[comments]')";
    	
    	$sqlquery = mysql_query($'sqlquery');
    	mysql_close();
    	
    
    	
    //confirm 
    echo "Query Finished";
    }
    ?>
    anyone know what im doing wrong?? I tried to get $php_self to work but it gave me a bad request...

    I am a php newb and I would appreciate any insight as to what im doing wrong.

    Thanks a bunch

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Couple things, you forgot a $ sign when declaring your 'street' variable, also, you were running mysql_query() twice? Also, what is the field id? is it an auto increment field? but assuming that it is, you can try this out:
    PHP Code:
    <?php
        
    //Change the user and pw to mySql name and password
    $hostname 'localhost'
    $user 'root'
    $password 'xxxx
    $database = '
    derbyworld'
    $table = '
    info'

    //connect to db, and select db
    mysql_connect($hostname,$user,$password) or die(Could not connect to $database);
    mysql_select_db($database) or or die(mysql_error());

    //convert all the posts to variables
    $name = $_POST['
    name'];
    $address = $_POST['
    addres'];
    $street = $_POST['
    street'];
    $state = $_POST['
    state'];
    $zip = $_POST['
    zip'];
    $email = $_POST['
    email'];
    $tel = $_POST['
    tel'];
    $comment = $_POST['
    comments'];

    //table = derbyworld
    //table columns = name,address,street,state,zip,email,tel,comments,id

    $sqlquery = mysql_query("INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values '
    $name', '$address', '$street', '$state', '$zip', '$email', '$tel', '$comment'") or die(mysql_error());

    if(!$sqlquery)
    {
        echo '
    Error';
    }
    else
    {
        echo '
    Query was good;
    }

    ?>

  • #3
    New Coder
    Join Date
    Oct 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oops forgot to take those out.. but i tried what you had posted and I still just get a blank page.. is the html ok? should I be using get rather then post? in the sql query do i need to do it like:

    Code:
    $sqlquery = mysql_query("INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values '$_POST['name];
    the id column is autoincrement with int(11)..

    Thanks for your quick reply
    Last edited by Smartmonkey; 06-06-2005 at 07:13 AM.

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Location
    Tokyo, Japan
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not sure if this is the error or just a typo but in your code you have...

    PHP Code:
    //table = derbyworld
    //table columns = name,address,street,state,zip,email,tel,comments,id

    $sqlquery mysql_query("INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values '$name', '$address', '$street', '$state', '$zip', '$email', '$tel', '$comment'") or die(mysql_error()); 
    so I assume that your table name is 'derbyworld' (based on the first comment)

    but above you have $table = 'info';... (and you need to close the quote for the value in $password)
    PHP Code:
    $hostname 'localhost'
    $user 'root'
    $password 'xxxx
    $database = '
    derbyworld'
    $table = '
    info 
    shouldn't it be $table = 'derbyworld'; ?
    It also appears that you're missing ";" at the end of your lines.

    try this:
    PHP Code:
    $hostname 'localhost';
    $user 'root';
    $password 'xxxx';
    $database 'derbyworld';
    $table 'derbyworld'

    Yak
    Last edited by Yakisoba; 06-06-2005 at 07:41 AM.

  • #5
    New Coder
    Join Date
    Oct 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oops i meant that to say database name, but yeah the db is derbyworld, table is info.

    I added the ';' to the end and I still get the same thing.... is there a reason that I don't get the mysql_error()) ?? I just get a blank white page.. doesn't appear to do anything. I believe all the typo's should be fixed heh.. but heres what I have now:

    Code:
    <?php 
        //Username / PW 
    $hostname = 'localhost';
    $user = 'root';
    $password = 'password';
    $database = 'derbyworld'; 
    $table = 'info';
    
    //connect to db, and select db 
    mysql_connect($hostname,$user,$password) or die(Could not connect to $database); 
    mysql_select_db($database) or or die(mysql_error()); 
    
    //convert all the posts to variables 
    $name = $_POST['name']; 
    $address = $_POST['address']; 
    $street = $_POST['street']; 
    $state = $_POST['state']; 
    $zip = $_POST['zip']; 
    $email = $_POST['email']; 
    $tel = $_POST['tel']; 
    $comments = $_POST['comments']; 
    
    //database = derbyworld 
    //table = info
    //table columns = name,address,street,state,zip,email,tel,comments,id 
    
    $sqlquery = mysql_query("INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values '$name', '$address', '$street', '$state', '$zip', '$email', '$tel', '$comment'") or die(mysql_error()); 
    
    if(!$sqlquery) 
    { 
        echo 'Error'; 
    } 
    else 
    { 
        echo 'Query was good'; 
    } 
    
    ?>
    and this was the sql I used to create the 'info' table:

    Code:
    SQL-query: 
    CREATE TABLE `info` (
    `address` VARCHAR( 100 ) NOT NULL ,
    `comments` VARCHAR( 500 ) NOT NULL ,
    `email` VARCHAR( 50 ) NOT NULL ,
    `name` VARCHAR( 50 ) NOT NULL ,
    `state` VARCHAR( 10 ) NOT NULL ,
    `street` VARCHAR( 100 ) NOT NULL ,
    `tel` VARCHAR( 12 ) NOT NULL ,
    `zip` VARCHAR( 7 ) NOT NULL ,
    `id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
    UNIQUE (
    `id` 
    )
    );
    Last edited by Smartmonkey; 06-06-2005 at 09:04 AM.

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Location
    Tokyo, Japan
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this may be another typo...

    you have "or" written twice, and you need to put quotes around "Could not connect to $database"

    PHP Code:
    //connect to db, and select db 
    mysql_connect($hostname,$user,$password) or die(Could not connect to $database); 
    mysql_select_db($database) or or die(mysql_error()); 
    did you mean:
    PHP Code:
    //connect to db, and select db 
    mysql_connect($hostname,$user,$password) or die('Could not connect to $database'); 
    mysql_select_db($database) or die(mysql_error()); 
    I order to get mysql_error on a webpage you have to "echo" it (i think).

    i.e.
    PHP Code:
    mysql_select_db($database) or die(echo mysql_error()); 
    give that a shot

    Yak
    Last edited by Yakisoba; 06-06-2005 at 09:27 AM.

  • #7
    New Coder
    Join Date
    Oct 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well that at least got me:

    Code:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''asd', 'asda', 'asd', 'asd', 'asd', 'asd', 'asd', ''' at line 1
    I was just putting in garbage for the info.. but is it my query that is messed up do I need to do
    Code:
    $sqlquery = mysql_query("INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values '$POST_['name'],
    ?? I appreciate your help

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Location
    Tokyo, Japan
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try this...

    Code:
    $sqlquery = mysql_query("INSERT INTO $table (name, address, street, state, zip, email, tel, comments) VALUES ('$name', '$address', '$street', '$state', '$zip', '$email', '$tel', '$comment')";
    make sure all the col names and variable names I typed are correct.

    Yak

  • #9
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Also, tacking error_reporting(E_ALL) at the top of your script is helpful for tracking down errors.

  • #10
    New Coder
    Join Date
    Oct 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hrm, tried your sqlquery and I don't get anything anymore.. just a blankpage

    I also added

    Code:
    // error reporting
    error_reporting(E_ALL);
    at the top of my script and still a blank page..

  • #11
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    I had some typo's, but try this out:
    html form:
    PHP Code:
    <html>
    <
    body>

              <
    form method="post"  action="test.php">
    <
    fieldset>
    <
    legendReserve a Copy </legend>

    <
    label for="name">Name:<input type="text" name="name" id="name" /></label><br />
    <
    label for="address">Address:<input type="text" name="address" id="address" /></label><br />
    <
    label for="street">Street:<input type="text" name="street" id="street" /></label><br />
    <
    label for="state">State:<input type="text" name="state" id="state" /></label><br />
    <
    label for="zip">Zip:<input type="text" name="zip" id="zip" /></label><br />
    <
    label for="email">Email:<input type="text" name="email" id="email" /></label><br />
    <
    label for="tel">Telephone:<input type="text" name="tel" id="tel" /></label><p><br /></p><p>
    <
    label for="comments">Comments:<textarea name="comments" id="comments" /></textarea></label><br />
    </
    p><p>&nbsp;</p><p>&nbsp;</p><p><input type="submit" value="Submit" class="submit" />

       </
    TD>
    </
    TR>
    <
    TR>
    </
    fieldset>



    </
    table>
    </
    form>
    </
    html>
    </
    body
    php page:
    PHP Code:
    <?php
        
    //Change the user and pw to mySql name and password
    $hostname 'localhost';
    $user 'root';
    $password 'your password';
    $database 'derbyworld';
    $table 'info';

    //connect to db, and select db
    mysql_connect($hostname,$user,$password) or die(mysql_error());
    mysql_select_db($database) or die(mysql_error());

    //convert all the posts to variables
    $name $_POST['name'];
    $address $_POST['address'];
    $street $_POST['street'];
    $state $_POST['state'];
    $zip $_POST['zip'];
    $email $_POST['email'];
    $tel $_POST['tel'];
    $comment $_POST['comments'];

    //table = derbyworld
    //table columns = name,address,street,state,zip,email,tel,comments,id


    $query "INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values('$name', '$address', '$street', '$state', '$zip', '$email', '$tel', '$comment')";
    $sqlquery mysql_query($query) or die(mysql_error());

    if(!
    $sqlquery)
    {
        echo 
    'Error';
    }
    else
    {
        echo 
    'Query was good';
    }

    ?>
    Last edited by Brandoe85; 06-07-2005 at 03:38 AM. Reason: ok, so I had alot of typo's...

  • #12
    New Coder
    Join Date
    Oct 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    right on that worked!, thanks for your help.. so what was the problem??

    I found the typos in my html form (oops) and the main difference I saw with the php was
    Code:
    query = "INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values('$name', '$address', '$street', '$state', '$zip', '$email', '$tel', '$comment')"; 
    $sqlquery = mysql_query($query) or die(mysql_error());
    is that all that you did to fix the problem??

    I really appreciate your help with this one

    also, how could I have gotten this to work with $PHP_SELF?? would I need to change the $query to $PHP_SELF['value'] ??

    again thanks alot for your help

  • #13
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Your welcome. Basically, I ended the double quotes too early on the first piece of code I posted, and I just separated it into the $query variable, helps me read it better. You could use PHP_SELF, I take it you want it to submit to itself?

    somepage.php:
    PHP Code:
    <html>
    <body>

              <form method="post"  action="<?php $_SERVER['PHP_SELF'?>">
    <fieldset>
    <legend> Reserve a Copy </legend>

    <label for="name">Name:<input type="text" name="name" id="name" /></label><br />
    <label for="address">Address:<input type="text" name="address" id="address" /></label><br />
    <label for="street">Street:<input type="text" name="street" id="street" /></label><br />
    <label for="state">State:<input type="text" name="state" id="state" /></label><br />
    <label for="zip">Zip:<input type="text" name="zip" id="zip" /></label><br />
    <label for="email">Email:<input type="text" name="email" id="email" /></label><br />
    <label for="tel">Telephone:<input type="text" name="tel" id="tel" /></label><p><br /></p><p>
    <label for="comments">Comments:<textarea name="comments" id="comments" /></textarea></label><br />
    </p><p>&nbsp;</p><p>&nbsp;</p><p><input type="submit" value="Submit" name="sub" class="submit" />

       </TD>
    </TR>
    <TR>
    </fieldset>

    </table>
    </form>
    <?php
    if(isset($_POST['sub']))
    {
            
    //Change the user and pw to mySql name and password
        
    $hostname 'localhost';
        
    $user 'root';
        
    $password 'yourpass';
        
    $database 'derbyworld';
        
    $table 'info'
        
        
    //connect to db, and select db
        
    mysql_connect($hostname,$user,$password) or die(mysql_error());
        
    mysql_select_db($database) or die(mysql_error());

        
    //convert all the posts to variables
        
    $name $_POST['name'];
        
    $address $_POST['address'];
        
    $street $_POST['street'];
        
    $state $_POST['state'];
        
    $zip $_POST['zip'];
        
    $email $_POST['email'];
        
    $tel $_POST['tel'];
        
    $comment $_POST['comments'];

        
    //table = derbyworld
        //table columns = name,address,street,state,zip,email,tel,comments,id


        
    $query "INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values('$name', '$address', '$street', '$state', '$zip', '$email', '$tel', '$comment')";
        
    $sqlquery mysql_query($query) or die(mysql_error());

        if(!
    $sqlquery)
        {
            echo 
    'Error';
        }
        else
        {
            echo 
    'Query was good';
        }
    }
    ?>
    </body>
    </html>

  • #14
    New Coder
    Join Date
    Oct 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh sweet so all I really have to add is:

    Code:
    action="<?php $_SERVER['PHP_SELF'] ?>">
    and

    Code:
    if(isset($_POST['sub'])) 
    {
    ?

    oh side note.. what do you use for your php editor? do u know of a good one?

  • #15
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Yep thats it, and make sure you name your submit button 'sub', I use textpad for my coding, but heres a thread on editors


  •  

    Posting Permissions

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