2012-04-14 61 views
0

可能重复:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
Warning: mysql_fetch_* expects parameter 1 to be resource, boolean given errorTRIM()函数给予警告

我得到这个警告

Warning: trim() expects parameter 1 to be string, array given in .. 

这是我的装饰线。 完整的代码用于在字段为空时发送错误。然而,这个错误似乎是说每个字段都是空的,但只有'本地'字段是必需的,这是我的第二个问题。 感谢您的帮助!

session_start(); 
$err = array(); 


$user_id = intval($_SESSION['user_id']); 
// otherwise 
if (isset($_POST['doLanguage'])) { 
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection."); 

// check if current user is banned 
$the_query = sprintf("SELECT COUNT(*) FROM users WHERE `banned` = '0' AND `id` = 
'%d'", $user_id); 
$result = mysql_query($the_query, $link); 
$user_check = mysql_num_rows($result); 
// user is ok 
if ($user_check > 0) { 

    // check for empty fields 
    foreach ($_POST as $key => $val) { 
     $value = trim($val); 
     if (empty($value)) { 
      $err[] = "ERROR - $key is required"; 
     } 
    } 
    // no errors 
    if(empty($err)) { 
     for($i = 0; $i < count($_POST["other"]); $i++) 


      $native = mysql_real_escape_string($_POST['native'][$i]); 
      $other = mysql_real_escape_string($_POST['other'][$i]); 
      $other_list = mysql_real_escape_string($_POST['other_list'][$i]); 
      $other_read = mysql_real_escape_string($_POST['other_read'][$i]); 
      $other_spokint = mysql_real_escape_string($_POST['other_spokint'][$i]); 
      $other_spokprod = mysql_real_escape_string($_POST['other_spokprod'][$i]); 
      $other_writ = mysql_real_escape_string($_POST['other_writ'][$i]); 

     // insert into the database 
     $the_query = sprintf("INSERT INTO `language` 
(`user_id`,`native`,`other`,`other_list`,`other_read`, `other_spokint` 
,`other_spokprod`,`other_writ`) VALUES ('%d','%s','%s','%s','%s','%s','%s','%s')", 
    $user_id,$native,$other,$other_list,$other_read, 
$other_spokint,$other_spokprod,$other_writ); 

     // query is ok? 
     if (mysql_query($the_query, $link)){ 

      // redirect to user profile 
      header('Location: myaccount.php?id=' . $user_id); 
      } 
     } 
    } 
} 
+0

井用'的var_dump($ _ POST);'以查看哪些数据是类型的阵列。 – brezanac 2012-04-14 17:25:23

+0

结果是array [8] {[“native”] => array(1){[0] => string(0)“”} [“other”] => array(1){[0] => string(0)“”} [“other_list”] => array(1){[0] => string(0)“”} [“other_read”] => array(1){[0] => string( 0)“”} [“other_spokint”] => array(1){[0] => string(0)“”} [“other_spokprod”] => array(1){[0] => string(0) “”} [“other_writ”] => array(1){[0] => string(0)“”} [“doLanguage”] => string(4)“Save”}' – user1257518 2012-04-14 17:30:02

+0

a ha!谢谢,现在很清楚 – 2012-04-14 17:41:19

回答

1
// check for empty fields 
foreach ($_POST as $key => $val) { 
    while (is_array($val)) 
     $val = reset($val); 
    if (is_string($val)) 
     $val = trim($val); 
    if (empty($val)) { 
     $err[] = "ERROR - $key is required"; 
    } 
} 
+0

没有错误消息出现 – user1257518 2012-04-14 17:28:26

+0

添加一些调试语句,输出密钥和值,并确保它循环遍历所有发布的值。 – 2012-04-14 17:30:18

+0

我不知道该怎么办我还在学习 – user1257518 2012-04-14 17:38:17