0

I am trying to make a submit action insert data into the database with

if(isset($_POST['sendrqst'])) {
    $wpdb->insert("wp_refundrequests", [
        "product_name"  => $name,
        "product_qty"   => '5',
        "comment"       => "something",
        "customer_name" => "test",
        "refund_total"  => "200",
        "request_date"  => "2000" ,
    ]);
    $wpdb->print_error();
}

within a different file, but while $name works everywhere else properly even outside it's own "for" statement, it won't work inside that secondary file even when included to the main page, it only prints out the name of the file itself instead of what it should which is the name of a product selected from the wpdb. The name variable comes from this:

$test = $_POST['productinfo'];
$total2 = 0;
for($i=0; $i < sizeof($test); $i++) {
    list($name, $quantity, $total) = explode("|", $test[$i]);

A global variable should work inside an "if" but in this case for some reason it doesn't work even tho it works just fine outside the "if". Why is it that it does not work at all while inside this certain "if" statement

Veraen
  • 65
  • 2
  • 13
  • 1
    Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Jeff Sep 25 '18 at 10:05
  • You have a very specific if statement though. `if(isset($_POST['sendrqst'])) { //something }`. Make sure that `$_POST['sendrqst']` *is set* when testing, or you will never enter the if statement. – Martin Sep 25 '18 at 11:57
  • Also make sure that the variable is within the scope that you are working in. If the variable is declared in one file, but used in another file, make sure that the file that declared the variable is included into the file where you wish to use the variable. Also if the variable is handled within a function scope, it will only be accessible within that function's scope. – Martin Sep 25 '18 at 11:59
  • when working with global scopes and default values, it's a good practice to have one file containing those variables as *defines* and then include that on the relevant pages. That way you also only have one place to edit / add global default values. – Martin Sep 25 '18 at 12:00
  • It does post something but for some reason won't find the variable, but I got it fixed somewhat by making a session and inputting the variable into session data – Veraen Sep 25 '18 at 12:01
  • @Veraen you most likely didn't have access to the variable (see past comments), or had it overwritten with an empty string. – Martin Sep 25 '18 at 12:02
  • @Martin Which is rather strange considering on the very next line I could properly `print_r($name);` the variable, also tried printing it before and after the mentioned code in the post and still worked – Veraen Sep 25 '18 at 12:04

0 Answers0