-1
This is my php code
       <?php
$host = "localhost";
$username = "root";
$password = "";
$db_name = "test1";
$tbl_name = "test_mysql";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$name = (isset($_POST['name'])) ? $_POST['name'] : '';
$lastname = (isset($_POST['lastname'])) ? $_POST['lastname'] : '';
$email = (isset($_POST['email'])) ? $_POST['email'] : '';
$sql = "INSERT INTO $tbl_name(name, lastname, email)VALUES('$name', '$lastname', '$email')";
$result = mysql_query($sql);

if ($result) {
    echo "Successful";
    echo "<BR>";
    echo "<a href='insert_ac.php'>Back to main page</a>";
} else {
    echo "ERROR";
}
mysql_close();`enter code here`

?> 

I am able to insert values to db,but after that if i click "back to main page" to insert more, i am getting the errors as shown below

Notice: Undefined variable: name in      C:\xampp\htdocs\PhpProject3(Insert)\insert_ac.php on line 25
Notice: Undefined variable: lastname in C:\xampp\htdocs\PhpProject3(Insert)\insert_ac.php on line 25
Notice: Undefined variable: email in C:\xampp\htdocs\PhpProject3(Insert)\insert_ac.php on line 25

Successfully Inserted
To insert more...Back to main page
How can I fix this???

MaryAnn
  • 393
  • 7
  • 17

4 Answers4

1

Before you put $_POST variables in a database you need to check and validate them thouroughly.

// start by making all posted varibales local, this is safe because of the prefix
extract($_POST,EXTR_PREFIX_ALL,'post');
// first check if all wanted variables exist
if (isset($post_name) &&
    isset($post_lastname) &&
    isset($post_email))
{
  // now make the variables safe for insertion
  $name     = mysql_real_escape_string($post_name);
  $lastname = mysql_real_escape_string($post_lastname);
  $email    = mysql_real_escape_string($post_email);
  // only then insert them in the database
  $sql      = "INSERT INTO $tbl_name (name,lastname,email) 
               VALUES ('$name','$lastname','$email')";
  $result   = mysql_query($sql);
  echo ($result ? 'Success' : 'Failure');
}

I left out a few bits, like making the db connection.

The mysql extension is deprecated, please do not use it.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
0

Well, maybe define it even if it didn't get a value? You should know what you want to do if there is no POST data.

if(isset($_POST['name'])){
  $name = $_POST['name'];
}else{
  $name='';
}
Viktor Koncsek
  • 564
  • 3
  • 13
0

You should put data into db only after data are send by post, so you must first check if form is send:

if (!empty($_POST)) { //HERE you check if form is send

if(isset($_POST['name'])){ $name = $_POST['name']; } 
if(isset($_POST['lastname'])){ $lastname = $_POST['lastname']; } 
if(isset($_POST['email'])){ $email = $_POST['email']; } 
$sql="INSERT INTO $tbl_name(name, lastname, email)VALUES('$name', '$lastname', '$email')";
$result=mysql_query($sql);
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert_ac.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
}
?>
nospor
  • 4,190
  • 1
  • 16
  • 25
  • 'This is only half solution because when form is not send your code will add empty values into db' So if I POST something but not name lastname email, it still inserts empty values. 'half answer' – Viktor Koncsek Jun 02 '16 at 08:56
  • It will happen only happen when author will have wrong form or he will be attacked. if you want, you can check all fields and if all exists then make insert. But for this moment I think it resolve author problem. – nospor Jun 02 '16 at 09:02
0

Use ternary operator or assign blank value through if else statement. You need to assign default value to variable.

Try this one :

<?php
$host = "localhost";
$username = "root";
$password = "";
$db_name = "test1";
$tbl_name = "test_mysql";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$name = (isset($_POST['name'])) ? $_POST['name'] : '';
$lastname = (isset($_POST['lastname'])) ? $_POST['lastname'] : '';
$email = (isset($_POST['email'])) ? $_POST['email'] : '';
$sql = "INSERT INTO $tbl_name(name, lastname, email)VALUES('$name', '$lastname', '$email')";
$result = mysql_query($sql);

if ($result) {
    echo "Successful";
    echo "<BR>";
    echo "<a href='insert_ac.php'>Back to main page</a>";
} else {
    echo "ERROR";
}
mysql_close();
R Yadav
  • 1
  • 3
  • Here, if i click on "back to main page" its not redirecting – MaryAnn Jun 02 '16 at 09:41
  • Now i am able to add data to db, but the problem is with redirection..which can be corrected by changing insert_ac.php,where i am redirecting to same page..Thanks for the help!! – MaryAnn Jun 02 '16 at 10:04