drop down value not being submitted to post array - php

Currently working with a php project in which I connect to a database in phpmyadmin. I'm currently implementing my one to many relationship and on one of my forms there is a drop down list with the list of categorys(foreign key) that a product in my database can have, however when I check the post array, and the array that contains all the values for the insert query everything is there except the foreign key.
Drop down list and array of values:
<select name="Category_ID">
<?php
foreach ($category as $cat) {
echo '<option value="' . $cat['ID'] . '">' . $cat['ID'] . '</option>';
}
?>
</select>
Array (
[authorName] => Hiroshi Sakurazaka
[bookName] => All You Need Is Kill
[costPrice] => 59
[sellPrice] => 99
[productCatID] => )
Could not insert book
Heres the file that converts the data in the formdata array into an object:
<?php
require_once 'product.php'; //Connecting to the product class
require_once 'Classes/productTable.php'; //Connecting to the TableGateway
require_once 'Classes/Connection.php'; //Connecting to the Connection class
require_once 'validateProduct.php';//Connecting to the product validation
require_once 'utils/functions.php';
//start_session();
//
//if (!is_logged_in()) {
// header("Location: login_form.php");
//}
echo '<pre>';
print_r($_POST);
echo '</pre>';
$formdata = array();
$errors = array();
validate($formdata, $errors);
if (empty($errors)) {
$AuthorName = $formdata['AuthorName'];
$BookName = $formdata['BookName'];
$costPrice = $formdata['CostPrice'];
$sellPrice = $formdata['sellPrice'];
$productCatID = $formdata['productCatID'];
echo '<pre>';
print_r($formdata);
echo 'Form Data array';
echo '</pre>';
$connection = Connection::getInstance();
$gateway = new productTable($connection);
$id = $gateway->insert($AuthorName, $BookName, $costPrice, $sellPrice, $productCatID);
header('Location: viewProducts.php');
}
else {
require 'createProductForm.php';
}
Heres the function in the table gateway that inserts the object into the database:
> public function insert($authorName, $bookName, $costPrice, $sellPrice,
> $productCatID) {
> $sql = "INSERT INTO "
> . "`product`(`AuthorName`, `BookName`, `CostPrice`, `sellPrice`, `productCatID`)"
> . " VALUES (:authorName,:bookName,:costPrice,:sellPrice,:productCatID)";
> $statement = $this->connection->prepare($sql);
> $params = array(
> "authorName" => $authorName,
> "bookName" => $bookName,
> "costPrice" => $costPrice,
> "sellPrice" => $sellPrice,
> "productCatID" => $productCatID
> );
> print_r($params);
> $status = $statement->execute($params);
>
> if (!$status) {
> die("Could not insert book");
> }
>
> $id = $this->connection->lastInsertId();
>
> return $id; }
can somebody please tell me what I'm missing?

Your select has the name of Category_ID not productCatID. If you expecting GET/POST data coming in under productCatID you need to name your select productCatID.

Solved my problem finally, so I'll post how I did it. For debuging my code and to see what values were being passed into the $POST array and the $formdata array, I used print_r to post each array if there was a problem and heres what I got:
$POST Array
(
[AuthorName] => g
[BookName] => g
[CostPrice] => 33
[sellPrice] => 3
[productCatID] => 4
[createProduct] => Create Product
)
form data array
(
[AuthorName] => g
[BookName] => g
[CostPrice] => 33
[sellPrice] => 3
[ProductCatID] =>
)
As you can see the $POST array was getting the value from the drop down list just fine, it was the form data array that was the issue. Embarrassingly the issue was just a simple typo error that was quickly resolved in my validation script.

With foreach you have to explicitly request access to the key, so if you don't, you'll only get the array values.
Just do this to debug (outside of the <select>):
foreach($category as $key=>$cat){
var_dump($cat['ID'], $cat, $key);
}
And I think you'll see where the actual data you need is.
(also, you seem to be running without strict errors and notices on, which is crucial for debugging and might show you some notices when array keys you try to access don't exist)

Related

SPARQL query on GraphDB with PHP locally

I'm trying to get information from my local ontology on GraphDB with a PHP script.
Online I've only found this this, so I've tried to adapt it to my necessities.
// ARC2 static class inclusion
include_once('semsol/ARC2.php');
$dbpconfig = array(
"remote_store_endpoint" => "http://localhost:7200/sparql",
);
$store = ARC2::getRemoteStore($dbpconfig);
if ($errs = $store->getErrors()) {
echo "<h1>getRemoteSotre error<h1>" ;
};
$query = 'PREFIX aplm: <http://www.andareperlomondo.it/rdf/ontologia.rdf#>
select * where {
?s aplm:nomeCognome ?o .
}';
// execute the query
$rows = $store->query($query, 'rows');
if ($errs = $store->getErrors()) {
echo "Query errors\n" ;
print_r($errs);
};
// display the results in an HTML table
foreach ($rows as $value) {
echo $value;
};
However, the connection doesn't work and in PHP prints the following error:
Array (
[0] => 404 "Not Found (HTTP status 404)..." in ARC2_Reader
[1] => missing stream in "getFormat" via ARC2_Reader
[2] => missing stream in "readStream" http://localhost:7200/sparql?query=%0APREFIX+aplm%3A+%3Chttp%3A%2F%2Fwww.andareperlomondo.it%2Frdf%2Fontologia.rdf%23%3E%0A%09%09%09%09%09%09%09%09select+%2A+where+%7B+%0A%09%09%09%09%09%09%09%09%09%3Fs+aplm%3AnomeCognome+%3Fo+.%0A%09%09%09%09%09%09%09%09%7D via ARC2_Reader
)
What shuld I do?

How to insert data to database from multiple select dropdown in codeigniter?

I have drop down which is look like the following
<select class="selectpicker form-control" multiple name="category[]">
<option value="1" id="MENTAL HEALTH">MENTAL HEALTH</option>
<option value="2" id="SUICIDE SUPPORT">SUICIDE SUPPORT</option>
<option value="3" id="HEALTH">HEALTH</option>
<option value="4" id="SUPPORT">SUPPORT</option>
</select>
In my controller I have the following code to get that data from the dropdown
public function save()
{
$ctgry= ($this->input->post('category'));
$orgName= ($this->input->post('InputName'));
$streetNo= ($this->input->post('StreetNo'));
$streetName= ($this->input->post('StreetName'));
$suburb= ($this->input->post('Suburb'));
$state= ($this->input->post('state'));
$postCode= ($this->input->post('PostCode'));
$count = count($ctgry);
$supprtGroup = array(
'name' => $orgName,
'street_no' => $streetNo,
'street' => $streetName,
'suburb' => $suburb,
'state' => $state,
'pc' => $postCode
);
$supportgroup_id = $this->Home_Model->addSupportGroup($supprtGroup);
$j = 0;
i = 0;
$sc_id = 0
echo 'aa';//just for error checking purpose
if ($count > 1)
{
echo ' aa1';//just for error checking purpose
while ($i > $count)
{
echo ' aa2';//just for error checking purpose
$support_category = array(
'supportgroup_id' => $supportgroup_id,
'category_id' => $ctgry[$j]
);
$sc_id = $this->Home_Model->addSupportrCategory($support_category);
$i++;
echo $sc_id;//just for error checking purpose
}
}
else
{
echo ' aa3';//just for error checking purpose
$support_category = array(
'supportgroup_id' => $supportgroup_id,
'category_id' => $ctgry[$j]
);
$sc_id = $this->Home_Model->addSupportrCategory($support_category);
echo $sc_id;//just for error checking purpose
}
echo'bb';//just for error checking purpose
}
The following is my model class
function addSupportGroup($supprtGroup=NULL)
{
$this->db->insert('support_group', $supprtGroup);
return $this->db->insert_id();
}
function addSupportrCategory($support_category=NULL)
{
$this->db->insert('support_category', $support_category);
return $this->db->insert_id();
}
When select multiple values from dropdown the out put as follows
aa aa1bb
Could anyone give me an answer why this is happening, if it is a single value
selected from the dropdown its saving to the DB fine I search every similar
posts but couldn't find the answer.
I can't see where $count is defined in your question so I don't know what it's value is meant to be, however, if you are getting "aa1" as output you are saying that it's bigger than 1.
With your while() you're saying that $i must be bigger than $count but $i equals 0 so therefore the while loop won't start.
If you are just wanting to loop through the categories it would be easier to just:
$supportgroup_id = $this->Home_Model->addSupportGroup($supprtGroup);
foreach ($ctgry as $cat) {
$insert = array(
'supportgroup_id' => $supportgroup_id,
'category_id' => $cat
);
$sc_id = $this->Home_Model->addSupportrCategory($support_category);
}
This will work because the dropdown will always be picked up as an array.
You should also look at adding some validation to this!
Hope this helps!

update checkbox value in CodeIgniter

I have an update query problem in CodeIgniter. I am trying to solve that problem but I can't. I have one array $arrpartnerId=([0=>1,[1]=>4,[3]=>5 like..) and my other array is $promotionData['promotion_id']. The inserting into checkbox value is correct, but the updating checkbox value is not working.
My model function is:
public function update_promotion($promotionData, $partnerData) {
// print_r( $promotionData['promotion_id']);
$arrPartnerId = $partnerData['partner_id'];
print_r($partnerData['partner_id']);
if (is_array($arrPartnerId) > 0) {
foreach ($arrPartnerId as $partnerId) {
$this->db->set('promotion_id', $promotionData['promotion_id']);
$this->db->where('partner_id', $partnerId);
$this->db->update('partner_promotion_relation');
}
}
}
If your array is like this,
$arrPartnerId = array(
0 => 1,
1 => 4,
2 => 5
);
And
$promotionData['promotion_id'] = 123; //assumption
Then try this,
if(sizeof($arrPartnerId) > 0 )
{
foreach( $arrPartnerId as $partnerId)
{
$this->db->set('partner_id', $partnerId );
$this->db->where('promotion_id', $promotionData['promotion_id'] );
$this->db->update('partner_promotion_relation');
}
}
It will resolve the problem.

Unset Not Clearing Array Key/Value

I have form submission that redirects based on the results of a survey. On the landing page, I call a function to process query string, query the database and return results as an array for in-page processing.
function surveyResults() {
if($goodtogo) {
$survey = $wpdb->get_results(...,ARRAY_A);
$name_has_space = strpos(trim($q_name_field[0]),' ');
if($name_has_space === false) {
$q_first_name = $q_name_field[0];
$name_has_num = preg_match('/[0-9]/',$q_first_name);
$q_first_name = ((0 === $name_has_num) ? " ".ucfirst($q_first_name).", " : '');
} else {
$q_first_name = substr(trim($q_name_field[0]),0,$name_has_space);
$name_has_num = preg_match('/[0-9]/',$q_first_name);
$q_first_name = ((0 === $name_has_num) ? " ".ucfirst($q_first_name).", " : '');
}
$survey['name']['q_fname'] = $q_first_name;
$results = $survey;
} else {
$results = false;
}
return $results;
}
Output:
Array (
[0]=> Array (
'key' => 'value'
)
...
[n]=> Array (
'key' => 'value'
)
['name'] => Array (
[q_fname] => MyName
)
)
Which is perfect – except – each time I test the page, the $survey[0-n] results change as queried, but the $survey['name']['q_fname'] still holds the previous value MyName.
I have tried adding unset($survey['name']['q_fname']); immediately after setting $results = $survey; but that doesn't seem to make a difference. Do I need to unset($results) or use a reference &$fname...
What am I missing here?
Thanks
I'm macgregor, and I'm an idiot. Missed a critical piece of condition in the query.

jQuery Auto Complete Return Data - Showing the ID aswell as label

I am using Codeigniter to create an Autocomplete for user names:
Using the parameter : search-user?term=s I get the following data back as Json :
{"id":"1","value":"Stuart Blackett"}{"id":"2","value":"Simon Wilton"}
However when I am running the auto select, On the search for say "St" it brings back by name in the options, But also the ID as well.
How do I prevent this from happening? I suspect my loop could be the issue.
My PHP Function is as follows :
function search_user()
{
$term = $this->input->get('term');
$user_search = $this->vendor_model->search_user($term);
$user['response'] = 'false';
if(count($user_search) > 0)
{
$user['response'] = 'true';
$user = array();
foreach($user_search as $user)
{
$user = array(
'id' => $user->user_id,
'value' => $user->user_firstname .' '. $user->user_surname
);
echo json_encode($user);
}
}
}
{"id":"1","value":"Stuart Blackett"}{"id":"2","value":"Simon Wilton"} isn't valid JSON.
Try not echoing each $user's information separately - instead, build a new array of users and json_encode() that array. Example:
foreach($user_search as $user) {
$users[] = array(
'id' => $user->user_id,
'value' => $user->user_firstname .' '. $user->user_surname
);
}
echo json_encode($users);

Categories