I have some code which inserts a title and comments from the user. Could be 1 title and comment or up to 10. The problem is if the user leaves the comments section blank, just adding the title the insert statement fails.The title and comments come from a form post.
So for example if the user adds just one title ($collection_item_title_1) and no comment (collection_item_text_1), nothing gets inserted.
The data which comes from a $_POST:
[php]
$form = $_POST;
$category_name = $form[ ‘selectedcat’ ];
$collectionname = $form[ ‘title’ ];
$collection_title_comment = $form[ ‘commenttitle’ ];
$collection_item_title_1 = $form[ ‘number1’ ];
$collection_item_text_1 = $form[ ‘comment1’ ];
$collection_item_title_2 = $form[ ‘number2’ ];
$collection_item_text_2 = $form[ ‘comment2’ ];
$collection_item_title_3 = $form[ ‘number3’ ];
$collection_item_text_3 = $form[ ‘comment3’ ];
$collection_item_title_4 = $form[ ‘number4’ ];
$collection_item_text_4 = $form[ ‘comment4’ ];
$collection_item_title_5 = $form[ ‘number5’ ];
$collection_item_text_5 = $form[ ‘comment5’ ];
$collection_item_title_6 = $form[ ‘number6’ ];
$collection_item_text_6 = $form[ ‘comment6’ ];
$collection_item_title_7 = $form[ ‘number7’ ];
$collection_item_text_7 = $form[ ‘comment7’ ];
$collection_item_title_8 = $form[ ‘number8’ ];
$collection_item_text_8 = $form[ ‘comment8’ ];
$collection_item_title_9 = $form[ ‘number9’ ];
$collection_item_text_9 = $form[ ‘comment9’ ];
$collection_item_title_10 = $form[ ‘number10’ ];
$collection_item_text_10 = $form[ ‘comment10’ ];
[/php]
This places the data into the array.
[php]
//Build Array
$values = array($collection_item_title_1, $collection_item_text_1, $collection_item_title_2, $collection_item_text_2,
$collection_item_title_3, $collection_item_text_3, $collection_item_title_4, $collection_item_text_4,
$collection_item_title_5, $collection_item_text_5, $collection_item_title_6, $collection_item_text_6,
$collection_item_title_7, $collection_item_text_7, $collection_item_title_8, $collection_item_text_8,
$collection_item_title_9, $collection_item_text_9, $collection_item_title_10, $collection_item_text_10);
// get the empty array keys using array_keys
$keys = array_keys($values,"");
// foreach empty key, we unset that entry
foreach ($keys as $k)
unset($values[$k]);
[/php]
In this code the collection_item_text_1 is empty and it is unset using the example above. So the $values array will be without [1] index. But this index is used in the insert statement. In the end the users title and comment dose not get posted because the comment is empty.
[php]
$statement1 = $link->prepare(“INSERT INTO collection_items(collection_list_id, collection_item_number, username_id, collection_item_title, collection_item_text, vote_count, created_date)
VALUES(:collection_list_id, :collection_item_number, :username_id, :collection_item_title, :collection_item_text, :vote_count, now())”);
for ($i = 0, $p = 1, $initial_vote=10; $i < count($values); $i++, $p++,$initial_vote–) {
$statement1->execute(array(
“collection_list_id” => $last_id,
“collection_item_number” => “$p”,
“username_id” => $slusername,//$user_number,
“collection_item_title” => $values[$i],
“collection_item_text” => $values[++$i],
“vote_count” => “$initial_vote”
));
[/php]
So how do I check to see if there is a title and no comment and to insert a empty or null into the comment? I was thinking about looping through the array, if it finds a title without a comment it adds an empty or null, I’m not great at nested loops.
Any help would be appreciated.
Thanks