Eggzorcist Posted August 17, 2009 Share Posted August 17, 2009 For some reason this function isn't working and I'm not quite sure why... Here's the function code, it doesn't give me any error, but it goes to the else and outputs error and doesn't add anything to the database... I'm really not sure why not. Any help would be greatly appreciated. function addeventToPending($eventTitle, $from1, $from2, $fromtime, $to1, $to2, $totime, $quickDesc, $description){ $query = "INSERT INTO events (name, fromslashes, fromdisplay, fromtime, toslashes, todisplay, totime, quickdescription, pagedescription, sameday, status) VALUES ($eventTitle, $from1, $from2, $fromtime, $to1, $to2, $totime, $quickDesc, $description)"; $insert_query = mysql_query($query); if($insert_query){ echo "Your event was put up for approval."; } else { echo "Error"; } } Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/170643-insert-sql-issue/ Share on other sites More sharing options...
JonnoTheDev Posted August 17, 2009 Share Posted August 17, 2009 Use mysql_error() to see the query error <?php function addeventToPending($eventTitle, $from1, $from2, $fromtime, $to1, $to2, $totime, $quickDesc, $description) { $query = "INSERT INTO events (name, fromslashes, fromdisplay, fromtime, toslashes, todisplay, totime, quickdescription, pagedescription, sameday, status) VALUES ($eventTitle, $from1, $from2, $fromtime, $to1, $to2, $totime, $quickDesc, $description)"; if(!mysql_query($query)) { die(mysql_error()); } echo "Your event was put up for approval."; } ?> You would have been better passing in an associative array as a function parameter. As a rule of thumb if you have more than 3 parameters use an array. Also clean your data to prevent sql error. <?php function addeventToPending($data) { // clean data foreach($data as $key => $value) { $data[$key] = mysql_real_escape_string($value); } if(!mysql_query("INSERT INTO events (name, fromslashes, fromdisplay, fromtime, toslashes, todisplay, totime, quickdescription, pagedescription, sameday, status) VALUES (".$data['eventTitle'].", ".$data['from1'].", ".$data['from2'].", ".$data['fromtime'].", ".$data['to1'].", ".$data['to2'].", ".$data['totime'].", ".$data['quickDesc'].", ".$data['description'].")")) { die(mysql_error()); } echo "Your event was put up for approval."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900038 Share on other sites More sharing options...
PFMaBiSmAd Posted August 17, 2009 Share Posted August 17, 2009 String data values need to be enclosed in single-quotes to make them actual strings. Quote Link to comment https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900039 Share on other sites More sharing options...
JonnoTheDev Posted August 17, 2009 Share Posted August 17, 2009 <?php function addeventToPending($data) { // clean data foreach($data as $key => $value) { $data[$key] = mysql_real_escape_string($value); } if(!mysql_query("INSERT INTO events (name, fromslashes, fromdisplay, fromtime, toslashes, todisplay, totime, quickdescription, pagedescription, sameday, status) VALUES ('".$data['eventTitle']."', '".$data['from1']."', '".$data['from2']."', '".$data['fromtime']."', '".$data['to1']."', '".$data['to2']."', '".$data['totime']."', '".$data['quickDesc']."', '".$data['description']."')")) { die(mysql_error()); } echo "Your event was put up for approval."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900058 Share on other sites More sharing options...
Eggzorcist Posted August 17, 2009 Author Share Posted August 17, 2009 So using the associative array method, how would I put the function together with the $_POST? I'm currently using addeventToPending($_POST['title'], $_POST['datepicker1'], $_POST['alternative1'], $_POST['timestart'], $_POST['datepicker1'], $_POST['alternative2'], $_POST['timefinish'], $_POST['Qdescription'], $_POST['elm1']); and this isn't working... Any tips? I like to assoc idea however. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900070 Share on other sites More sharing options...
JonnoTheDev Posted August 17, 2009 Share Posted August 17, 2009 You could use the following method. However it makes life a lot easier when your form field names have the same name as your database fields. Here I am adding the data in the form fields to the corresponding database fields. <?php function addeventToPending($data) { // clean data foreach($data as $key => $value) { $data[$key] = mysql_real_escape_string($value); } if(!mysql_query("INSERT INTO events (name, fromslashes, fromdisplay, fromtime, toslashes, todisplay, totime, quickdescription, pagedescription, sameday, status) VALUES ('".$data['eventTitle']."', '".$data['from1']."', '".$data['from2']."', '".$data['fromtime']."', '".$data['to1']."', '".$data['to2']."', '".$data['totime']."', '".$data['quickDesc']."', '".$data['description']."')")) { die(mysql_error()); } echo "Your event was put up for approval."; } // usage $inputFieldNamesToDb = array('eventTitle' => 'title', 'from1' => 'datepicker1', 'from2' => 'alternative1', 'fromtime' => 'timestart', 'to1' => 'datepicker1', 'to2' => 'alternative2', 'totime' => 'timefinish', 'quickDesc' => 'Qdescription', 'description' => 'elm1'); foreach($inputFieldNamesToDb as $dbField => $postField) { $data[$dbField] = $_POST[$postField]; } addeventToPending($data); ?> Quote Link to comment https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900082 Share on other sites More sharing options...
Eggzorcist Posted August 17, 2009 Author Share Posted August 17, 2009 I added //usage before the function but I still get the same error. Warning: Invalid argument supplied for foreach() in /Users/JPFoster/Sites/Event_Profiler/scripts/functions.php on line 213 Column count doesn't match value count at row 1 Quote Link to comment https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900087 Share on other sites More sharing options...
JonnoTheDev Posted August 17, 2009 Share Posted August 17, 2009 Post your whole code with the function Quote Link to comment https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900098 Share on other sites More sharing options...
Eggzorcist Posted August 17, 2009 Author Share Posted August 17, 2009 function function addeventToPending($data){ foreach($data as $key => $value) { $data[$key] = mysql_real_escape_string($value); } if(!mysql_query("INSERT INTO events (name, fromslashes, fromdisplay, fromtime, toslashes, todisplay, totime, quickdescription, pagedescription, sameday, status) VALUES ('".$data['eventTitle']."', '".$data['from1']."', '".$data['from2']."', '".$data['fromtime']."', '".$data['to1']."', '".$data['to2']."', '".$data['totime']."', '".$data['quickDesc']."', '".$data['description']."')")) { die(mysql_error()); } echo "Your event was put up for approval."; } Form Area: if($_POST){ $inputFieldNamesToDb = array('eventTitle' => 'title', 'from1' => 'datepicker1', 'from2' => 'alternative1', 'fromtime' => 'timestart', 'to1' => 'datepicker1', 'to2' => 'alternative2', 'totime' => 'timefinish', 'quickDesc' => 'Qdescription', 'description' => 'elm1'); foreach($inputFieldNamesToDb as $dbField => $postField) { $data[$dbField] = $_POST[$postField]; } Quote Link to comment https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900101 Share on other sites More sharing options...
JonnoTheDev Posted August 17, 2009 Share Posted August 17, 2009 And have you called the function using foreach($inputFieldNamesToDb as $dbField => $postField) { $data[$dbField] = $_POST[$postField]; } // CALL FUNCTION addeventToPending($data); Quote Link to comment https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900103 Share on other sites More sharing options...
Eggzorcist Posted August 17, 2009 Author Share Posted August 17, 2009 Oh I forgot to post it the entire area. the function calling is done by: if($_POST){ $inputFieldNamesToDb = array('eventTitle' => 'title', 'from1' => 'datepicker1', 'from2' => 'alternative1', 'fromtime' => 'timestart', 'to1' => 'datepicker1', 'to2' => 'alternative2', 'totime' => 'timefinish', 'quickDesc' => 'Qdescription', 'description' => 'elm1'); foreach($inputFieldNamesToDb as $dbField => $postField) { $data[$dbField] = $_POST[$postField]; } addeventToPending($_POST['title'], $_POST['datepicker1'], $_POST['alternative1'], $_POST['timestart'], $_POST['datepicker1'], $_POST['alternative2'], $_POST['timefinish'], $_POST['Qdescription'], $_POST['elm1']); } Quote Link to comment https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900108 Share on other sites More sharing options...
JonnoTheDev Posted August 17, 2009 Share Posted August 17, 2009 You are calling the function incorrecly. It only takes 1 parameter now. // CALL FUNCTION addeventToPending($data); Look at the new code again Quote Link to comment https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900113 Share on other sites More sharing options...
Eggzorcist Posted August 17, 2009 Author Share Posted August 17, 2009 One error is delt with but I'm still getting the: Column count doesn't match value count at row 1 Quote Link to comment https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900120 Share on other sites More sharing options...
JonnoTheDev Posted August 17, 2009 Share Posted August 17, 2009 This is because you have specified 11 database field names but only provided 9 peices of data if(!mysql_query("INSERT INTO events (name, fromslashes, fromdisplay, fromtime, toslashes, todisplay, totime, quickdescription, pagedescription, sameday, status) VALUES ('".$data['eventTitle']."', '".$data['from1']."', '".$data['from2']."', '".$data['fromtime']."', '".$data['to1']."', '".$data['to2']."', '".$data['totime']."', '".$data['quickDesc']."', '".$data['description']."')")) { die(mysql_error()); } It is much easier to use the following sql syntax for insert/update "INSERT INTO events SET name='".$data['eventTitle']."',fromslashes='".$data['from1']."',fromdisplay='".$data['from2']."',fromtime='".$data['fromtime']."',toslashes='".$data['to1']."',todisplay='".$data['to2']."'" // complete the rest yourself Quote Link to comment https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900128 Share on other sites More sharing options...
JonnoTheDev Posted August 17, 2009 Share Posted August 17, 2009 Read through your code thoroughly!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900130 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.