Taku Posted January 17, 2013 Share Posted January 17, 2013 (edited) 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 January 17, 2013 by Taku Quote Link to comment Share on other sites More sharing options...
requinix Posted January 17, 2013 Share Posted January 17, 2013 The number of fields you mention in your INSERT does not match up with the number of values you provided. Something's extra or something's missing. Quote Link to comment Share on other sites More sharing options...
Taku Posted January 17, 2013 Author Share Posted January 17, 2013 ohh tnx I was able to solve it.. my problem now is in my phpmyadmin the first field always have 0 value.. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted January 17, 2013 Share Posted January 17, 2013 Taku, would you be willing to submit that code as an example of "worst practices: how not to code in any language ever"? Quote Link to comment Share on other sites More sharing options...
Taku Posted January 17, 2013 Author Share Posted January 17, 2013 ofc cuz this is my first try in php and I know its bad xD Quote Link to comment Share on other sites More sharing options...
Christian F. Posted January 17, 2013 Share Posted January 17, 2013 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. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted January 17, 2013 Share Posted January 17, 2013 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? Quote Link to comment Share on other sites More sharing options...
Taku Posted January 17, 2013 Author Share Posted January 17, 2013 (edited) tnx for reminding me muddy_funster and Christian yah I will haha... I am confuse now because of what I did... I will keep what u guys told me in mind also the 1st entry got 0 Edited January 17, 2013 by Taku Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted January 17, 2013 Share Posted January 17, 2013 That normally happens when you try to put a string value into an int field which has a default value of 0. check the datatype and the variable value being inserted. Quote Link to comment Share on other sites More sharing options...
Taku Posted January 18, 2013 Author Share Posted January 18, 2013 I see o-o... I am so noob `.` and do you guys have an example of editing an existing record in the table and also deleting it... tnx so much Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted January 18, 2013 Share Posted January 18, 2013 to delete : DELETE FROM <tablename> WHERE <value> = <condition> and to change a record UPDATE <tablename> SET <columnName> = <value> WHERE <anotherValue> = <condition> Quote Link to comment Share on other sites More sharing options...
Taku Posted January 18, 2013 Author Share Posted January 18, 2013 so I have to retrieve the data first from the table? and then update one by one? Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted January 18, 2013 Share Posted January 18, 2013 not really, no. What is it you want to do (and why do you have a box on your head? )? Quote Link to comment Share on other sites More sharing options...
Taku Posted January 18, 2013 Author Share Posted January 18, 2013 (edited) I tried to update the record all become 4 like lol idk what I did hahaha (the box is nice u know) oh rofl I know what I did wrong haha wrong variable.. (changing it) Edited January 18, 2013 by Taku Quote Link to comment Share on other sites More sharing options...
Taku Posted January 18, 2013 Author Share Posted January 18, 2013 (edited) 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 January 18, 2013 by Taku Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted January 18, 2013 Share Posted January 18, 2013 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? Quote Link to comment Share on other sites More sharing options...
Taku Posted January 18, 2013 Author Share Posted January 18, 2013 I see yes it helps I think that is my problem (first time to know identifier T_T) Quote Link to comment Share on other sites More sharing options...
Christian F. Posted January 18, 2013 Share Posted January 18, 2013 You can retrieve and update the value in the same query, SET `count` = `count` + 1 for example. Quote Link to comment Share on other sites More sharing options...
Taku Posted January 18, 2013 Author Share Posted January 18, 2013 I know my problem now I created a wrong table crap crap `.` I will be back guys tnx so much (*bows) Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted January 18, 2013 Share Posted January 18, 2013 remeber to use descriptive names! good luck Quote Link to comment Share on other sites More sharing options...
Taku Posted January 18, 2013 Author Share Posted January 18, 2013 (edited) remeber to use descriptive names! 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 January 18, 2013 by Taku Quote Link to comment Share on other sites More sharing options...
Christian F. Posted January 18, 2013 Share Posted January 18, 2013 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. Quote Link to comment Share on other sites More sharing options...
Taku Posted January 18, 2013 Author Share Posted January 18, 2013 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 @@ Quote Link to comment Share on other sites More sharing options...
Christian F. Posted January 18, 2013 Share Posted January 18, 2013 You're welcome. Quote Link to comment Share on other sites More sharing options...
Taku Posted January 18, 2013 Author Share Posted January 18, 2013 (edited) $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 January 18, 2013 by Taku Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.