Jump to content

Accepting user input question


parabolate

Recommended Posts

I manage a simple website for a small business - nothin fancy. I'm interested using some php to enhance the site a bit. I'm looking to accept some user input and put it into a database then display it back to them on another page. What php functions would be best to use to accomplish this?

 

Thanks for the input!

Link to comment
Share on other sites

The code ive posted shows example of how to connect to db, have a post form, insert form value into database, retrieve database rows and echo them.

 

PLEASE NOTE- i have done NO security its completely raw and vulnerable but it shows you an example which you can work on.

 

<?php
//===== This code connects to database =======
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'TEST'; //this is the name of the database
mysql_select_db($dbname);
//============================================


//============================================
//This code handles the form and inserting into the text into the database
    if(isset($_POST['submit'])){
        $text = $_POST['text'];
        
        //Adding security checks here is essential!

        //Inserts Data into Database
        mysql_query("INSERT INTO users_input set text='$text'");
        
    }
//=============================================

?>
<html>
    <form method="POST">
        <input type="text" name="text"/>
        <input type="submit" name="submit" value="Submit"/>
    </form>
    
    <?php
    //========================================== 
    // This code selects all rows from db and echos them out
    $query = mysql_query("SELECT * FROM users_input ORDER BY text");
        while ($response=mysql_fetch_array($query)){
            echo $response['text']."<br/>";
        }
    //==========================================   
    ?>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.