Link to home
Start Free TrialLog in
Avatar of peter_coop
peter_coopFlag for United Kingdom of Great Britain and Northern Ireland

asked on

mysql error Column count

hello
i am getting this error when i run this query:

mysql error Column count doesn't match value count at row 1

i have tried to find error but cannot seem to correct it. could someone point out my mistake? many thanks
function runSQL($rsql) {
	$hostname = "localhost";
	$username = "root";
	$password = "";
	$dbname   = "sample";
	$connect = mysql_connect($hostname,$username,$password) or die ("Error: could not connect to database");
	$db = mysql_select_db($dbname);
	$result = mysql_query($rsql) or die ('mysql error ' . mysql_error() . ' in query ' . $rsql);
	return $result;
	mysql_close($connect);
}

$dept  = array();
$box = array();

while ($row = mysql_fetch_array($result)) {
          $dept[] = $row['department'];
          $box[] = $row['custref'];
}

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: application/json");
$json = "";
$json .= "{\n";
$json .= "dept: [\"". implode('","', $dept). "\"],\n";
$json .= "box: [\"". implode('","', $box) ."\"]\n";
$json .= "}\n";
echo $json;

$sql = "INSERT INTO `act` (department, item) VALUES ('". implode("'),('", $dept) . "'),('" . implode("'),('", $box) . "')";
$result = runSQL($sql);

Open in new window

Avatar of ncoo
ncoo

Your select statement has an implode on dept and item, if there are exactly two dept and two items your code will work,.

However the error suggests this isn't happening,

If you echo out $rsql in your runSQL() function you'll see the problem.

You are trying to insert more values into a row then there are columns.
ASKER CERTIFIED SOLUTION
Avatar of ncoo
ncoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peter_coop

ASKER

excellent. thanks very much