php - I need some help to find an error -


i created file useful php functions operate sql databases have problem code inserts record in sql table. when try execute code appear this:

fatal error: uncaught error: unsupported operand types in c:\xampp\htdocs\iterations_php_mysql.php:118 stack trace: #0 c:\xampp\htdocs\prova.php(9): setrecord(object(mysqli), 'studenti', array, array) #1 {main} thrown in c:\xampp\htdocs\iterations_php_mysql.php on line 118

this function ($link link of host $link = mysqli_connect($host, $username);, $table table name, $tablefields array contain names of fields, $recordfields array contains fields of new record):

function setrecord($link, $table, $tablefields, $recordfields){      $sql = "insert ".$table." (";      if(count($tablefields) == count($recordfields)) $n = $tablefields;     else    return false;      for($i=0; $i<$n; $i++){          $sql = $sql . $tablefields[$i];         if($i != $n-1){   $sql = $sql . ", ";}   //this line 118     }      $sql = $sql.") values (";      for($i=0; $i<$n; $i++){          $sql = $sql.$recordfields[$i];         if($i != $n-1)   $sql = $sql . ", ";     }      $sql = $sql.")";      return mysqli_query($link, $sql); } 

$n = $tablefields 

so guess $tablefields array?

if so, makes no sense following array:

if ($i != $n-1) ... 

because can't arithmetic on array that. work better this:

if ($i != count($n)-2) ... 

note you'd have subtract 2, because arrays start @ index 0.

but have better suggestion simplify code:

instead of this:

for($i=0; $i<$n; $i++){     $sql = $sql . $tablefields[$i];     if($i != $n-1){   $sql = $sql . ", ";}   //this line 118 } 

write this:

$sql .= implode(", ", $tablefields); 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -