0

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>
6
  • 1
    You've shown us everything except the actual query part in your code. Commented Oct 17, 2013 at 1:20
  • comma separated values into a single mysql field is almost always a bad idea Commented Oct 17, 2013 at 1:21
  • And the error seems to be self-explanatory! You are trying to put the values in a $_POST variable? Commented Oct 17, 2013 at 1:22
  • It seems self explanatory that NewsIndustryID cannot be null however I can see that it's understanding my input of ( 'name' => 'NewsIndustryID', 'value' => array ( 0 => '201', 1 => '204', 2 => '205', ) so why doesn't it want to put it in? Commented Oct 17, 2013 at 1:24
  • Ok what should I be putting them in - pardon my ignorance I am a front end designer being pushed to provide more functionality than I know how too. Commented Oct 17, 2013 at 1:26

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

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.

-

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.