获取mysqli\u查询以执行的问题

kpbwa7wx  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(319)

我用php编写了以下函数 mysqli_query 在没有任何错误或异常的情况下运行。然而 INSERT INTO 声明或 $insert 变量似乎没有按预期工作,我也弄不明白。我意识到仅仅发布代码的一部分可能会让我很难确定它为什么不工作,但是我真的希望确认这个函数中没有错误。
我需要利用 mysqli_real_escape_string 对于提供的每个url?我试着改变 $website$_website 但它什么也没回来。
只是想弄清楚是否有什么地方我做错了,妨碍了sql查询的工作。它不返回使调试变得困难的错误。提前谢谢!

$jp = mysqli_connect("localhost", "myuser", "password", "mydatabase");

if (!$jp) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

function create_distributor( $new_user_id ) {
  $errors = new WP_Error();
  $error=false;
  $errorMsg='';
  $logo=true;
  $name=addslashes(htmlentities($_REQUEST['name']));
  $contact=addslashes(htmlentities($_REQUEST['contact_info']));
  $user_info = get_userdata( $new_user_id );

  $website = $_POST['website'];
  if (stripos($website, "http://") !== 0)   //doesn't start with http:// ? , then add it
      $website = "http://" . $website; 
  // $_website = mysqli_real_escape_string($jp, $website); // THIS DOESNT RETURN ANYTHING
  $subdir = $user_info->user_nicename; // use nicename because user_login is obfuscated as unverified
  $distribpath = 'http://ghq.com/dhdq/'.$subdir;
  $ga_code = 'UA-15331916-1'; //default GA code
  $logo = 'http://ghq.com/wp-content/themes/CAG/img/ghlogo.jpg'; //default png logo

  if(!isset($_REQUEST['name']) || $_REQUEST['name']=='')
  {
    $error=true;
    $errors->add('Distributor Name is required', __('<strong>ERROR</strong>:Distrubutor\'s name was not provided.'));
  }
  if($error)
  {
    return($errorMsg);
  }

  $insert="INSERT INTO distributor (id, name, contact, logo, path, subdir, website, ga_code) VALUES ('".$new_user_id."','".$name."','".$contact."','".$logo."','".$distribpath."','".$subdir."','".$website."','".$ga_code."')";
//   var_dump($insert); 

        // The var_dump print out above is the following SQL Command which if copied and pasted 
    in phpmyadmin works fine: string(252) "INSERT INTO distributor (id, name, contact, 
logo, path, subdir, website, ga_code) VALUES ('1748','test24','','http://ghq.com/wp-content/themes/CAG/img/ghlogo.jpg',
                'http://ghq.com/dhdq/test24','test24','','UA-15331916-1')"

  mysqli_query($jp, $insert);
  if ( false===$insert ) {
  printf("error: %s\n", mysqli_error($jp));
}
  else {
    echo 'done.';
  }
  if($error)
  {
    return $errors;
  }
  else
  {
    return($id);
  }
}
hxzsmxv2

hxzsmxv21#

我可以直接看到的问题是,您检查的是sql变量,而不是查询结果。

mysqli_query($jp, $insert);
if ( false===$insert ) {
  printf("error: %s\n", mysqli_error($jp));
}
  else {
    echo 'done.';
}

尝试将其更改为:

$result = mysqli_query($jp, $insert);
if (!$result) {
  printf("error: %s\n", mysqli_error($jp));
}else {
    echo 'done.';
}

还有美元日元是多少?看起来你没有给它分配任何东西。确保这是有mysqli\u连接的变量。关于mysqli\u real\u escape\u字符串的问题,您也应该真正利用mysqli准备的语句。应清除所有用户输入。

相关问题