简体   繁体   中英

How to insert record using PHP MySQL?

I created database(my_db) and table(persons) in which i used three fields FirstName,LastName and Age. I run below php script it shows error like:

( ! ) Notice: Undefined index: FirstName in C:\\wamp\\www\\insert.php on line 5 Call Stack

Time Memory Function Location

1 0.0007 369800 {main}( ) ..\\insert.php:0

( ! ) Notice: Undefined index: LastName in C:\\wamp\\www\\insert.php on line 6 Call Stack

Time Memory Function Location

1 0.0007 369800 {main}( ) ..\\insert.php:0

( ! ) Notice: Undefined index: Age in C:\\wamp\\www\\insert.php on line 7 Call Stack

Time Memory Function Location

1 0.0007 369800 {main}( ) ..\\insert.php:0 Records added to the database

BELOW IS THE PHP CODE: PLS HELP ME AND THANKS IN ADVANCE

  <html>
 <body>

   <?php
  $first=$_POST ['FirstName']; 
  $last=$_POST ['LastName'];
  $a=$_POST ['Age'];

  $user_name = "root";
  $password = "";
  $database = "my_db";
  $server = "127.0.0.1";
  $db_handle = mysql_connect($server, $user_name, $password);

  $db_found = mysql_select_db($database, $db_handle);

  if ($db_found) {

  $SQL = "INSERT INTO persons(FirstName, LastName, Age) VALUES     ('$first', '$last', '$a')";

     $result = mysql_query($SQL);

     mysql_close($db_handle);

     print "Records added to the database";
    }
    else 
      {
        print "Database NOT Found ";
     mysql_close($db_handle);
     }

     ?>

    AND HTML FORM IS BELOW:

     <html>
       <body>
        <form action="insert.php" mehtod="POST">
          First Name: <input type="text" name="FirstName">
          <br />
          Last Name:<input type="text" name="LastName">
          <br />
          Age:<input type="text" name="Age">
          <br />
         <input type="submit" value="ADD" />
       </form>  
      </body>
     </html>

It is most likely your $_POST -variables are not set.

Try a...

var_dump($_POST);

...instead of...

$first=$_POST['FirstName']; 
$last=$_POST['LastName'];
$a=$_POST['Age'];

...to debug, if the values you're looking for are stored in there (get rid of the space between the $_POST and the identifier, too, please!).

Not your question, but if you're INSERT ing those variables without sanitizing them first, that opens any possibilities for SQL injections. Sanitize them properly to counter this risk.

You have used mehtod in

<Form mehtod="post">

instead of

<Form method="post">

Are you sure you are submitting your form with POST instead of GET??

Try to add dumps to see what you get

<?php
  var_dump($_POST);
  var_dump($_GET);

  //Anyway you can use $_REQUEST instead of $_POST or $_GET
  $first= $_REQUEST['FirstName']; 
  $last= $_REQUEST['LastName'];

Try this:

    if ($db_found) {

    $SQL = sprintf("
        INSERT INTO persons 
            (FirstName, LastName, Age) 
        VALUES ('%s', '%s', '%s')",
        mysql_real_escape_string($first), 
        mysql_real_escape_string($last), 
        mysql_real_escape_string($a));

    $result = mysql_query($SQL);
    if( $result == true ){
        print "Records added to the database";
    }

    mysql_close($db_handle);
}

The reason why it wasn't working is that you weren't calling your $result anywhere, you just declared it.

Also, I put your query into a sprintf() function which means that it is easier to see what data you are passing it.

Another thing, is that you want to check if the affected rows are > 0 as that will mean that something has been inserted.

The "errors" your seeing are actually notices. They occur because you are trying to access array indices that don't exist.

When you try to get the values out of $_POST, they do not exist, and the notice is generated.

It is possible to turn these notices off , but you shouldn't. Instead, you should check to make sure the input is valid ( AND SANITIZE IT FOR GOODNESS' SAKE !) using isset or empty .

The reason why the errors are being displayed is hard to guess, since you haven't included the HTML that is used to post the data. But most likely, you misnamed the variables or sent them via GET instead of POST.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM