dongyo1818 2013-10-17 01:18
浏览 55
已采纳

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

I am having trouble uploading some industry category id's into a single field in my mysql table. I am using php/html.

My form is inserting every value I require except the NewsIndustryID which I would like inserted as comma separated values - for example: 201,206,209

This is the error message I am getting when I submit my form (it works fine if I take the NewsIndustryID field out):

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' )

If anyone could offer any assistance that would be fantastic!

This is the code on my page (apologies for the amount of code I included everything before the HTML declaration as I am not sure where the problem is):

<?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']); 
} 
?>

This is the code for my form:

<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条回答 默认 最新

  • duanjiao2978 2013-10-17 02:19
    关注

    NewsIndustryID is an array, if you really want a comma seperated string, and i still say don't, you can use implode()

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

    Not sure what your array is called here

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!