使用php将逗号分隔值插入mysql字段

时间:2013-10-17 01:18:00

标签: php mysql arrays

我无法将某些行业类别ID上传到我的mysql表中的单个字段中。我正在使用php / html。

我的表单正在插入我需要的每个值,除了我想插入的NewsIndustryID作为逗号分隔值 - 例如:201,206,209

这是我提交表单时收到的错误消息(如果我将NewsIndustryID字段删除,它可以正常工作):

Warning: mysql_real_escape_string() expects parameter 1 to be string,
array given in /home/coverfor/public_html/dwzUpload/dwzDataBase.php on
 line 426 Error on update record

 Column 'NewsIndustryID' cannot be null

array ( 'table_name' => 'Images', 'fields' => array ( 0 => array (
 'name' => 'Caption', 'value' => 'Test Image Caption', 'type' =>
 'text', 'def_value' => '', 'not_def_value' => '', ), 1 => array (
 'name' => 'NewsIndustryID', 'value' => array ( 0 => '201', 1 => '204',
 2 => '205', ), 'type' => 'text', 'def_value' => '', 'not_def_value' =>
 '', ), 2 => array ( 'name' => 'slideURL', 'value' =>
 '/newsimages/jackpot_15.jpg', 'type' => 'text', ), 3 => array ( 'name'
 => 'slideHeight', 'value' => 111290, 'type' => 'int', ), 4 => array ( 'name' => sliverURL', 'value' => '/newsimages/jackpot_15_sliver.jpg',
 'type' => 'text', ), 5 => array ( 'name' => 'BannerURL', 'value' =>
 '/newsimages/jackpot_15_banner.jpg', 'type' => 'text', ), ), )

 INSERT INTO `Images` ( `Caption`, `NewsIndustryID`, `slideURL`,
 `slideHeight`, `sliverURL`, `BannerURL` ) VALUES ( 'Test Image
 Caption', NULL, '/newsimages/jackpot_15.jpg', 111290,
 '/newsimages/jackpot_15_sliver.jpg',
 '/newsimages/jackpot_15_banner.jpg' )

如果有人能提供任何非常棒的帮助!

这是我页面上的代码(对于我在HTML声明之前包含所有内容的代码量道歉,因为我不确定问题出在哪里):

<?php

require_once("../../dwzUpload/dwzUpload.php");
require_once('../../Connections/newsDBconnection.php');

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
?>
<?php

if (!isset($_SESSION)) {
  session_start();
}


$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){

  $_SESSION['MM_Username'] = NULL;
  $_SESSION['MM_UserGroup'] = NULL;
  $_SESSION['PrevUrl'] = NULL;
  unset($_SESSION['MM_Username']);
  unset($_SESSION['MM_UserGroup']);
  unset($_SESSION['PrevUrl']);

  $logoutGoTo = "../login.php";
  if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
  }
}
?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 

  $isValid = False; 


  if (!empty($UserName)) { 

    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 

    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "../login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) 
  $MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php

set_time_limit(5400);
$upload = new dwzUpload();
$upload->SetProgressBar("");
$upload->SetTempFolder("");
$upload->SetTotalFileSize("");
$upload->SetRedirectUrl("imagesgallery.php");
$upload->SetFiles("/newsimages;1;;png, jpg, jpeg, gif, tiff, tif;2000;1;slideURL;1;slideHeight&N;2;;0;;;;sliverURL;1;BannerURL;1;photo@_@_@0@_@_@php@_@_@-1;710;322;100;-1;230;80;100;-1;_sliver;-1;1;-1;460;230;100;-1;_banner;1@_@_@../../@_@_@POST");
$upload->SetFormName("form1");
$upload->SetUploadType("insert");
$upload->SetValueToRedirectSend("");
$upload->SetDbParam($hostname_newsDBconnection, $database_newsDBconnection, $username_newsDBconnection, $password_newsDBconnection);
$upload->SetEditTable("Images");
$upload->SetFields("Caption|value|NewsIndustryID|value");
$upload->SetColumns("Caption|text|NewsIndustryID|text");
$upload->Execute();

if (isset($_POST['NewsIndustryID'])) { 
$_POST['NewsIndustryID'] = implode(',', $_POST['NewsIndustryID']); 
} 
?>

这是我表单的代码:

<form onsubmit="return ProgressBar()" action="<?php echo $upload->GetEditAction(); ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <h1>
    Upload Image to Gallery
    <?php
$upload->GetProgressBarField();
?>
  </h1>
  <p>&nbsp; </p>
  <p>
    <label for="Caption">Caption</label>
    <input type="text" name="Caption" id="Caption" />
</p>
  <p>
    <label for="NewsIndustryID">Related Industries</label>
    <select name="NewsIndustryID[]" size="11" multiple="multiple" id="NewsIndustryID">
      <option value="200" selected="selected">Not Specified</option>
      <option value="201">Construction</option>
      <option value="202">Energy</option>
      <option value="203">Forestry</option>
      <option value="204">Not For Profit</option>
      <option value="205">Professionals</option>
      <option value="206">Healthcare</option>
      <option value="207">Manufacturing</option>
      <option value="208">Mining</option>
      <option value="209">Property</option>
      <option value="210">Transport</option>
      <option value="211">Agriculture</option>
    </select>
  </p>

  <p>
    Photo: <input name="photo" type="file" onchange="dwz_setEvent(this,'change')" onclick="dwz_setEvent(this,'click')"><br> 
</p>
  <p>Submit
    <input type="submit" name="dwzSubmit" id="dwzSubmit" value="Submit" />
  </p>
  <input type="hidden" name="dwzUpload" id="dwzUpload" value="form1" />
   <input type="hidden" name="MM_insert" value="form1" />
</form>

2 个答案:

答案 0 :(得分:5)

新闻行业是一个阵列,如果你真的想要一个逗号分隔的字符串,我还是说不要,你可以使用implode()

$NewsIndustryID =implode(',',$array['NewsIndustryID']); 

不确定此处调用的数组是什么

价:http://php.net/manual/en/function.implode.php

答案 1 :(得分:0)

试试这个:

Use implode() function of PHP. It will convert array into string with separator defined in the function.

$NewsIndustryID_str =implode(',',$array['NewsIndustryID']); 

Also you can use any separator other than , like spacer or | or any.

-

由于