hola Posted December 8, 2007 Share Posted December 8, 2007 I have a function that inserts into table tru Post method of form. But it is not inserting into database. Under Function I have foreach loop and insert statement. Function fun($fields, $values, $table){ foreach ($fields...){...} foreach($values..){...} mysql_query("INSERT INTO $table($fields)VALUES($values)") or die('Fail Inserting...'); } For coding side... $connect->fun($fields,$values,"table"); I tried Debugging the value passed is correct...but its not inserting into DB; Array ( [ 0 ] => test [1] => 1 ) Error: Fail Inserting... Why is it not inserting? Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted December 8, 2007 Share Posted December 8, 2007 There are problems with your SQL syntax. Try changing your technique, you are over simplifying. please include more code there are details missing. P.S. use code tags. Function fun($fields, $values, $table) Quote Link to comment Share on other sites More sharing options...
hola Posted December 8, 2007 Author Share Posted December 8, 2007 Here is the code: Class test{ function insert($fields, $values, $table) { foreach ($fields as $field) { $queryfields += "`". $field .","; } foreach ($values as $value) { $queryvalues += "'". $value ."'"; } mysql_query("INSERT INTO $table($queryfields)VALUES($queryvalues)") or die('Inserting failed...'); } } ---------------------------------------------------------------------------------------------------- if(isset($_POST['submit'])){ $name = $_POST['name']; $age = $_POST['age']; $fields = array("name", "age"); $values = array($name, $age); $test->insert($fields,$values,"table_test"); } Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted December 8, 2007 Share Posted December 8, 2007 Instead of using foreach to join the values within an array together you could just use implode instead: <?php Class test { function insert($fields, $values, $table) { $qryFields = implode("','", $fields); $qryValues = implode("','", $values); $qry = "INSERT INTO $table('$qryFields') VALUES ('$qryValues')"; mysql_query($qry) or die('QUERY ERROR: Inserting failed...<br /><code>'.$qry.'</code><br />' . mysql_error()); } } if(isset($_POST['submit'])) { $name = $_POST['name']; $age = $_POST['age']; $fields = array('name', 'age'); $values = array($name, $age); $test = new test; $test->insert($fields, $values, 'table_test'); } ?> Quote Link to comment Share on other sites More sharing options...
hola Posted December 8, 2007 Author Share Posted December 8, 2007 its still givin me error; I tried debugging ...everything appears correctly but still givin me the error; ??? Array ( [ 0 ] => bob [1] => 19 ) INSERT INTO test('name','age') VALUES ('bob','19') QUERY ERROR: Inserting failed... Is it because of the "age" filed being numeric and its passing string? or is it something else? Quote Link to comment Share on other sites More sharing options...
hola Posted December 8, 2007 Author Share Posted December 8, 2007 I solved it...the problem was with the fields...because it has ' ...it should be without it. thanks "wildteen88" Quote Link to comment Share on other sites More sharing options...
hola Posted December 8, 2007 Author Share Posted December 8, 2007 How do I do for "Update and Delete" mysql function? 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.