PHP - MYSQL - SQLSTATE[HY093]: Invalid parameter number [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
i have little problem with inserting data to my database.
I get this error but i have no idea way. I have almost identical query on other script and it works fine but this one does not. I check the POST data on chrome inspector it shows fine.
if you have idea whats wrong with it, let me know.
Thanks in Advance.
$fact_total = (float)$_POST['precio'];
$fact_btax = (float)$_POST['precio_sin'];
$fact_tax = (float)$_POST['impuestos'];
$fact_name = e($_POST['fact_name']);
$fact_tipo = e($_POST['fact_serv']);
$fact_tax_rate = 21;
try{
$handler = $db->prepare('INSERT INTO fact_info
(id_client, cl_name, cl_last_name, cl_last_name_2, cl_email, cl_tel, cl_doc_type, cl_doc, cl_via, cl_street, cl_number, cl_level, cl_stairs, cl_door, cl_provincia, cl_city, cl_cod_postal, fact_urgencia, fact_name, fact_tipo, fact_total, fact_btax, fact_tax, fact_tax_rate, created ) VALUES (:id_client, :cl_name, :cl_last_name, :cl_last_name_2, :cl_email, :cl_tel, :cl_doc_type, :cl_doc, :cl_via, :cl_street, :cl_number, :cl_level, :cl_stairs, :cl_door, :cl_provincia, :cl_city, :cl_cod_postal, :fact_urgencia, :fact_name, :fact_tipo, :fact_total, :fact_btax, :fact_tax, :pres_tax_rate, NOW())');
$handler->execute(array(
':id_client' => $client_ids,
':cl_name' => e($_POST['fact_cl_name']),
':cl_last_name' => e($_POST['fact_lastname']),
':cl_last_name_2' => e($_POST['fact_lastname_2']),
':cl_email' => e($_POST['fact_email']),
':cl_tel' => e($_POST['fact_tel']),
':cl_doc_type' => e($_POST['fact_document_type']),
':cl_doc' => e($_POST['fact_document_number']),
':cl_via' => e($_POST['fact_dir_via']),
':cl_street' => e($_POST['fact_dir_calle']),
':cl_number' => (int)$_POST['fact_dir_number'],
':cl_level' => e($_POST['fact_dir_level']),
':cl_stairs' => e($_POST['fact_dir_stairs']),
':cl_door' => e($_POST['fact_dir_door']),
':cl_provincia' => e($_POST['fact_dir_provincia']),
':cl_city' => e($_POST['fact_dir_localidad']),
':cl_cod_postal' => (int)$_POST['fact_dir_cod_postal'],
':fact_urgencia' => '1',
':fact_name' => $fact_name,
':fact_tipo' => $fact_tipo,
':fact_total' => $fact_total,
':fact_btax' => $fact_btax,
':fact_tax' => $fact_tax,
':fact_tax_rate' => $fact_tax_rate
));
$fact_id = $db->lastInsertId();
foreach ($_POST['inv_desc'] as $key => $value) {
$handler4 = $db->prepare('INSERT INTO fact_content (id_fact, fact_desc, fact_qty, fact_price, fact_subtotal) VALUES (:id_fact, :fact_desc, :fact_qty, :fact_price, :fact_subtotal)');
$handler4->execute(array(
':id_fact' => $fact_id,
':fact_desc' => e($_POST['inv_desc'][$key]),
':fact_qty' => (float)$_POST['inv_qty'][$key],
':fact_price' => (float)$_POST['inv_precio'][$key],
':fact_subtotal' => (float)$_POST['inv_subtotal'][$key]
));
}
header('Location: fact_confirm.php?fact_id='.$fact_id.'');
exit();

I'll explain you why this error occurs.
SQLSTATE[HY093]: Invalid parameter number
This error basically occurs because of the following reasons.
Reason1
As the error name suggests, there is some difference between the number of parameters in your prepared query. What I mean is, take a look at the following example:
$s = $conn->prepare("INSERT INTO table(column1,column2) values(:column1)
$s->bindParam(':column1', $column1Value);
$s->bindParam(':column2', $column2Value);
Now, this will generate the error you mentioned because you're trying to insert into 2 columns, but are only providing value for 1.
Reason2
$s = $conn->prepare("INSERT INTO table(column1) values(:column1,:column2)
$s->bindParam(':column1', $column1Value);
$s->bindParam(':column2', $column2Value);
Now this would generate an error because you're trying to insert value into one column, but providing 2 values inside the VALUE section of the query.
Reason3
$s = $conn->prepare("INSERT INTO table(column1,column2) values(:column1,:column2)
$s->bindParam(':column1', $column1Value);
In this case, you've written the query part correct. However, you missed binding the parameters for the second value, the :column2.
These are the reasons why this error bumps up. Check your code and you'd find out what error out of these 3 you've made.

It appears that there is no :fact_tax_rate. Looks like a typo. Your last insert statement column inserts :pres_tax_rate.

Your first insert has the wrong variable name for the second to last value. Change :pres_tax_rate to :fact_tax_rate

Related

PHP write to .ini file

Everyone, hello!
I'm currently trying to write to an .ini file from PHP, and I'm using Teoman Soygul's answer and code from here: How to read and write to an ini file with PHP
This works out great, although, when I save the data to it, it shows up strange in my .ini:
[Server] = ""
p_ip = "192.168.10.100"
p_port = 80
p_password = 1234
[Variable] = ""
string1_find = "Caution"
Most notably it also seems to see attempt to give the categories Server and Variable an empty value. Also, sometimes it saves the variable between consistency and sometimes not. How come there is no consistency here?
The code I'm using to find/post in PHP is this:
...
$a=array("[Server]"=>'',"p_ip"=>$_POST['pip'],"p_port"=>$_POST['pport'], "p_password"=>$_POST['pass'],
"[Variable]"=>'',"string1_find"=>$_POST['string1_find'],
...
If anyone could point me into the right direction, that would really be appreciated. Thank you!
You are not using right, you should be passing a multidimentional array instead:
$data = array(
'Server' => array(
'p_ip' => '192.168.10.100',
'p_port' => 80,
'p_password' => 1234,
),
'Variable' => array(
'string1_find' => 'Caution'
)
);
//now call the ini function from Soygul's answer
write_php_ini($data, 'file.ini');
Here is my output:
[Server]
p_ip = "192.168.10.100"
p_port = 80
p_password = 1234
[Variable]
string1_find = "Caution"
Notice that you need to create an extra array per new section and then you can start listing your custom definitions.

Use JSON in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I use some website that give me information about IP but the information that that website return in JSON and I don't know the JSON. I want to use this to check the user if it is from IR do something but I dont know how to use JSON in php,
Here is the JSON that the website return:
{"address":"0.0.0.0.0","country":"IR","stateprov":"somewhere ","city":"Tehr\somewhere (somewhere)"}
I want to save the country in a variable and add this code to my website:
<?php
if($country == 'IR'){
//Do somethong
}
$country is the country name that return from the website,
You'll need to use json_decode().
$s = '{"address":"0.0.0.0.0","country":"IR","stateprov":"somewhere ","city":"Tehrsomewhere (somewhere)"}';
$d = json_decode($s);
Which returns:
stdClass Object
(
[address] => 0.0.0.0.0
[country] => IR
[stateprov] => somewhere
[city] => Tehrsomewhere (somewhere)
)
That would allow you to check the country/other fields like this:
if($d->country == 'IR') {
// do something
}
NOTE: you had an error (invalid json) in your "city" field, the \ makes it invalid.
Example
You can ensure that your json is valid by checking it at JSON Lint.
I think you are looking for the function json_decode.It decodes the JSON string
See the documentation here
You have to first decode this json string.
$data = '{"address":"0.0.0.0.0","country":"IR","stateprov":"somewhere ","city":"Tehr\somewhere (somewhere)"}';
$decodeData = json_decode($data);
Then use this decode json string in php like this.
if($decodeData->country == 'IR'){
//Do somethong
}
First of all i would like to inform you that given json is not valid. "city" : "Tehr\somewhere (somewhere)" is not valid because of "\".
So change it into below given format.
$jsonEncode = { "address": "0.0.0.0.0","country": "IR","stateprov": "somewhere ","city": "There somewhere (somewhere)"}
$jsonDecode = json_decode($jsonEncode,true);
Now you will get the value in array format.
Array(
[address] => 0.0.0.0.0
[country] => IR
[stateprov] => somewhere
[city] => There somewhere (somewhere)
);
print_r($jsonDecode['city']); will give you city name or details

PHP Array values from file_get_contents [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I declared one variables like this
echo $OPTIONS="500=>250.00, 1000=>500.00,2500=>1100.00,5000=>2250.00";
and
I got this variables through file_get_contents() functions.
$contents = file_get_contents(SERVICE_URL."options_config.php?options=".$OPTIONS);
$package=array($contents)
foreach($package as $pack=>$price)
{
echo $pack;
}
But I got 0 values. What is the problem?
print_r($package);
The result is :
Array ( [0] => 500=>250.00, 1000=>500.00,2500=>1100.00,5000=>2250.00 )
I want the result like this
500 as 250.00
1000 as 500.00
I think what you are looking for is serialize and unserialize
Example: test.php
<?php
// Handle Get Request
// This portion of your code can be on another file
//
if (isset($_GET['getOptions']))
{
$myOptions = array(
500 => 250.00,
1000 => 500.00,
2500 => 1100.00,
5000 => 2250.00
);
exit(serialize($myOptions));
}
// Sample Usage
$options = file_get_contents('http://localhost/test.php?getOptions');
$options = unserialize($options);
// Debug
var_dump($options);
?>
Outputs:

PHP Parse error: syntax error, unexpected ':' line 7 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Please i dont know how to remove this error on line 7 onto this $rest_url = “https://mydomain.com/rest-api/analytics/HTML/”.$login.”/”.$hash.”/”.$timestamp.”.do”;
$timestamp = round(microtime(true) * 1000);
$login = ‘username’;
$password = ‘password’;
$hash = md5(md5($password).$timestamp);
$rest_url = “https://mydomain.com/rest-api/analytics/HTML/”.$login.”/”.$hash.”/”.$timestamp.”.do”;
$post_data = array(
‘range’ => ‘LAST_7_DAYS’,
‘groupBy’ => ‘PLACEMENT’
);
/* Options of HTTP request; http key should be used when posting to https url */
$options = array(
‘http’ => array(
‘header’ => “Content-type: application/x-www-form-urlencoded\r\n”,
‘method’ => ‘POST’,
‘content’ => http_build_query($post_data)
)
);
/* Actual call to REST URL */
$context = stream_context_create($options);
$result = file_get_contents($rest_url, false, $context);
echo($result);
Your string starting and ending characters are incorrect.
You are using ‘ and “ ... replace them with ' and " to make your code valid.
since file_get_contents() is not reliable in remote connections,
You have to use CURL to overcome this http error.
For more info on curl_init read this

Dissapearing PHP Variables

I am creating a 3D Secure PHP Project. I am having a rather bizzare issue in that the "MD" code is going missing when re-submitting the Array of data
My code is as follows :
$paRes = $_REQUEST['PaRes'];
$md = $_REQUEST['MD'];
require "payment_method_3d.php";
x_load('cart','crypt','order','payment','tests');
/*
* For Debugging Purposes
* Only.
echo "The Value Of PaRes is : ";
echo $paRes;
*/
$soapClient = new SoapClient("https://www.secpay.com/java-bin/services/SECCardService?wsdl");
$params = array (
'mid' => '',
'vpn_pswd' => '',
'trans_id' => 'TRAN0095', // Transaction ID MUST match what was sent in payment_cc_new file
'md' => $md,
'paRes' => $paRes,
'options' => ''
);
It seems that the $_REQUEST['MD'] string seems to go missing AFTER the soap call. Although I am having difficulty print this out to the screen. The strange thing is the $paRes variable works without issue.
Any ideas why this would be the case?
Check your case. PHP array keys are case sensitive. From this little bit of code it looks as if the request variable may be 'md' instead of 'MD'.
Try $md = $_REQUEST['md'];
PHP array statements are case sensitive, so this should work:....
$md = $_REQUEST['md'];
Thanks for your responses guys.
What was happening was the include page was sitting in front of the request methods and causing issues loading the REQUEST methods to the page.

Categories