Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/170643-insert-sql-issue/
Share on other sites

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.";
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900038
Share on other sites

<?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.";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900058
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900070
Share on other sites

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900082
Share on other sites

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 

Link to comment
https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900087
Share on other sites

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];
}

Link to comment
https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900101
Share on other sites

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']);

} 

Link to comment
https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900108
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/170643-insert-sql-issue/#findComment-900128
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.