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
https://forums.phpfreaks.com/topic/250217-accepting-user-input-question/
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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.