Jump to content

Fail to input dataColumn count doesn't match value count at row 1


Taku

Recommended Posts

Guys can someone help me about my problem.. I have no idea why this error is always coming up

 

Fail to input dataColumn count doesn't match value count at row 1

 

 

<?php


require "connect.php";


if($db_tr)
{


if(isset($_POST['Input']))
{
$cl = $_POST['clt'];
$c = $_POST['ct'];
$cb = $_POST['cbt'];
$cc = $_POST['cct'];
$hc = $_POST['hct'];
$m = $_POST['mt'];
$ma = $_POST['mat'];
$pmt = $_POST['pmtt'];
$sb = $_POST['sb'];
$u = $_POST['u'];
$vl = $_POST['vl'];
$wm = $_POST['wm'];
$bp = $_POST['bp'];
$bf = $_POST['bf'];
$jm = $_POST['jm'];
$cm = $_POST['cm'];
$hh = $_POST['hh'];
$dl = $_POST['dl'];
$fs = $_POST['fs'];
$mg = $_POST['mg'];
$cs = $_POST['cs'];
$cj = $_POST['cj'];
$ck = $_POST['ck'];
$rm = $_POST['rm'];
$rc = $_POST['rc'];
$mw = $_POST['mw'];
$sg = $_POST['sg'];
$ts = $_POST['ts'];
$cd = $_POST['cd'];


if(!$cl OR !$c OR !$cb OR !$cc OR !$hc OR !$m OR !$ma OR !$pmt OR !$sb OR !$u OR !$vl OR !$wm OR !$bp OR !$bf OR !$jm OR !$cm OR !$hh OR !$dl OR !$fs OR !$mg OR !$cs OR !$cj OR !$ck OR !$rm OR !$rc OR !$mw OR !$sg OR !$ts OR !$cd)
{
echo '<script type="text/javascript">alert("Please fill up the form")</script>';
require "custodian.php";
}
else
{
$sql = mysql_query("INSERT INTO $tbl_name(cl,c,cb,cc,hc,m,ma,pmt,sb,u,vl,wm,bp,bf,jm,cm,hh,dl,fs,mg,cs,cj,ck,rm,rc,mw,sg,ts,cd)
VALUES( '$cl','$c','$cb','$cc','$hc','$m','$ma','$pmt','$sb','$u','$vl','$wm','$bp','$bf','$jm','$cm','$hh','$dl','$fs','$mg','$cs','$cj','$ck''$rm','$rc','$mw','$sg','$ts','$cd')");
if(!$sql)
{
die('Fail to input data'. mysql_error());
}
else
{
echo("Success");
require "custodian.php";
}
}


}

}
mysql_close($db_handle);



?>

Edited by Taku
Link to comment
Share on other sites

There are several others examples out there, which are a lot worse. He did check for submission, and for SQL errors, after all. Also, giving him the benefit of the doubt he's also using pre-filled values in his form, in case of "validation" errors.

 

That said, those are the only highlights of this bit of code, I'm afraid.

 

Taku: Quick list of what you need to fix:

  • Do some proper validation on the input, to ensure that it's actually valid according to what you expect to get.
  • Give your variables sensible and descriptive names.
  • Do item-by-item validation of the input data, so that you can give accurate error messages to the user.
  • Don't mix/match code like you've done, do all of the PHP processing before you sent any HTML/content to the browser.
  • When adding data from variables into an SQL query, you need to escape the string data and typecast the numerical data. Otherwise you're wide open for SQL injections.
  • Instead of just echoing out "success" you really should use header () to redirect the user. This stops the data from being resubmitted, if the user refreshes the page.
  • Don't manually close the MySQL connection. PHP does this for you, and saves you from future headaches.
  • Use MySQLI or DBO instead of the old, outdated, and no longer maintained MySQL library. The PHP manual has more information on this.

 

Also you generally don't want to outright kill your scripts like that, when you encounter any fatal processing errors (such as SQL errors). It is much better to create a function to handle these errors, which properly closes the HTML code for you.

Also, sending the proper HTTP header is a nice thing to do, as it allows all kinds of programs to detect errors. ;)

 

Link to comment
Share on other sites

It's not just the php: ok, none of your variables make any sense, but neither do your database column names, and neither do your html form element names (with the exception of Input). You need to use relevent and descriptive names for things, otherwise the code meens nothing to anyone else and will be impossible for even you to maintain.

 

As ChristianF said, it's not all that bad, and I admit I was a bit harsh with my last post, but this is a way of coding you need to stay clear of.

 

When you say "the first field" do you meen the entire first record or just the first entry in the cl column?

Link to comment
Share on other sites

what did I do wrong this time? (sorry for double post cant edit the last one)

 

<?php



require "connect.php";


if(isset($_POST['Edit']))
{
$query = "SELECT * FROM $tbl_name WHERE cafelatte = '$cafelatte'";
$result=mysql_query($query);
$num= mysql_num_rows($result);
mysql_close();


$i=0;
while($i < $num)
{
$cafelatte = mysql_result($result,$i,"cafelatte");
$chocolate = mysql_result($result,$i,"chocolate");
$cremebrulee = mysql_result($result,$i,"crembrulee");
++$i;
}
$u_cafelatte = mysql_escape_string($_POST['cafelattet']);
$u_chocolate = mysql_escape_string($_POST['chocolatet']);
$u_cremebrulee = mysql_escape_string($_POST['cremebruleet']);

if(!$u_cl OR !$u_c OR !$u_cb)
{
echo '<script type="text/javascript">alert("Please fill up the form")</script>';
require "custodian.php";
}
else
{
$query1 = "UPDATE $tbl_name SET cafelatte='$u_cafelatte', chocolate='$u_chocolate', cremebrulee='$u_cremebrulee'";
mysql_query($query1) or die('error'.mysql_error().'in query'.$SQL);
echo "Record Update";
mysql_close();

}
}


?>

Edited by Taku
Link to comment
Share on other sites

you need to use a unique identifier in the where clause to update a single record or else every record will be updated. some examples of uptate syntax:

UPDATE table SET name='muddy_funster' WHERE id = 1;

UPDATE table SET name='muddy_funster' WHERE id = (SELECT id from table as t where email = 'muddy_mail@your.net');

UPDATE table
INNER JOIN table2
ON (table.id=table2.id)
SET table.value = (table2.value / 100) * 10;

 

does that help any?

Link to comment
Share on other sites

remeber to use descriptive names! :P good luck

 

hahaha yes tnx so much I realized my mistake

 

back and now I need help again

 

{
$query1 = "UPDATE $tbl_name SET beg_inven='$u_cafelatte', beg_inven='$u_chocolate', beg_inven='$u_cremebrulee' where flav = cafelatte AND flav = chocolate AND flav = creambrulee";
}

 

is this right? if it is.. is there a way to make this short? tnx guys

Edited by Taku
Link to comment
Share on other sites

You have some problems with your logic: The flav can't be equal to two (or more things) at the same time. Only in quantum physics are such states possible. ;)

 

What you need here, is the SWITCH - CASE case statement for the UPDATE query. This thread shows how it's used.

 

hahah thought so `.` and tnx so much @@

Link to comment
Share on other sites

$query1 = "UPDATE f_beg
 SET begin_inven=
 case flav
 when = cafelatte
 then $u_cafelatte
 when = chocolate
 then $u_chocolate
 when = cremebrulee
 then $_cremebrulee

end
WHERE flav(cafelatte,chocolate,cremebrulee)"

 

is this correct?

Edited by Taku
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.