3

My code inserts an empty record into the MySQL table "table1" instead of getting what inserted in the field "Name" in form1.html. Any idea why it inserts an empty record instead of what the user entered in the field?

form1.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"
    >
<html lang="en">
<head>
    <title>Insert Your Name</title>
</head>
<body>
    <h3> Insert Your Name</h3>
    <form action="form1.php" method="post">
        <input type="text" name="Name">
            <input type="Submit" value="Submit" name="Submit">
    </form>
</body>
</html>

form.php

> <?php 
> $connection =
> mysql_connect("localhost","root","")
> or die ("Couldn't Connect To Server");
> $db = mysql_select_db("db1",
> $connection) or die ("Couldn't Select
> Database"); $query = "CREATE TABLE IF
> NOT EXISTS table1 (Name VARCHAR(20))";
> $result = mysql_query($query) or die
> ("Query Failed: " . mysql_error());
> $query = "INSERT INTO table1 (Name)
> VALUES ('$_post[Name]')"; $result =
> mysql_query($query) or die ("Query
> Failed: " . mysql_error()); $query =
> "SELECT * FROM table1"; $result =
> mysql_query($query) or die ("Query
> Failed: " . mysql_error());
>     echo "<TABLE BORDER = '1'>";
>     echo "<TR>";
>     echo "<TH>Name</TH>";
>     echo "</TR>";
>     
>     while ($row = mysql_fetch_array($result))
>     {
>         echo "<TR>";
>         echo "<TD>", $row['name'], "</TD>";
>         echo "</TR>";
>     }
>     echo "</TABLE>";
>     mysql_close($connection); ?>
4
  • 5
    +1 seldom newbie with reputation 1 able to post question with great readability :) Commented Dec 4, 2010 at 16:26
  • Thanks ajreal! I hope this is the place I'll be able to find answers:) Commented Dec 4, 2010 at 16:29
  • Have you looked in the database to verify if the name field is being saved there? That will determine if the problem is in your insert or your select. Commented Dec 4, 2010 at 16:36
  • Not sure I understand, Surreal Dreams. Do you mean that I hsould check if the Name field is saved properly in the database? because it is. Commented Dec 4, 2010 at 16:40

4 Answers 4

2

Instead of

$query = "INSERT INTO table1 (Name) VALUES ('$_post[Name]')";

Try this

$query = "INSERT INTO table1 (Name) VALUES ('" .
          mysql_real_escape_string($_post['Name'],$db) . "')";
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks to you to Roy-same question as I asked Pradeep: the php code that shows a table with the data from "table1" does not show the new recoed entered. Ideas?
Propably it's the same problem: case.echo "<TD>", $row['Name'], "</TD>";
0
 $query = "INSERT INTO table1 (Name)
 VALUES ('{$_POST[Name]}')"; $result =
 mysql_query($query) or die ("Query
 Failed: " . mysql_error());

$_POST and $_GET are uppercase.

change the display code to:

 while ($row = mysql_fetch_array($result))
     {
         echo "<TR>";
         echo "<TD>", $row['Name'], "</TD>";
         echo "</TR>";
     }

4 Comments

Thanks to you to Horia! Wow, it's reall ynice how quickly stuff is answered around here. HoI will refer my second question to you too: the php code that shows a table with the data does not show the new recoed entered. Ideas?
also, try to keep all things lowercase in the future. Less headaches that way.
Works Perectly! Thanks Horia. So the problem is in the letter case, eh? Good to know.
When I get the reputation, when I get the reputation:)
0

Please change this

<input type="text" name="fname">


$query = "INSERT INTO table1 (Name)
 VALUES ('".mysql_real_escape_string($_POST[fname],$db)."')"; 

3 Comments

However, there is a different problem now-the php code that shows a table with the data does not show the new recoed entered. Ideas?
after insert please redirect the page
Solved already, Pradeep, buy anyhow-what do you mean redirec the page? Should i build a new php file, and divide it to one that stores the data (ehich could be the HTML itself?) and one that shows the data?
0
 " . ($_POST['Fname'] != "") ? $_POST['Fname'] : "No Name" ."

1 Comment

Thanks Geeo! What is the reason for the "No Name"? So if somebody inserted a blank field, that it would just write "no name"?

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.