Column count doesn't match value count at row 1

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashraf02
    New Member
    • Feb 2008
    • 53

    #1

    Column count doesn't match value count at row 1

    hi

    i am trying to insert a record via a form using php and mysql and when i try to submit the form it comes up with the following error

    Column count doesn't match value count at row 1

    if someone can please help me overcome this error i would be grateful.

    the insert_shirt.ph p code is:

    [PHP]<?php
    $conn = mysql_connect(" localhost","roo t","")
    or die (mysql_error()) ;
    mysql_select_db ("noble",$conn) ;

    $insert = "INSERT INTO Shirts
    VALUES('$_POST[Shirt_ID]','$_POST[Smart_ID]','$_POST[Shirt_Brand]','$_POST[Shirt_Type]',
    '$_POST[Shirt_Colour]','$_POST[Shirt_Price]','$_POST[Shirt_image]',
    '$_POST[Shirt_Descripti on]')";

    if (mysql_query($i nsert,$conn)) {
    echo "Record Added";
    } else {
    echo mysql_error();
    }
    ?>[/PHP]

    the form code is :

    [HTML]<body>
    <h1><img src="Web Images/Banners/SuitFormTitle1. jpg"></img></h1>
    <table>
    <tr>
    <td>
    <h3>
    <a href="Suit_Form .html"><img src="Web Images/Buttons/Smart.jpg"></img></a><br />
    <a href="Casual_Fo rm.html"><img src="Web Images/Buttons/Casual.jpg"></img></a><br />
    <a href="Suit_Form .html"><img src="Web Images/Buttons/Urban.jpg"></img></a><br />
    </h3>
    </td>
    </tr>
    </table>
    <div style='margin:0 auto;width:600p x;'>
    <form action="Insert_ Shirt.php" method="post">
    <div class="row">
    <span class="item"><h 2>Shirt ID</h2></span>
    <span class="formw">< input type="text" name="Shirt_ID" size="5" /></span>
    </div>
    <div class="row">
    <span class="item"><h 2>Smart ID</h2></span>
    <span class="formw">< input type="text" name="Smart_ID" value="2" size="5" /></span>
    </div>
    <div class="row">
    <span class="item"><h 2>Shirt Brand</h2></span>
    <span class="formw">< input type="text" name="Shirt_Bra nd" size="20"/></span>
    </div>
    <div class="row">
    <span class="item"><h 2>Shirt Type</h2></span>
    <span class="formw">< input type="text" name="Shirt_Typ e" size="20"/></span>
    </div>
    <div class="row">
    <span class="item"><h 2>Shirt Colour</h2></span>
    <span class="formw">< input type="text" name="Shirt_Col our" size="20"/></span>
    </div>
    <div class="row">
    <span class="item"><h 2>Shirt Price</h2></span>
    <span class="formw">< input type="text" name="Shirt_Pri ce" size="10"/></span>
    </div>
    <div class="row">
    <span class="item"><h 2>Shirt Image</h2></span>
    <span class="formw">< input type="text" name="Shirt_ima ge" size="30" value="web images\Items\Sh irts\" /></span>
    </div>
    <div class="row">
    <span class="item"><h 2>Shirt Description</h2></span>
    <span class="formw">< textarea name="Shirt_Des cription" rows="5" cols="20"></textarea></span>
    </div>
    <div class="row">
    <span class="item"></span>
    <span class="formw">< input type="image" name="submit" src="Web Images/Buttons/Submit.jpg"/></span>
    </form>
    </div>
    </body>[/HTML]

    i am trying to insert the following values:

    Code:
    '1',
    '2',
    'YSL',
    'Pleated',
    'Grey',
    '90.00',
    'image_url',
    'Description'.
    thanks in advance

    ashraf
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    I can't see it from the code, but maybe I am shortsghted. One problem could be that you have more columns than you insert (what about an index column?).

    Best way to do such an insert is to name the columns in your sql statement. Since you have not done that, the quickest way now is to do an [php]echo $insert;[/php]right before the mysql_query and you can see the contents and check the hnumber of columns in the sql statement.

    Ronald

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Originally posted by ronverdonk
      I can't see it from the code, but maybe I am shortsghted. One problem could be that you have more columns than you insert (what about an index column?).

      Best way to do such an insert is to name the columns in your sql statement. Since you have not done that, the quickest way now is to do an [php]echo $insert;[/php]right before the mysql_query and you can see the contents and check the hnumber of columns in the sql statement.

      Ronald
      Well, he hasn't given php the names of the columns he wants to INSERT INTO, so ofcourse php will throw an error, right?

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Originally posted by markusn00b
        Well, he hasn't given php the names of the columns he wants to INSERT INTO, so of course php will throw an error, right?
        No you don't have to give the column names, you can do it with VALUE() only.

        But then you must be sure that you specify the values for ALL columns in the table. Usually you forget the primary key auto_increment field, because you think that it will be done by default, but that is not true. For such a column you must specify a zero (0), then it will be auto incremented when inserted. But you may not omit any column values when using just VALUES().

        Ronald

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by ronverdonk
          No you don't have to give the column names, you can do it with VALUE() only.

          But then you must be sure that you specify the values for ALL columns in the table. Usually you forget the primary key auto_increment field, because you think that it will be done by default, but that is not true. For such a column you must specify a zero (0), then it will be auto incremented when inserted. But you may not omit any column values when using just VALUES().

          Ronald
          I see.

          Could you use NULL for the auto-increment aswell?

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Originally posted by markusn00b
            I see.

            Could you use NULL for the auto-increment as well?
            In the insert you mean? Yes, the row will normally auto increment with that (as if it were a 0).

            Ronald

            Comment

            • ashraf02
              New Member
              • Feb 2008
              • 53

              #7
              thanks guys i have figured out the problem i took the advice of ronald and looked at my sql table i had a colour field in the code and not on the table. kinda stupid from my part.

              thanks alot

              ashraf.

              Comment

              Working...