Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When Arrays go Bad!!

Status
Not open for further replies.

boab

Programmer
May 30, 2001
75
0
0
GB
Folks any help is appreciated,

What I have is a script which processes the results of a Flash movie and passes them into a database

in order to encode the variables that are coming out of the POST I have setup two arrays one for each table

Code:
$userFields_array=array("PayRef","Fname","Lname","Est");
$responseFields_array=array("PayRef","Code","ELquest1","ELquest2","ELquest3","ELquest4","ELquest5","ELquest6","ELquest7","ELquest8","ELquest9","ELquest10","ELquestComm","PstLO1","PstLO2","PstLO3","PstLO4","PstLO5","PstLO6","PstLO7","PstLO8","PstLOComm","TI1","TI2","TI3","TI4","TI5","TI6","TI7","TI8","TIComm","TF1","TF1Comm","TF2","TF2Comm");

for the two tables then I use these arrays to build the strings for the sql statements

for Emps table
Code:
//set up sql fragments
	$sqluserA="INSERT INTO Emps (";
	$sqluserB="";
	$sqluserC=") VALUES (";
	$sqluserD="";
	$sqluserE=");";
// get vars out of post data, compile list of field names
	foreach ($UserFields_array as $key => $value) {
		$sqluserB=$sqluserB.$value.",";
   	 	$sqluserD=$sqluserD."'".htmlentities($_POST[$value], ENT_QUOTES)."',";	
	}
// tidy ends of loop compiled strings
	$sqluserB=substr($sqluserB,0,-1);
	$sqluserD=substr($sqluserD,0,-1);
// concatenate sql string
	$sqluser=$sqluserA.$sqluserB.$sqluserC.$sqluserD.$sqluserE;
	$rs=odbc_exec($con,$sqluser);
	echo "sqluser = ".$sqluser."<br/>";
	if($rs){
		$retVal = 1;	
	}else{
		$retVal=0;
	}
	return $retVal;
}

For ResponsePst Table
Code:
//set up sql fragments
echo "insertR called";
	$sqlRespA="INSERT INTO ResponsePst (R_id,";
	$sqlRespB="";
	$sqlRespC=",dateString) VALUES ('".$_POST[payRef]."_".$_POST[Code];
	$sqlRespD="";
	$sqlRespE=", '".date("F j - Y - g:i a")."');";
	
// get vars out of post data, compile list of field names
	foreach($ResponseFields_array as $key => $value) {
		$sqlRespB=$sqlRespB.$value.",";
   		$sqlRespD=$sqlRespD."'".htmlentities($_POST[$value], ENT_QUOTES)."',";	
	}
// tidy ends of loop compiled strings
	$sqlRespB=substr($sqlRespB,0,-1);
	$sqlRespD=substr($sqlRespD,0,-1);
// concatenate sql string
	$sqlResp=$sqlRespA.$sqlRespB.$sqlRespC.$sqlRespD.$sqlRespE;
	$rs=odbc_exec($con,$sqlResp);
	echo "SQLResp =".$sqlResp."<br/>"; 
	if($rs){
		$retVal = 1;	
	}else{
		$retVal=0;
	}
	return $retVal;
}

after having tried most things I know the error message I am getting are:

"Invalid argument supplied for foreach() " and

"odbc_exec(): supplied argument is not a valid ODBC-Link resource in"

Now I'm guessing the the first error causes the second error.

The sql I am getting out of the script is:

sqluser = INSERT INTO Emps () VALUES (); for Emps and

SQLResp =INSERT INTO ResponsePst (R_id,,dateString) VALUES ('_, 'December 1 - 2004 - 11:27 am'); for ResponsePst Table


The question is how do I fix the first error? Would need to use 1 large array to extract info from the POST data then split it up somehow.

The Start of wisdom is to realise you know nothing. I'll be a genius then!
 
I think you have to simplify your code.

In the following code, I've assumed you have named your fields in your form the same as the fields in your database:
Code:
$vtmp = array();  //initialize a temp array to hold value names
$dtmp = array(); //initialize a temp array to hold data
$postkeys = array_keys($_POST);
for ($i=0;$i<count($postkeys);$i++) // loop thru posted info
    switch ($postkeys[$i]) {
          case 'PayRef':
          case 'Fname':
          case 'Lname':
          case 'Est':  //these are the only posted values you care about
               $vtmp[] = $postkeys[$i];
               $dtmp[] = "'" . htmlentities($_POST[$postkeys[$i]], ENT_QUOTES) . "'";
               break;
    }
$sql_query = 'INSERT INTO Emps (';
$sql_query .= implode(',',$vtmp);
$sql_query .= ') VALUES (';
$sql_query .= implode(',',$dtmp).')';

The implode will get the commas between the array values and not at the end, so you don't have to worry about getting rid of the extra comma.

You should be able to extrapolate this method for your other code.

I have been using similar code for my database adds and updates. For updates, you only add a value to the temp arrays when the value coming in from the form is different than the one already in the database.

This code can probably be simplified more.

Ken
 
ok have tried that below is the Code I am using
Code:
// checks each field to determine if the field is blank also builds body of email

###This first line of Code is to load your database variables.
include ('Includepost.inc');
//set arrays for users table
$Uvtmp = array();
$Udtmp = array();
//set arrays for reponse table
$Rvtmp = array();
$Rdtmp = array();

$postKeys=array_keys($_POST);
for($i=0;$i<count($postKeys);$i++){
		switch ($postKeys[$i]){
			case 'PayRef':
				$Rvtmp[]=$postKeys[$i];
				$Rdtmp[]="'".htmlentities($_POST[$postKeys[$i]],ENT_QUOTES)."'";	
				
				$Uvtmp[]=$postKeys[$i];
				$Udtmp[]="'".htmlentities($_POST[$postKeys[$i]],ENT_QUOTES)."'";
				
				break;
				
			
			case 'Fname':
			case 'Lname':
			case 'est':
				$Uvtmp[]=$postKeys[$i];
				$Udtmp[]="'".htmlentities($_POST[$postKeys[$i]],ENT_QUOTES)."'";
				break;
			case 'ELquest1':
			case 'ELquest2':
			case 'ELquest3':
			case 'ELquest4':
			case 'ELquest5':
			case 'ELquest6':
			case 'ELquest7':
			case 'ELquest8':
			case 'ELquest9':
			case 'ELquest10':
			case 'ELquestComm':
			case 'PstLO1':
			case 'PstLO2':
			case 'PstLO3':
			case 'PstLO4':
			case 'PstLO5':
			case 'PstLO6':
			case 'PstLO7':
			case 'PstLO8':
			case 'PstLOComm':
			case 'TI1':
			case 'TI2':
			case 'TI3':
			case 'TI4':
			case 'TI5':
			case 'TI6':
			case 'TI7':
			case 'TI8':
			case 'TIComm':
			case 'TF1':
			case 'TF1Comm':
			case 'TF2':
			case 'TF2Comm':
			case 'code':

				$Rvtmp[]=$postKeys[$i];
				$Rdtmp[]="'".htmlentities($_POST[$postKeys[$i]],ENT_QUOTES)."'";	
				
				break;
		}
}

$SqlUchk="SELECT* FROM Emps WHERE payref='".$_POST[payref]."'";
$sqlCchk="SELECT* FROM ResponsePst WHERE code='".$_POST[code]."'";

$con = odbc_connect($Dsn,$DBuser,$DBpass);
	
if($con){
	$rsUchk=odbc_exec($con,$SqlUchk);
	$numU=odbc_num_rows($rsUchk);
	if ($numU>0){
		$rsCchk=odbc_exec($con,$sqlCchk);
		$numR=odbc_num_rows($rsCchk);
		if ($rsCchk>0){
			echo "Use already in db <br/>";
			echo "User Already has a Post Course Response for this course";
			echo "&result=0";
		}else{
			echo "User Already in DB";
			$val1=insertR();
			echo "&result=".$val1;
		}
	}else{
		$val1=insertU();
		$val2=insertR();
		if ($val1=$val2 && $val1==1){
			echo "&result=".$val1;
		}else{
			echo "&result=0";
		}
	}
}else{
	echo"The database is not valid";
	echo"&result=0";	
}

function insertR(){
	$sqlResp='INSERT INTO ResponsePst (';
	$sqlResp=$sqlResp.implode(',',$Rvtmp);
	$sqlResp=$sqlResp.') VALUES (';
	$sqlResp=$sqlResp.implode(',',$Rdtmp).')';
	$rs=odbc_exec($con,$sqlResp);
	
	if($rs){
		$chksql="Select * FROM ResponsePst WHERE code='".$_POST[code]."' AND PayRef = '".$_POST[PayRef]."'";
		$chkRS = odbc_exec($con,$chksql);
		$num=odbc_num_rows($chkRS);
		if($num>0){
			$res=1;
		}else{
			$res=0;
		}
	}else{
		$res=0;
	}
	echo $sqlResp."</br>";
	return $res;
	
}

function insertU(){
	$sqluser='INSERT INTO Emps (';
	$sqluser=$sqluser.implode(',',$Uvtmp);
	$sqluser=$sqluser.' VALUES (';
	$sqluser=$sqluser.implode(',',$Udtmp).')';
	$rs=odbc_exec($con,$sqluser);
	
	if(rs){$chksql="Select * FROM Emps WHERE code='".$_POST[PayRef]."'";
		$chkRS = odbc_exec($con,$chksql);
		$num=odbc_num_rows($chkRS);
		if($num>0){
			$res=1;
		}else{
			$res=0;
		}
	}else{
		$res=0;
	}
	echo $sqlUser."</br>";
	return $res;
}

I am still geting warnings

The Start of wisdom is to realise you know nothing. I'll be a genius then!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top