0

Hello dear why i always fail to learning php, would be grateful if someone can help me. :'( i was followed step by step here :

http://www.w3schools.com/php/php_mysql_insert.asp

but when i click button submit query nothing happen, just show a blank white screen and i dont see new data on database?

enter image description here

<html>
<body>

<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
Age: <input type="text" name="age">
<input type="submit">
</form>

</body>
</html> 

    <?php
    $con=mysqli_connect("localhost","root","","garutexpress");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    $sql="INSERT INTO Persons (FirstName, LastName, Age)
    VALUES
    ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

    if (!mysqli_query($con,$sql))
      {
      die('Error: ' . mysqli_error($con));
      }
    echo "1 record added";

    mysqli_close($con);
   ?> 

if data successfull added it should give echo "1 record added"; but i never see this message.

3
  • Danger: You are vulnerable to SQL injection attacks that you need to defend yourself from. Commented Apr 24, 2014 at 6:53
  • Note // escape variables for security part in the tutorial. Commented Apr 24, 2014 at 6:53
  • thanks for suggestion dear, i have ever hear about sql injection maybe when i was understand PHP and sql step by step i will learning how to defend myself :)) Commented Apr 24, 2014 at 7:01

5 Answers 5

1

Your table name is "persons", not "Persons"

When you make a query, your table name has to be the same as in your database. If you look in phpMyAdmin , your table is "persons" with lowercase

Edited according to : @I Can Has Cheezburger

Please change the name of your table in your code like and make sure about to wrap quotes accordingly :

  <?php
    $con=mysqli_connect("example.com","peter","abc123","my_db");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }



    $sql='INSERT INTO persons (Firstname, Lastname, Age)
VALUES
("'.$_POST['firstname'].'","'.$_POST['lastname'].'","'.$_POST['age'].'");

    if (!mysqli_query($con,$sql))
      {
      die('Error: ' . mysqli_error($con));
      }
    echo "1 record added";

    mysqli_close($con);
   ?> 
Sign up to request clarification or add additional context in comments.

4 Comments

so uppercase and lowercase will take effect?
use the same in both ( table name and when you query ) if you have "persons" in database, when you query, put "persons" also
oh okaay, you are right deaar .. thank you so much :))
The columns are also mismatched (FirstName => Firstname and LastName => Lastname).
1

Common error, you are not wrapping your POST array index with quotes.

Do it like:

$sql='INSERT INTO persons (FirstName, LastName, Age)
VALUES
("'.mysqli_real_escape_string($con,$_POST['firstname']).'","'.mysqli_real_escape_string($con,$_POST['lastname']).'","'.mysqli_real_escape_string($con,$_POST['age']).'");

Also, as @seblaze mentioned, table names are case-sensitive, so use persons instead of Persons

For more security, use prepared statements.

2 Comments

like this dear? $sql="INSERT INTO persons (FirstName, LastName, Age) VALUES (mysqli_real_escape_string('$_POST[firstname]'),mysqli_real_escape_string('$_POST[lastname]'),mysqli_real_escape_string('$_POST[age]'))";
dear thankyou so much :)) but i cant give reputation to two people at the same time :(
0
  1. A blank screen means most of the time that you are dealing with some error. You have to turn error reporting on for your local development. How do I enable error reporting in PHP?

  2. Check that your column names are written camelCase in your script but not in your database.

  3. In most cases it's handy to have an ID column which is your unique identifier.

  4. Good practice: Start using PDO

Comments

0

First,

You need to update the PHP configurations as:
memory_limit = 64M
Make sure you increase the memory .

Then, you need to enable Error reporting, using .htaccess file or configure it with php.ini. Read this for help

After that you can debug your work.

1 Comment

thanks dear for coming here and tried to help me :))
0

Try the code in this, http://www.tizag.com/mysqlTutorial/mysqlinsert.php

in w3schools it uses mysqli I also had some issues with it.

in the link it has some sample codes and it uses mysql

2 Comments

okay dear thanks for coming here, i will learning on that site :)
yea also look at the manual atwww.php.net/manual/en/ there you can get all the information about PHP functions with how to use it. Hope with it you will get a better idea.

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.