• PHP Help PHP Databases
  • [RESOLVED] Column count doesn't match value count at row 1 - trying to upload file to database

Im trying to upload a file to my database and am getting an error saying this:
Column count doesn't match value count at row 1
I have looked at the SQL statement and it lines up with what I have in the table. So I was wondering maybe it could be something to do with my php but am not sure where to look

Here is the php code:

<?php
include("database_vars.php");

$cust_equip = "SELECT ce_id, comp_name
			   FROM cust_equipment, company
			   WHERE company.comp_id = cust_equipment.comp_id";
$cust_result = mysql_query($cust_equip);

if(isset($_POST['stand_submit'])) {
	$stand_id = $_POST['stand_id'];
	$cust_equip = $_POST['cust_equip'];
	$doc_type = $_POST['doc_type'];
	$file_name = $_FILES['stand_doc']['name'];
	$tmp_name = $_FILES['stand_doc']['tmp_name'];
	$file_size = $_FILES['stand_doc']['size'];
	$file_type = $_FILES['stand_doc']['type'];

$fp = fopen($tmp_name, 'r');
$content = fread($fp, $file_size);
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc()) {
	$file_name = addslashes($file_name);
}

$insert_query = "INSERT INTO standards(st_id, ce_id, st_type, st_file_name, st_file_type, st_file_size, st_file)
				 VALUES('$stand_id', '$cust_equip', '$doc_type', '$file_name', '$file_type' '$file_size', '$content')";
$insert_result = mysql_query($insert_query) or die('Error, query failed ' . mysql_error());
echo "<br />File " . $file_name . "uploaded<br />";
}

?>

and here is the form that puts data into the database:

<form action="" enctype="multipart/form-data" method="post" >
	<p>Standard Procedure ID: <input type="text" name="stand_id" /></p>
	<p>Customer: <select name="cust_equip">
	<?php while($cust_array = mysql_fetch_array($cust_result)) {
		echo "<option value='" . $cust_array[ce_id] . "'>" . $cust_array[comp_name] . "</option>\n";
	}?>
	</select></p>
	<p>Document Type: <select name="doc_type">
	<option>Drawing</option>
	<option>CheckSheet</option>
	<option>Manual</option>
	<option>Estimating Document</option>
	</select></p>
	<p>Document: <input type="hidden" name="MAX_FILE_SIZE" value="2000000" /><input type="file" name="stand_doc" size="50" /></p>
	<p><input type="submit" name="stand_submit" value="Submit" /></p>
	</form>

So is there anything wrong with my code or is the file just not being uploaded?

    A manual count of the columns in the insert statement shows that they match, so that should not be a problem. Perhaps this is an SQL injection problem, but chances are the use of addslashes() avoids that (but maybe not for a binary file, or one with particular encodings). The first fix you can try is to use the more correct [man]mysql_real_escape_string/man instead of addslashes().

    If that does not work, perhaps it would just be better to switch to the PDO extension and make use of prepared statements. With PDO:😛ARAM_STR and PDO:😛ARAM_LOB, you can specify exactly that you want to insert a text and a BLOB, respectively.

    Also, another way of doing this is to use [man]move_uploaded_file/man to upload the file to the system, and then store the path to the file in the database.

      I changed where I put the files from the database to the server using the move_uploaded_file command and I can see that the file was uploaded but I am still getting the error I was getting before with the SQL statement. I also ouput the SQL statment to the browser and both the table column and the value count was the same, so there must be an error else where or I have a problem with my server somewhere

        Well I found the problem, seems I missed a comma. Just a small stupid mistake.

          Write a Reply...