0

I have two php files that are called form1.php, and demo12.php. This is the result of the form1.php file:

<form action="demo12.php" method="post" />
<p>Name <input type="text" name="input1" /></p>
<p>Age <input type="text" name="input2" /></p>
<input type="submit" value="Submit" />
</form>

Then the code of my demo12.php is:

<?php

define('DB_NAME', 'form1');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

$value = $_POST['input1'];
$value2 = $_POST['input2'];

$sql = "INSERT INTO user_info (name, age) VALUES ('$value', '$value2')";

if (!mysql_query($sql)) {
    die('Error: ' . mysql_error());
}

$result = mysql_query("SELECT * FROM name") or die(mysql_error());

while($row = mysql_fetch_assoc($result)) {
    echo $row["name"];
}

?>

I am able to enter a name, age, then click the submit button, which brings the user to the demo12.php page and inserts the data into the MySQL database. The problem is, I want to retrieve and then display all from the name column onto the page. So I have a database named form1, them a table named user_info. In that the 3 columns I have are ID, name, and age. To display all from the name column I have this at the near-end of my script:

$result = mysql_query("SELECT * FROM name") or die(mysql_error());

while($row = mysql_fetch_assoc($result)) {
    echo $row["name"];
}

Then when the data is inserted, then when it is going to display the all of the names in the table it has an error:

Table 'form1.name' doesn't exist

But it has no errors when I take that code out. Why is it not working?

2
  • 1
    Warning: mysql_* extension is depreciated please learn PDO or mysqli_* Commented Jul 2, 2015 at 22:43
  • oh my god what am I looking at? Commented Jul 2, 2015 at 22:45

3 Answers 3

1

You are inserting into the database user_info and then trying to select form the database name. Try changing your select to "SELECT * FROM user_info".

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

1 Comment

or even SELECT name FROM user_info
0

You should have written:

$result = mysql_query("SELECT * FROM user_info") or die(mysql_error());

and not

$result = mysql_query("SELECT * FROM name") or die(mysql_error());

Comments

0

Please replace at your last part with following

$result = mysql_query("SELECT name FROM user_info") or die(mysql_error());

while($row = mysql_fetch_assoc($result))
{
    echo $row['name'];
}

Comments

Your Answer

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