undefined variable in php while giving sql values











up vote
-3
down vote

favorite












while inserting values into table its showing error



the error is like this




Notice: Undefined variable: pname in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: price in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: tax in /opt/lampp/htdocs/zubair/index.php
on line 24



Notice: Undefined variable: image in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: manufacturer in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: address in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: city in /opt/lampp/htdocs/zubair/index.php
on line 24



Notice: Undefined variable: state in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: country in
/opt/lampp/htdocs/zubair/index.php on line 24 Duplicate entry '0' for
key 'PRIMARY







<?

if(isset($_POST) & !empty($_POST)){
$pname = mysql_real_escape_string($_POST['pname']);
$price = mysql_real_escape_string($_POST['price']);
$tax = mysql_real_escape_string($_POST['tax']);
$image = mysql_real_escape_string($_POST['image']);
$manufacturer = $_POST['manufacturer'];
$address = mysql_real_escape_string($_POST['address']);
$city = mysql_real_escape_string($_POST['city']);
$state = mysql_real_escape_string($_POST['state']);
$country = mysql_real_escape_string($_POST['country']);

}


$CreateSql = "INSERT INTO `products` (product_name, price, tax, image, manufacturer, address, city, state, country) VALUES ('$pname', '$price', '$tax', '$image', '$manufacturer', '$address', '$city', '$state', '$country')";

$connection = mysqli_connect("localhost","root","","product");
$res = mysqli_query($connection, $CreateSql) or die(mysqli_error($connection));

if($res){
$smsg = "Successfully inserted data, Insert New data.";
}else{
$fmsg = "Data not inserted, please try again later.";
}







?>












share|improve this question
























  • you are not passing any data in POST
    – guradio
    Nov 9 at 8:05






  • 1




    Possible duplicate of "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP
    – Tomasz
    Nov 9 at 8:06










  • @guradio can u show me as an answer
    – Zubair Nazer Oliyat
    Nov 9 at 8:07










  • You have to set an else condition when you have empty fields. P.s. your code is open to SQL Injection
    – Sfili_81
    Nov 9 at 8:11










  • You are using & operator. Should be if(isset($_POST) && !empty($_POST)) otherwise $_POST is empty
    – ivion
    Nov 9 at 8:11















up vote
-3
down vote

favorite












while inserting values into table its showing error



the error is like this




Notice: Undefined variable: pname in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: price in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: tax in /opt/lampp/htdocs/zubair/index.php
on line 24



Notice: Undefined variable: image in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: manufacturer in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: address in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: city in /opt/lampp/htdocs/zubair/index.php
on line 24



Notice: Undefined variable: state in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: country in
/opt/lampp/htdocs/zubair/index.php on line 24 Duplicate entry '0' for
key 'PRIMARY







<?

if(isset($_POST) & !empty($_POST)){
$pname = mysql_real_escape_string($_POST['pname']);
$price = mysql_real_escape_string($_POST['price']);
$tax = mysql_real_escape_string($_POST['tax']);
$image = mysql_real_escape_string($_POST['image']);
$manufacturer = $_POST['manufacturer'];
$address = mysql_real_escape_string($_POST['address']);
$city = mysql_real_escape_string($_POST['city']);
$state = mysql_real_escape_string($_POST['state']);
$country = mysql_real_escape_string($_POST['country']);

}


$CreateSql = "INSERT INTO `products` (product_name, price, tax, image, manufacturer, address, city, state, country) VALUES ('$pname', '$price', '$tax', '$image', '$manufacturer', '$address', '$city', '$state', '$country')";

$connection = mysqli_connect("localhost","root","","product");
$res = mysqli_query($connection, $CreateSql) or die(mysqli_error($connection));

if($res){
$smsg = "Successfully inserted data, Insert New data.";
}else{
$fmsg = "Data not inserted, please try again later.";
}







?>












share|improve this question
























  • you are not passing any data in POST
    – guradio
    Nov 9 at 8:05






  • 1




    Possible duplicate of "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP
    – Tomasz
    Nov 9 at 8:06










  • @guradio can u show me as an answer
    – Zubair Nazer Oliyat
    Nov 9 at 8:07










  • You have to set an else condition when you have empty fields. P.s. your code is open to SQL Injection
    – Sfili_81
    Nov 9 at 8:11










  • You are using & operator. Should be if(isset($_POST) && !empty($_POST)) otherwise $_POST is empty
    – ivion
    Nov 9 at 8:11













up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











while inserting values into table its showing error



the error is like this




Notice: Undefined variable: pname in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: price in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: tax in /opt/lampp/htdocs/zubair/index.php
on line 24



Notice: Undefined variable: image in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: manufacturer in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: address in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: city in /opt/lampp/htdocs/zubair/index.php
on line 24



Notice: Undefined variable: state in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: country in
/opt/lampp/htdocs/zubair/index.php on line 24 Duplicate entry '0' for
key 'PRIMARY







<?

if(isset($_POST) & !empty($_POST)){
$pname = mysql_real_escape_string($_POST['pname']);
$price = mysql_real_escape_string($_POST['price']);
$tax = mysql_real_escape_string($_POST['tax']);
$image = mysql_real_escape_string($_POST['image']);
$manufacturer = $_POST['manufacturer'];
$address = mysql_real_escape_string($_POST['address']);
$city = mysql_real_escape_string($_POST['city']);
$state = mysql_real_escape_string($_POST['state']);
$country = mysql_real_escape_string($_POST['country']);

}


$CreateSql = "INSERT INTO `products` (product_name, price, tax, image, manufacturer, address, city, state, country) VALUES ('$pname', '$price', '$tax', '$image', '$manufacturer', '$address', '$city', '$state', '$country')";

$connection = mysqli_connect("localhost","root","","product");
$res = mysqli_query($connection, $CreateSql) or die(mysqli_error($connection));

if($res){
$smsg = "Successfully inserted data, Insert New data.";
}else{
$fmsg = "Data not inserted, please try again later.";
}







?>












share|improve this question















while inserting values into table its showing error



the error is like this




Notice: Undefined variable: pname in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: price in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: tax in /opt/lampp/htdocs/zubair/index.php
on line 24



Notice: Undefined variable: image in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: manufacturer in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: address in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: city in /opt/lampp/htdocs/zubair/index.php
on line 24



Notice: Undefined variable: state in
/opt/lampp/htdocs/zubair/index.php on line 24



Notice: Undefined variable: country in
/opt/lampp/htdocs/zubair/index.php on line 24 Duplicate entry '0' for
key 'PRIMARY







<?

if(isset($_POST) & !empty($_POST)){
$pname = mysql_real_escape_string($_POST['pname']);
$price = mysql_real_escape_string($_POST['price']);
$tax = mysql_real_escape_string($_POST['tax']);
$image = mysql_real_escape_string($_POST['image']);
$manufacturer = $_POST['manufacturer'];
$address = mysql_real_escape_string($_POST['address']);
$city = mysql_real_escape_string($_POST['city']);
$state = mysql_real_escape_string($_POST['state']);
$country = mysql_real_escape_string($_POST['country']);

}


$CreateSql = "INSERT INTO `products` (product_name, price, tax, image, manufacturer, address, city, state, country) VALUES ('$pname', '$price', '$tax', '$image', '$manufacturer', '$address', '$city', '$state', '$country')";

$connection = mysqli_connect("localhost","root","","product");
$res = mysqli_query($connection, $CreateSql) or die(mysqli_error($connection));

if($res){
$smsg = "Successfully inserted data, Insert New data.";
}else{
$fmsg = "Data not inserted, please try again later.";
}







?>








<?

if(isset($_POST) & !empty($_POST)){
$pname = mysql_real_escape_string($_POST['pname']);
$price = mysql_real_escape_string($_POST['price']);
$tax = mysql_real_escape_string($_POST['tax']);
$image = mysql_real_escape_string($_POST['image']);
$manufacturer = $_POST['manufacturer'];
$address = mysql_real_escape_string($_POST['address']);
$city = mysql_real_escape_string($_POST['city']);
$state = mysql_real_escape_string($_POST['state']);
$country = mysql_real_escape_string($_POST['country']);

}


$CreateSql = "INSERT INTO `products` (product_name, price, tax, image, manufacturer, address, city, state, country) VALUES ('$pname', '$price', '$tax', '$image', '$manufacturer', '$address', '$city', '$state', '$country')";

$connection = mysqli_connect("localhost","root","","product");
$res = mysqli_query($connection, $CreateSql) or die(mysqli_error($connection));

if($res){
$smsg = "Successfully inserted data, Insert New data.";
}else{
$fmsg = "Data not inserted, please try again later.";
}







?>





<?

if(isset($_POST) & !empty($_POST)){
$pname = mysql_real_escape_string($_POST['pname']);
$price = mysql_real_escape_string($_POST['price']);
$tax = mysql_real_escape_string($_POST['tax']);
$image = mysql_real_escape_string($_POST['image']);
$manufacturer = $_POST['manufacturer'];
$address = mysql_real_escape_string($_POST['address']);
$city = mysql_real_escape_string($_POST['city']);
$state = mysql_real_escape_string($_POST['state']);
$country = mysql_real_escape_string($_POST['country']);

}


$CreateSql = "INSERT INTO `products` (product_name, price, tax, image, manufacturer, address, city, state, country) VALUES ('$pname', '$price', '$tax', '$image', '$manufacturer', '$address', '$city', '$state', '$country')";

$connection = mysqli_connect("localhost","root","","product");
$res = mysqli_query($connection, $CreateSql) or die(mysqli_error($connection));

if($res){
$smsg = "Successfully inserted data, Insert New data.";
}else{
$fmsg = "Data not inserted, please try again later.";
}







?>






php sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 8:26









Sfili_81

383314




383314










asked Nov 9 at 8:04









Zubair Nazer Oliyat

1




1












  • you are not passing any data in POST
    – guradio
    Nov 9 at 8:05






  • 1




    Possible duplicate of "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP
    – Tomasz
    Nov 9 at 8:06










  • @guradio can u show me as an answer
    – Zubair Nazer Oliyat
    Nov 9 at 8:07










  • You have to set an else condition when you have empty fields. P.s. your code is open to SQL Injection
    – Sfili_81
    Nov 9 at 8:11










  • You are using & operator. Should be if(isset($_POST) && !empty($_POST)) otherwise $_POST is empty
    – ivion
    Nov 9 at 8:11


















  • you are not passing any data in POST
    – guradio
    Nov 9 at 8:05






  • 1




    Possible duplicate of "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP
    – Tomasz
    Nov 9 at 8:06










  • @guradio can u show me as an answer
    – Zubair Nazer Oliyat
    Nov 9 at 8:07










  • You have to set an else condition when you have empty fields. P.s. your code is open to SQL Injection
    – Sfili_81
    Nov 9 at 8:11










  • You are using & operator. Should be if(isset($_POST) && !empty($_POST)) otherwise $_POST is empty
    – ivion
    Nov 9 at 8:11
















you are not passing any data in POST
– guradio
Nov 9 at 8:05




you are not passing any data in POST
– guradio
Nov 9 at 8:05




1




1




Possible duplicate of "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP
– Tomasz
Nov 9 at 8:06




Possible duplicate of "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP
– Tomasz
Nov 9 at 8:06












@guradio can u show me as an answer
– Zubair Nazer Oliyat
Nov 9 at 8:07




@guradio can u show me as an answer
– Zubair Nazer Oliyat
Nov 9 at 8:07












You have to set an else condition when you have empty fields. P.s. your code is open to SQL Injection
– Sfili_81
Nov 9 at 8:11




You have to set an else condition when you have empty fields. P.s. your code is open to SQL Injection
– Sfili_81
Nov 9 at 8:11












You are using & operator. Should be if(isset($_POST) && !empty($_POST)) otherwise $_POST is empty
– ivion
Nov 9 at 8:11




You are using & operator. Should be if(isset($_POST) && !empty($_POST)) otherwise $_POST is empty
– ivion
Nov 9 at 8:11












2 Answers
2






active

oldest

votes

















up vote
0
down vote













According to your script variables defined if is set and not empty the $_POST variable. if you not pass your data to the script via POST method that section will not run. Then you get all the variables as undefined.
Probably your form method should change to POST. check your html form method if you using html form to test this.






share|improve this answer




























    up vote
    0
    down vote













    You are using the & operator. Use &&




    <?

    if(isset($_POST) && !empty($_POST)){ //<-- use the && operator
    $pname = mysql_real_escape_string($_POST['pname']);
    $price = mysql_real_escape_string($_POST['price']);
    $tax = mysql_real_escape_string($_POST['tax']);
    $image = mysql_real_escape_string($_POST['image']);
    $manufacturer = $_POST['manufacturer'];
    $address = mysql_real_escape_string($_POST['address']);
    $city = mysql_real_escape_string($_POST['city']);
    $state = mysql_real_escape_string($_POST['state']);
    $country = mysql_real_escape_string($_POST['country']);

    $CreateSql = "INSERT INTO `products` (product_name, price, tax, image, manufacturer, address, city, state, country) VALUES ('$pname', '$price', '$tax', '$image', '$manufacturer', '$address', '$city', '$state', '$country')";

    $connection = mysqli_connect("localhost","root","","product");
    $res = mysqli_query($connection, $CreateSql) or die(mysqli_error($connection));

    if($res){
    $smsg = "Successfully inserted data, Insert New data.";
    }else{
    $fmsg = "Data not inserted, please try again later.";
    }

    }

    ?>





    otherwise your $_POST variable is empty and be aware of mysql injection






    share|improve this answer























    • its not working
      – Zubair Nazer Oliyat
      Nov 9 at 8:16










    • have you check if you receive the value ?
      – Sfili_81
      Nov 9 at 8:17










    • What is the error?
      – ivion
      Nov 9 at 8:17











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221886%2fundefined-variable-in-php-while-giving-sql-values%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    According to your script variables defined if is set and not empty the $_POST variable. if you not pass your data to the script via POST method that section will not run. Then you get all the variables as undefined.
    Probably your form method should change to POST. check your html form method if you using html form to test this.






    share|improve this answer

























      up vote
      0
      down vote













      According to your script variables defined if is set and not empty the $_POST variable. if you not pass your data to the script via POST method that section will not run. Then you get all the variables as undefined.
      Probably your form method should change to POST. check your html form method if you using html form to test this.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        According to your script variables defined if is set and not empty the $_POST variable. if you not pass your data to the script via POST method that section will not run. Then you get all the variables as undefined.
        Probably your form method should change to POST. check your html form method if you using html form to test this.






        share|improve this answer












        According to your script variables defined if is set and not empty the $_POST variable. if you not pass your data to the script via POST method that section will not run. Then you get all the variables as undefined.
        Probably your form method should change to POST. check your html form method if you using html form to test this.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 8:13









        Thushara Saman

        1316




        1316
























            up vote
            0
            down vote













            You are using the & operator. Use &&




            <?

            if(isset($_POST) && !empty($_POST)){ //<-- use the && operator
            $pname = mysql_real_escape_string($_POST['pname']);
            $price = mysql_real_escape_string($_POST['price']);
            $tax = mysql_real_escape_string($_POST['tax']);
            $image = mysql_real_escape_string($_POST['image']);
            $manufacturer = $_POST['manufacturer'];
            $address = mysql_real_escape_string($_POST['address']);
            $city = mysql_real_escape_string($_POST['city']);
            $state = mysql_real_escape_string($_POST['state']);
            $country = mysql_real_escape_string($_POST['country']);

            $CreateSql = "INSERT INTO `products` (product_name, price, tax, image, manufacturer, address, city, state, country) VALUES ('$pname', '$price', '$tax', '$image', '$manufacturer', '$address', '$city', '$state', '$country')";

            $connection = mysqli_connect("localhost","root","","product");
            $res = mysqli_query($connection, $CreateSql) or die(mysqli_error($connection));

            if($res){
            $smsg = "Successfully inserted data, Insert New data.";
            }else{
            $fmsg = "Data not inserted, please try again later.";
            }

            }

            ?>





            otherwise your $_POST variable is empty and be aware of mysql injection






            share|improve this answer























            • its not working
              – Zubair Nazer Oliyat
              Nov 9 at 8:16










            • have you check if you receive the value ?
              – Sfili_81
              Nov 9 at 8:17










            • What is the error?
              – ivion
              Nov 9 at 8:17















            up vote
            0
            down vote













            You are using the & operator. Use &&




            <?

            if(isset($_POST) && !empty($_POST)){ //<-- use the && operator
            $pname = mysql_real_escape_string($_POST['pname']);
            $price = mysql_real_escape_string($_POST['price']);
            $tax = mysql_real_escape_string($_POST['tax']);
            $image = mysql_real_escape_string($_POST['image']);
            $manufacturer = $_POST['manufacturer'];
            $address = mysql_real_escape_string($_POST['address']);
            $city = mysql_real_escape_string($_POST['city']);
            $state = mysql_real_escape_string($_POST['state']);
            $country = mysql_real_escape_string($_POST['country']);

            $CreateSql = "INSERT INTO `products` (product_name, price, tax, image, manufacturer, address, city, state, country) VALUES ('$pname', '$price', '$tax', '$image', '$manufacturer', '$address', '$city', '$state', '$country')";

            $connection = mysqli_connect("localhost","root","","product");
            $res = mysqli_query($connection, $CreateSql) or die(mysqli_error($connection));

            if($res){
            $smsg = "Successfully inserted data, Insert New data.";
            }else{
            $fmsg = "Data not inserted, please try again later.";
            }

            }

            ?>





            otherwise your $_POST variable is empty and be aware of mysql injection






            share|improve this answer























            • its not working
              – Zubair Nazer Oliyat
              Nov 9 at 8:16










            • have you check if you receive the value ?
              – Sfili_81
              Nov 9 at 8:17










            • What is the error?
              – ivion
              Nov 9 at 8:17













            up vote
            0
            down vote










            up vote
            0
            down vote









            You are using the & operator. Use &&




            <?

            if(isset($_POST) && !empty($_POST)){ //<-- use the && operator
            $pname = mysql_real_escape_string($_POST['pname']);
            $price = mysql_real_escape_string($_POST['price']);
            $tax = mysql_real_escape_string($_POST['tax']);
            $image = mysql_real_escape_string($_POST['image']);
            $manufacturer = $_POST['manufacturer'];
            $address = mysql_real_escape_string($_POST['address']);
            $city = mysql_real_escape_string($_POST['city']);
            $state = mysql_real_escape_string($_POST['state']);
            $country = mysql_real_escape_string($_POST['country']);

            $CreateSql = "INSERT INTO `products` (product_name, price, tax, image, manufacturer, address, city, state, country) VALUES ('$pname', '$price', '$tax', '$image', '$manufacturer', '$address', '$city', '$state', '$country')";

            $connection = mysqli_connect("localhost","root","","product");
            $res = mysqli_query($connection, $CreateSql) or die(mysqli_error($connection));

            if($res){
            $smsg = "Successfully inserted data, Insert New data.";
            }else{
            $fmsg = "Data not inserted, please try again later.";
            }

            }

            ?>





            otherwise your $_POST variable is empty and be aware of mysql injection






            share|improve this answer














            You are using the & operator. Use &&




            <?

            if(isset($_POST) && !empty($_POST)){ //<-- use the && operator
            $pname = mysql_real_escape_string($_POST['pname']);
            $price = mysql_real_escape_string($_POST['price']);
            $tax = mysql_real_escape_string($_POST['tax']);
            $image = mysql_real_escape_string($_POST['image']);
            $manufacturer = $_POST['manufacturer'];
            $address = mysql_real_escape_string($_POST['address']);
            $city = mysql_real_escape_string($_POST['city']);
            $state = mysql_real_escape_string($_POST['state']);
            $country = mysql_real_escape_string($_POST['country']);

            $CreateSql = "INSERT INTO `products` (product_name, price, tax, image, manufacturer, address, city, state, country) VALUES ('$pname', '$price', '$tax', '$image', '$manufacturer', '$address', '$city', '$state', '$country')";

            $connection = mysqli_connect("localhost","root","","product");
            $res = mysqli_query($connection, $CreateSql) or die(mysqli_error($connection));

            if($res){
            $smsg = "Successfully inserted data, Insert New data.";
            }else{
            $fmsg = "Data not inserted, please try again later.";
            }

            }

            ?>





            otherwise your $_POST variable is empty and be aware of mysql injection






            <?

            if(isset($_POST) && !empty($_POST)){ //<-- use the && operator
            $pname = mysql_real_escape_string($_POST['pname']);
            $price = mysql_real_escape_string($_POST['price']);
            $tax = mysql_real_escape_string($_POST['tax']);
            $image = mysql_real_escape_string($_POST['image']);
            $manufacturer = $_POST['manufacturer'];
            $address = mysql_real_escape_string($_POST['address']);
            $city = mysql_real_escape_string($_POST['city']);
            $state = mysql_real_escape_string($_POST['state']);
            $country = mysql_real_escape_string($_POST['country']);

            $CreateSql = "INSERT INTO `products` (product_name, price, tax, image, manufacturer, address, city, state, country) VALUES ('$pname', '$price', '$tax', '$image', '$manufacturer', '$address', '$city', '$state', '$country')";

            $connection = mysqli_connect("localhost","root","","product");
            $res = mysqli_query($connection, $CreateSql) or die(mysqli_error($connection));

            if($res){
            $smsg = "Successfully inserted data, Insert New data.";
            }else{
            $fmsg = "Data not inserted, please try again later.";
            }

            }

            ?>





            <?

            if(isset($_POST) && !empty($_POST)){ //<-- use the && operator
            $pname = mysql_real_escape_string($_POST['pname']);
            $price = mysql_real_escape_string($_POST['price']);
            $tax = mysql_real_escape_string($_POST['tax']);
            $image = mysql_real_escape_string($_POST['image']);
            $manufacturer = $_POST['manufacturer'];
            $address = mysql_real_escape_string($_POST['address']);
            $city = mysql_real_escape_string($_POST['city']);
            $state = mysql_real_escape_string($_POST['state']);
            $country = mysql_real_escape_string($_POST['country']);

            $CreateSql = "INSERT INTO `products` (product_name, price, tax, image, manufacturer, address, city, state, country) VALUES ('$pname', '$price', '$tax', '$image', '$manufacturer', '$address', '$city', '$state', '$country')";

            $connection = mysqli_connect("localhost","root","","product");
            $res = mysqli_query($connection, $CreateSql) or die(mysqli_error($connection));

            if($res){
            $smsg = "Successfully inserted data, Insert New data.";
            }else{
            $fmsg = "Data not inserted, please try again later.";
            }

            }

            ?>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 9 at 8:17

























            answered Nov 9 at 8:13









            ivion

            17325




            17325












            • its not working
              – Zubair Nazer Oliyat
              Nov 9 at 8:16










            • have you check if you receive the value ?
              – Sfili_81
              Nov 9 at 8:17










            • What is the error?
              – ivion
              Nov 9 at 8:17


















            • its not working
              – Zubair Nazer Oliyat
              Nov 9 at 8:16










            • have you check if you receive the value ?
              – Sfili_81
              Nov 9 at 8:17










            • What is the error?
              – ivion
              Nov 9 at 8:17
















            its not working
            – Zubair Nazer Oliyat
            Nov 9 at 8:16




            its not working
            – Zubair Nazer Oliyat
            Nov 9 at 8:16












            have you check if you receive the value ?
            – Sfili_81
            Nov 9 at 8:17




            have you check if you receive the value ?
            – Sfili_81
            Nov 9 at 8:17












            What is the error?
            – ivion
            Nov 9 at 8:17




            What is the error?
            – ivion
            Nov 9 at 8:17


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221886%2fundefined-variable-in-php-while-giving-sql-values%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            JBPM : POST request for execute process go wrong