PDOException: Query was empty [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 a PDO PHP file making use of just one $_POST value stored on the $data array, and if an statement is true, a second value is added to that array to make a new query with two values:
<?php
session_start();
include("../conexionbbdd.php");
if($_SESSION['estado'] == 'activo' && $_SESSION['rol'] == '1'){
$data = array(
'us_id' => $_POST['us_id'],
);
$selectUsers= "SELECT * FROM ws_users WHERE us_id= :us_id";
$statementSelectUsers = $pdo->prepare($selectUsers);
$statementSelectUsers->execute($data);
$result = $statementSelectUsers->fetch(PDO::FETCH_ASSOC);
$us_fk_ui_id = $result['us_fk_ui_id'];
if($us_fk_ui_id==='1'){
$data['us_credits']=$_POST['us_credits'];
$updateUser = mysqli_query($con,"UPDATE ws_users SET us_credits = :us_credits, us_access = '1' WHERE us_id = :us_id");
$statementUpdateUser = $pdo->prepare($updateUser);
$statementUpdateUser->execute($data);
}
Everything goes fine untill the $statementUpdateUser->execute($data); line (34), where I get the usual error
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1065
Query was empty in C:\wamp\www**********\actions\ad_updateUserInfo.php on
line 34
As far as I've seen, this should be due to the unexistance of one of the placeholders on the array, but if I print the array values after the $data['us_credits']=$_POST['us_credits']; it seems to be correct, having the 2 expected values needed for my query:
Array (
[0] => 2
[1] => 1.5 )
How could I check where the mistake is? There's no possibility of echoing the query as it is an object unable to transform on string.

$updateUser = mysqli_query($con,"UPDATE ws_users SET us_credits = :us_credits, us_access = '1' WHERE us_id = :us_id");
^^^ WTF??
You have to pay more attention to the code you write. Stack Overflow is NOT the service for finding typos for you.

Related

I can't insert data from php to mysql [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 2 years ago.
Improve this question
In my Mysql I have this table. And I want to send data
Id is autoincrement
Id
insertId
invoceTaxApplayId
sumOfDist
if (isset($_POST['basic'])) {
$user_string = $_POST['basic'];
$basic = json_decode($user_string);
foreach ($basic as $key => $value){
$sql2 = "INSERT INTO `insert_tax_applay_map`( `insertId`, `invoceTaxApplayId`, `sumOfDist`) VALUES ('$value','', 5)";
echo $sql2; //printed
echo $key;
}
exit();
}
I can see echos, but data isn't sent to mysql.
You can fix the issue of not executing and your serious SQL injection bug with one simple trick: Prepared statements with placeholder values!
if (isset($_POST['basic'])) {
$user_string = $_POST['basic'];
$basic = json_decode($user_string);
// Prepare your database query with placeholder values
$stmt = $db->prepare("INSERT INTO insert_tax_applay_map (insertId, invoceTaxApplayId, sumOfDist) VALUES (:insertId, :invoiceTaxApplayId, :sumOfDist)");
// For each entry...
foreach ($basic as $key => $value) {
// ...execute the statement with that particular set of values.
$stmt-execute([
'insertId' => $value,
'invoiceTaxApplayId' => '',
'sumOfDist' => 5
]);
}
exit();
}
This example uses PDO but can easily be adapted to mysqli or whatever you're using.
Tip: For general guidance on PHP, see PHP the Right Way for more resources.

Fatal error: Call to a member function bindParam() on boolean in D:\xampp\htdocs\ipack\insertstatus.php on line 14 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
I am getting an error while running this code:
Fatal error: Call to a member function bindParam() on boolean in D:\xampp\htdocs\ipack\insertstatus.php on line 9
<?php
header('Access-Control-Allow-Origin: *');
include 'dbconnection.php';
$jobno = "AFE/0001/2015";
$jobseq = 0;
//to get INTJOBNO
$intjobno = "";
$data = $dbh->query("select INTJOBNO from PRTJOBHD where JOBNO = :jobno and JOBSEQ = :jobseq");
$data->bindParam(':jobno',$jobno,PDO::PARAM_STR);
$data->bindParam(':jobseq',$jobseq,PDO::PARAM_STR);
$data->execute();
foreach($data as $row) {
$intjobno = $row['INTJOBNO'];
echo $intjobno;
}
>
Have a look at this answer: PDO's query vs execute. You cannot bind parameters to PDO query, you need to use prepare instead.
header('Access-Control-Allow-Origin: *');
include 'dbconnection.php';
$jobno = "AFE/0001/2015";
$jobseq = 0;
//to get INTJOBNO
$intjobno = "";
$data = $dbh->prepare("select INTJOBNO from PRTJOBHD where JOBNO = :jobno and JOBSEQ = :jobseq");
$data->bindParam(':jobno',$jobno,PDO::PARAM_STR);
$data->bindParam(':jobseq',$jobseq,PDO::PARAM_STR);
$data->execute();
foreach($data as $row) {
$intjobno = $row['INTJOBNO'];
echo $intjobno;
}
PDO::query() returns a PDOStatement object, or FALSE on failure.
Source
It means your query has failed for some reason.
In this case you are using the wrong function to do what you want to do.
You need to prepare your statement since you want to bind two parameters in your query.
Use $dbh->prepare() instead of $dbh->query().

Array to string conversion [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
There is an error in my code but the code works perfectly. I mean all the values are inserted in the database but there is an error like this on the screen:
Severity: Notice
Message: Array to string conversion
Filename: models/some_model.php
Line Number: 106
This is my code:
View:
<?php foreach($app as $row){
echo "<tr><td><input type=checkbox name=appname[] value='".$row->app_name."'/>".$row->id."</td><td>".$row->app_name."</td><tr>".
?>
Controller:
public function hide(){
$this->load->model('some_model');
$visi = $this->input->post('appname');
$success = $this->some_model->hideApp($visi);
foreach($visi as $key=>$value)
{
$success = $this->some_model->hideApp($visi[$key]);
}
if($success == TRUE)
$this->hideApp_page(TRUE);
else $this->hideApp_page(FALSE);
}
Model:
public function hideApp($visi){
$visi = $this->db->escape_str($visi);
$queryStr = "UPDATE appwarehouse.application_table SET visibility='hidden' where app_name='$visi';"; /* this is line 106*/
$query = $this->db->query($queryStr);
return $query;
}
$visi is array like [1,2,3,4]
when you put $visi in hideApp()
it will be display "array to string error"
so maybe you can remove this line $success = $this->some_model->hideApp($visi);
you have already do some_model->hideApp($visi[$key]) in the foreach loop
so i don't know why you write this$success = $this->some_model->hideApp($visi);
if you still want to run $success = $this->some_model->hideApp($visi);
you have to put $visi into a string

Can't seem to figure out this php error [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
I've been trying to figure out this PHP error for almost a day. Sigh. I'm really new to coding but I thought I had this figured out. When I run this code, I get an error
Parse error: syntax error, unexpected 'body' (T_STRING), expecting ')' in /Users/baronjon/Sites/squaredb/functions.php on line 20
and here is the code:
<?php
function add_post($userid, $body) {
$sql = "insert into posts (user_id, body, stamp)
values ($userid, '". mysql_real_escape_string($body)."', now())";
$result = mysql_query($sql);
}
function show_posts($userid) {
$posts = array();
$sql = "select body, stamp from posts
where user_id ='$userid' order by stamp desc";
$result = mysql_query($sql);
while($data = mysql_fetch_object($result)) {
$posts[] = array( 'stamp' => $data->stamp,
'userid => $userid
'body' => $data->body //**This is line 20.**
);
}
return $posts;
}
?>
Being new to php (and coding in general) I realize that this could be really simple. But I don't see the parenthesis that it's telling me to look for.
I looked in the php manual to make sure I had the syntax correct and it seem pretty straightforward.
Can anyone point me in the right direction.
In your $posts array you are missing a trailing ' quote, and a comma.
$posts array should look like:
$posts[] = array(
'stamp' => $data->stamp,
'userid' => $userid,
'body' => $data->body //**This is line 20.**
);
Many times in PHP, when it says line 20, you should be looking at line 19.
As a commenter above mentioned I would really recommend using a code editor with syntax highlighting like vim, emacs, Sublime Text, Notepad++ or something similar. Saves my butt every day :).
You're missing an end quote on line 19 around 'userid => $userid and a comma. It should be 'userid' => $userid,.

PDOException error when running SQL Query in PDO [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 9 years ago.
Improve this question
I am trying to run this SQL Query using PHP PDO:
$stmt = $pdo_conn->prepare("select * from billing_pdf_archive where invoice_number = :invoice_number and sequence = :sequence ");
$stmt->execute(array(
':invoicenumber' => $_GET["inv"],
':sequence' => $_GET["seq"]
)
);
$result = $stmt->fetch();
Note: $_GET["inv"] and $_GET["seq"] show data when echoed
but i am getting this error
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' in /home/integra/public_html/lifeline/billing/resendpdfinvoice.php:94 Stack trace: #0 /home/integra/public_html/lifeline/billing/resendpdfinvoice.php(94): PDOStatement->execute(Array) #1 {main} thrown in /home/integra/public_html/lifeline/billing/resendpdfinvoice.php on line 94
i cannot work out what is wrong with it
where invoice_number = :invoice_number
^---- underscore here
$stmt->execute(array(':invoicenumber' => $_GET["inv"],
^---no underscore here
See here
invoicenumber!=invoice_number
It appears that your query contains :invoice_number when your execution statement asks for :invoicenumber. Try setting them to the same value (:invoice_number for example)
$stmt = $pdo_conn->prepare("select * from billing_pdf_archive"
. " where invoice_number = :invoice_number and sequence = :sequence ");
$stmt->execute(array(
':invoice_number' => $_GET["inv"],
':sequence' => $_GET["seq"]
));
$result = $stmt->fetch();

Categories