0

I'm creating a php-post form, containing: Who, What, Where, Contact and date_created. I've made a database with these rows. enter image description here

Here's my HTML Form code:

<form id="contactform" action="post.php"> 
            <p class="contact"><label for="who">Who</label></p> 
            <input id="who" name="who" placeholder="Who are you? (First & Second name)" required="" tabindex="1" type="text"> 

            <p class="contact"><label for="email">What</label></p> 
            <input id="what" name="what" placeholder="What do you want?" required="" type="text"> 

            <p class="contact"><label for="username">Where</label></p> 
            <input id="where" name="where" placeholder="Country, City, Street..." required="" tabindex="2" type="text"> 

            <p class="contact"><label for="password">Contact</label></p> 
            <input type="text" id="contact" name="contact" placeholder="Phone number or email"required=""> 

                <br><br>

        <input class="buttom" name="submit" id="submit" tabindex="5" value="Submit" type="submit">   

And here's the php post.php code:

<?php
    // Grab our POSTed form values
    // Note that whatever is enclosed by $_POST[""] matches the form input elements
    $who = $_POST["who"];
    $what = $_POST["what"];
    $where = $_POST["where"];
    $contact = $_POST["contact"];

    // Connect to our DB with mysql_connect(<server>, <username>, <password>)
    $sql_connection = mysql_connect("server_name", "admin", "password");

    mysql_select_db("database_name", $sql_connection);

    $sql = "INSERT INTO content (
                who,
                what,
                where,
                contact,
                date_created
            )
            VALUES (
                '$who',
                '$what',
                '$where',
                '$contact',
                NOW()
            )";

    mysql_query($sql, $sql_connection);

    mysql_close($sql_connection);
?>

When I try to post something, nothing is happening. The screen is just white, the database is empty and the url is like this:

http://my-website.com/post.php?who=Firstname+Secondname&what=Some+sentences+here-and&where=America&[email protected]&submit=Submit%21

6
  • You forgot to specify the method in <form id="contactform" action="post.php"> Thus it's sending a $_GET var :p Commented Mar 3, 2013 at 13:56
  • So what should I do then? Commented Mar 3, 2013 at 13:57
  • Change it to <form id="contactform" action="post.php" method="post"> Commented Mar 3, 2013 at 13:57
  • 2
    @downvoters I think the OP has put some efforts in asking the question, so at least don't downvote even if it's a silly question ... Commented Mar 3, 2013 at 13:59
  • Thanks, but now it directs me to post.php with a blank page, and the database is still empty... @HamZaDzCyberDeV Commented Mar 3, 2013 at 14:04

2 Answers 2

2

Just as HamZa DzCyberDeV said, you didn't specify which method you're using in <form> tag.

For situations when you're POSTing something in your database, just as you are now - use method="post" and for forms when you're searching for something, use method="get". In case of using post method, your URL will change to only my-website.com/post.php and in case of using get method, your URL will change to something like my-website.com/post.php?... (where your things which you're getting are going) - just how you got URL after submitting.

The screen is just white because post.php (where you're going after clicking on submit button) doesn't contain anything to send to output, which you can easily do with echo.

For instance, you can make a new html page which will be written down with echo:

echo '
<html
<body>
This is my website!
</body>
</html>

';

Also, what you could do is to use include() php script which has already formed HTML, or you can check out here for some other redirect methods: http://php.about.com/od/learnphp/ht/phpredirection.htm

Just remember that PHP is language which server is processing and only HTML tags (with CSS and JS) are sent to other browser to be read.

For more about POST and GET method you can read here:

http://php.net/manual/en/reserved.variables.post.php

http://php.net/manual/en/reserved.variables.get.php

Sign up to request clarification or add additional context in comments.

11 Comments

Okey, thanks! I've added method="post" to the form. I've also added or die("error". mysql_error()); to the database connection to see if there's some errors. But when I try to post, the post.php is still empty and nothing has been posted to the database...
Are you sure you made good connectiong to database? Make it like this to be sure: // 1. Create a database connection $sql_connection= mysql_connect("localhost","root",""); if (!$sql_connection) { die("Database connection failed: " . mysql_error()); } // 2. Select a database to use $db_select = mysql_select_db("database_name",$sql_connection); if (!$db_select) { die("Database selection failed: " . mysql_error()); } I've put this "localhost" "root" and "" for servers because I'm using XAMPP and I've made those in settings to be like that while installing it.
Ok, now I've done all that. But it's still white when posting... Wierd.
Is it saving in database? I wrote you in my post why it's white - you're not echoing anything in script. You should make redirect or include some other script which is echoing something or has some HTML content.
Nope, it's not saving in database.
|
2

why don't you try this to get an error or a clue to what is going wrong, enclose your code in try and catch blocks:

    try {

         // your code


    } catch ( Exception $e ) {

         echo $e->getMessage();

    }

3 Comments

Okey, so the error I'm getting is: Error: 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 'where, contact) VALUES ('fsdnkn', 'fsdjfn', 'fndsfk', 'njdsfnsdj')' at line 1
make sue that there is a space before the word VALUES
don't indent or use newlines in your sql statement, just put it on one line. see what happens

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.