Hello!!

I'm trying to upload images and text onto my database but i have a bit of a problem...

Error, query failed 1136-Column count doesn't match value count at row 1

I dont know if the problem is the script or the actuall database, I google it but I couldn't get anywhere on this one!
Sorry if the code is a bit long!
Here is the database structure

CREATE TABLE categories (
categoryID INT(5) NOT NULL,
cat_Description VARCHAR(50),
PRIMARY KEY (itemID)
)TYPE = INNODB;

CREATE TABLE items (
itemID INT(5) NOT NULL AUTO_INCREMENT,
categoryID INT(5) NOT NULL,
itemName CHAR(25) NOT NULL,
item_Description VARCHAR(255),
price CHAR(10),
contactName VARCHAR(50),
phone CHAR(15),
email VARCHAR(50),
website CHAR (25),
submitDate DATE NOT NULL,
expireDate DATE NOT NULL,
PRIMARY KEY(itemID,categoryID),
INDEX (submitDate),
FOREIGN KEY (categoryID) REFERENCES categories (categoryID)
ON DELETE CASCADE
)TYPE = INNODB;

CREATE TABLE images (
imagesID INT (5) NOT NULL AUTO_INCREMENT,
itemID INT (5) NOT NULL,
name VARCHAR (30) NOT NULL,
size INT (11) NOT NULL,
type VARCHAR (30) NOT NULL,
pix MEDIUMBLOB NOT NULL,
PRIMARY KEY(imagesID),
FOREIGN KEY (itemID) REFERENCES items (itemID)
ON DELETE CASCADE
)TYPE = INNODB;

And here are the scripts one is the form to upload images and the text (postad.php) and the other one is the php and sql script to upload the details onto the database.

postad.php

<form action = "upload.php" method = "POST" enctype="multipart/form-data">
<select name="CID" size="1">
<option selected value="">Select Category</option>
<?php
while ($cat = mysql_fetch_array($cats)) {
$CID = $cat['categoryID'];
$catDescription = htmlspecialchars($cat['cat_Description']);
echo("<option value='$CID'>$catDescription</option>\n");
}
?>
</select><br /> <br />
<p>
<b>Title of ad: </b><input type="text" name="itemName" id="itemName"/><br /><br />
<b>Description: </b><br />
<textarea name="item_Description" id="item_Description" rows="10" cols="40" wrap>
</textarea><br/><br/>
<b>Price: </b><input type="text" name="price" id="price"/><br />
<input type="hidden" name="itemID" /><br /><br />
<b>Contact Name: </b><input type="text" name="contactName" id="contactName"/><br /><br />
<b>Phone Number: </b><input type="text" name="phone" id="phone"/><br /><br />
<b>Email Address: </b><input type="text" name="email" id="email"/><br /><br />
<b>Website: </b><input type="text" name="website" id="website"/><br /><br />
<b>Upload image: </b>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"><br />
<input type="hidden" name="imageID" /><br /><br />
<input type="file" name="userfile" id="userfile" /><br /><br />
<input type="submit" name="submit" id="submit" class="box" value="SUBMIT AD">
<input type="submit" name="goBack" value="Cancel">
</form>

upload.php

<?php 
include("misc.inc");

$db = 0;
$db = mysql_connect($db_host . ":" . $db_port, $db_user, $db_pwd);
if ($db == 0) {die("Cannot connect, Error <b>" . mysql_errno() . "</b>: " . mysql_error());};

mysql_query("USE " . $db_db);
if (mysql_errno() != 0) {die("Cannot USE database, Error <b>" . mysql_errno() . "</b>: " . mysql_error());};

if (@$_POST['goBack'] == "Cancel")
{
	header("Location: index.php");
}

if(isset($_POST['submit']) && $_FILES['userfile']['size'] > 0)
{
$itemID = $_POST ['itemID'];
$imageID =$_POST ['imageID'];	
$itemName = $_POST ['itemName'];
$CID = $_POST ['CID'];
$item_Description = $_POST ['item_Description'];
$price = $_POST ['price'];
$contactName = $_POST ['contactName'];
$phone = $_POST ['phone'];
$email = $_POST ['email'];
$website =$_POST ['website'];

$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$pix = fread($fp, filesize($tmpName));
$pix = addslashes($pix);
fclose($fp);

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

$sql = "INSERT INTO items SET
		itemID = '$itemID',
		itemName = '$itemName',
		categoryID = '$CID',
		item_Description = '$item_Description',
		price = '$price',
		contactName = '$contactName',
		phone = '$phone',
		email = '$email',
		website ='$website',
		submitDate = CURDATE()";

if (@mysql_query ($sql)) {
	  echo('<p>Thank you for your posting. If you wish your item or other items go to Classifieds</p>');
	  } 
	else {
	  echo('<p>Error adding submitted ad: ' .mysql_errno().'-'.
            mysql_error() . '</p>');
	  }
	}
    
	  echo('<p><a href="postad.php"> Post another ad!</a></p>');
	  echo ('<p><a href="Classifieds.php"> Classifieds</a></p>');
	  echo ('<p><a href="home.php"> Home</a></p>'
		);

$sql1 = "INSERT INTO images (imageID, itemID, name, size, type, pix) ".
"VALUES ('$itemID', '$fileName', '$fileSize', '$fileType', '$pix')";

mysql_query($sql1) or die('<p>Error, query failed ' .mysql_errno().'-'.mysql_error() .'</p>');

echo "<br>File $fileName uploaded<br>";
?>

Thank u
Hernan

Recommended Answers

All 2 Replies

I think "column count doesn't match value count" means that when you try to insert data in your database, you have to many columns for data. What I mean to say : on line 70 you specify 6 columns, and only 5 values. If the first column (imageID) is an autoincrement-column, you do not have to specify that column.
Anyway, i think you're problem lies in line 70/71

Well spotted geertc!!

thank you very much.

Hernan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.