简体   繁体   English

问题使mysqli_query执行

[英]Issue getting mysqli_query to execute

I have written the following function in PHP that has a mysqli_query in it that runs without any errors or exceptions. 我已经在PHP中编写了以下函数,该函数中具有mysqli_query ,该函数运行时没有任何错误或异常。 However, the INSERT INTO statement or $insert variable doesn't seem to be working as expected and I can't figure it out. 但是, INSERT INTO语句或$insert变量似乎未按预期工作,我无法弄清楚。 I realize that posting only a portion of the code might make it difficult to ascertain why it is not working, but I am really looking for confirmation that there are no errors in this function. 我意识到仅发布一部分代码可能会很难确定为什么它不起作用,但是我确实在寻找确认此函数没有错误的原因。

Do I need to utilize mysqli_real_escape_string for every url provided? 我需要为提供的每个网址使用mysqli_real_escape_string吗? I tried altering $website to $_website to account for this, but it returned nothing. 我尝试将$website更改$website $_website来解决此问题,但没有返回任何结果。

Just really trying to figure out if there's anything I'm doing wrong here that's prevent the SQL query to work. 只是想弄清楚我是否在做错什么,以防止SQL查询工作。 It returns no error which is making it hard to debug. 它没有返回错误,这使得调试变得困难。 Thanks in advance! 提前致谢!

$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);
  }
}

The problem I can see straight off is you are checking your sql variable instead of the query result. 我直接看到的问题是您正在检查sql变量而不是查询结果。

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

Try changing it to: 尝试将其更改为:

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

Also whats $jp? 还有$ jp是什么? it doesn't look like you have assigned it anything. 您似乎没有为其分配任何内容。 Make sure this is the variable that has your mysqli_connect on it. 确保这是带有mysqli_connect的变量。 With your question regarding mysqli_real_escape_string, you should really be utilizing mysqli prepared statements as well. 关于mysqli_real_escape_string的问题,您实际上也应该利用mysqli准备的语句。 All user input should be sanitized. 所有用户输入均应进行清理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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