-3

I have an input text area where the user enter his name and i want his name to be shown in another page.

This is what i've tried so far:

HTML of the form:

<html>
<form method="post" name="input" action="name.php" >
Name:<br/>
<input name="user" type="text"/><br/>
<input type="submit" name="Submit" value="insert" />
</form>

name.php

<?php
mysql_connect("localhost", "root", "root") or die("Connection Failed");
mysql_select_db("user")or die("Connection Failed");
session_start();
$_SESSION['user'] = $_POST['user'];
$user = $_POST['user'];
$query = "INSERT INTO test(user)VALUES('$user')";
if(mysql_query($query)){
echo "inserted";}
else{
echo "fail";}
?>

and the page where i want the name to be shown:

<?php
$connection = mysql_connect('localhost', 'root', 'root');
mysql_select_db('user');

echo $user;

mysql_close(); 
?>

Thanks in advance

  • So do you have any error..? – 웃웃웃웃웃 Aug 01 '13 at 06:03
  • 2
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Aug 01 '13 at 06:03

2 Answers2

0

Before printing the value for $user with echo $user;, you've to fetch its value either from database with a SELECT query or from the SESSION Right now you're opening a connection to the database on the page where you want to print $user but are not fetching data from the table. $user has no value assigned here.

Fallen
  • 4,435
  • 2
  • 26
  • 46
0

You haven't done anything to populate the $user variable in the final page.

You need to make an SQL query or read some form data.

(You also need to defend against XSS attacks)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335