PHP, PDO: Can't insert new row with zero values - php

I am trying to get this script to run, but I fail on a very simple stupid thing that has kept me getting further for the past 2 hours.
I have written a PDO statement in a function of a class that inserts a bunch of integers in a mysql db.
If I call on the function from a file using
$result->createResult(0, 0, 0,0,0,0,0,"1234",0);
it does not work.
If I use
$result->createResult(1, 1, 1,1,1,1,1,"1234",1);
it runs just fine!
the function that created the mysql entry is
public function createResult($id, $pp_id, $i_id, $ii_id, $cs_id, $ed_id, $em_id, $l_id, $ud_id){
if ($this->databaseConnection()) {
$query_new_result_insert = $this->db_connection->prepare('INSERT INTO results (id, pp_id, i_id, ii_id, cs_id, ed_id, em_id, l_id, ud_id, created_at) VALUES(:id, :pp_id, :i_id, :ii_id, :cs_id, :ed_id, :em_id, :l_id, :ud_id, now())');
$query_new_result_insert->bindValue(':id', $id, PDO::PARAM_INT);
$query_new_result_insert->bindValue(':pp_id', $pp_id, PDO::PARAM_INT);
$query_new_result_insert->bindValue(':i_id', $i_id, PDO::PARAM_INT);
$query_new_result_insert->bindValue(':ii_id', $ii_id, PDO::PARAM_INT);
$query_new_result_insert->bindValue(':cs_id', $cs_id, PDO::PARAM_INT);
$query_new_result_insert->bindValue(':ed_id', $ed_id, PDO::PARAM_INT);
$query_new_result_insert->bindValue(':em_id', $em_id, PDO::PARAM_INT);
$query_new_result_insert->bindValue(':l_id', $l_id, PDO::PARAM_STR);
$query_new_result_insert->bindValue(':ud_id', $ud_id, PDO::PARAM_INT);
$query_new_result_insert->execute();
}
}
this seems to crash on 0 values. if I check (empty($i_id)) it returns true.
I can't get my head around this! Could anyone be so kind to help?

Ok, I finally figured it out...
It wasn't the table structure, but the empty check...
empty returns true on value 0 as described here

Related

UPDATE statement not working when using binding

I've got an issue regarding my UPDATE statement. When I want to update a column, it just doesn't let me to do that.
I tried to use a binded value for updating the column, I expected for it to change it but it didnt do that.
I wanted it to update the column which was the thing that I expected, but it didnt work.
Here's the code that I am struggling with:
$updatecolor = $conn->prepare("UPDATE avatar SET :part=:color WHERE user_id=:id");
$updatecolor->bindParam(':part', $part, PDO::PARAM_STR);
$updatecolor->bindParam(':color', $color, PDO::PARAM_STR);
$updatecolor->bindParam(':id', $myu->id, PDO::PARAM_INT);
$updatecolor->execute();
You can't bind param the name of a column. Perhaps use if statements to do this correctly.
if($part == "soandso"){
$updatecolor = $conn->prepare("UPDATE avatar SET soandso=:color WHERE
user_id=:id");
$updatecolor->bindParam(':color', $color, PDO::PARAM_STR);
$updatecolor->bindParam(':id', $myu->id, PDO::PARAM_INT);
$updatecolor->execute();
} elseif($part == "soandso2"){
// you get the idea

A variable seems to be uninitialized

I am calling a stored procedure from PHP and it is supposed to give me a date.
global $dbh;
$stmt = $dbh->prepare("CALL date_sp(?)");
$stmt->bindParam($employee_id, $return_value, PDO::PARAM_STR, 4000);
// Call the stored procedure
$stmt->execute();
I keep getting an error that variable $return_value (in the third line) is not initialized.
Be careful here. bindParam is used only for input parameters. Return values are handled with a different method. You should be doing:
$stmt = $dbh->prepare("CALL date_sp(:employee_id)");
$stmt->bindParam('employee_id', $employee_id, PDO::PARAM_STR, 4000);
// call the stored procedure
$stmt->execute();
Whenever writing code of this sort, be sure to check the API reference very carefully. There's often some subtle things you have to get perfectly right or nothing will work.
If you want to bind result values you can use bindColumn but it's often easier to just fetch it as an array and deal with it directly.
You don't actually need a named placed holder. Using the ? you would just do the following.
global $dbh;
$stmt = $dbh->prepare("CALL date_sp(?)");
$stmt->bindParam(1, $employee_id, PDO::PARAM_STR, 4000);
// call the stored procedure
$stmt->execute();
To actually fetch the result you would do something like:
$result = $stmt->fetch()

can't concatenate strings with quotes

Good evening to everyone,
Sorry if my question apperes sily but I'm pazzled by my very trivial problem
In one of the pages of my project I can't concatenate a string containing ' signs
this string can't be concatenated:
$stidR = "INSERT INTO rec_ret_info VALUES('".$rrcode."', ".$modnum.", '".$sdate."', '".$venue."', ".$fac.", ".$date.", ".$sem.")";
but this can:
$stidR = "INSERT INTO rec_ret_info VALUES(".$rrcode.", ".$modnum.", ".$sdate.", ".$venue.", ".$fac.", ".$date.", ".$sem.")";
Apparently if I remove ' signs it works. But i really need them. I really don't know where is the problem. Would be gratefull if you can point me on it.
Can you use a prepared statement to bind a variable?
Connection to Oracle with PDO - More information!
Update
PDO Prepared Statement as an example. The only thing you need to change is the query structure if Oracle is different to MySql in that regard. The binding of variables and the execution will work the same :)
$queryString= "INSERT INTO tablename (ColumnName1,ColumnName2,ColumnName3,ColumnName4,ColumnName5,ColumnName6,ColumnName7) VALUES (?,?,?,?,?,?,?)";
$query = $db->prepare($queryString);
$query->bindValue(1, $variable1, PDO::PARAM_STR);
$query->bindValue(2, $variable2, PDO::PARAM_STR);
$query->bindValue(3, $variable3, PDO::PARAM_STR);
$query->bindValue(4, $variable4, PDO::PARAM_STR);
$query->bindValue(5, $variable5, PDO::PARAM_STR);
$query->bindValue(6, $variable6, PDO::PARAM_STR);
$query->bindValue(7, $variable7, PDO::PARAM_STR);
$query->execute();
simply make echo of your statement like
echo $stidR ; and check the resulting sql, and see what you are doing wrong

mysql query failing in php

I am trying to run the following query in php, but its failing.
$results = $conn->prepare("INSERT INTO book_loans
VALUES (:book_id, :branch_id, :card_no, CURDATE(), DATE_ADD(CURDATE(), INTERVAL 14 DAY))");
//Binding params and executing insert for checking out
$results->bindParam(':book_id', $book_id, PDO::PARAM_STR);
$results->bindParam(':branch_id', $branch_id, PDO::PARAM_INT);
$results->bindParam(':card_no', $card_no, PDO::PARAM_STR);
if($results->execute()) {
return array("success");
} else {
return array("failure");
}
I am returning arrays with strings to check back and display a message.
But even for valid elements, the query is failing.
What is the error message?
Usually the cause is an error in the SQL query! Check that the query actually works in the MySQL console.
If you require more specific help you need post the desription of the book_loans table. Can you post the create table statement for this table?
In stead of returning array ('failure') return array($results->errorInfo()), analyze the error text and then fix it

PDO Multiple Execution Issues

I have two inserts on a processing page. The first works without a hitch. The second will not even activate. I even tried putting a PDO query to see if it would work but still nothing.
$cpinsert = $db->prepare('insert into Chatposts values (0, :chatid, :name, :url, :text, now(), :ipaddress, 0)');
$cpinsert -> bindParam(':chatid', $chatroomid, PDO::PARAM_INT);
$cpinsert -> bindParam(':name', $name, PDO::PARAM_STR);
$cpinsert -> bindParam(':url', $url, PDO::PARAM_STR);
$cpinsert -> bindParam(':text', $text, PDO::PARAM_STR);
$cpinsert -> bindParam(':ipaddress', $ipaddress, PDO::PARAM_STR);
$cpinsert -> execute();
// Needs an error checker
$cpid = $cpinsert ->lastInsertID();
$cpinsert->closeCursor();
^ That works fine, though I don't know about the lastinsertid since I cannot test it.
\V/ Nothing in there will execute no matter what I try. Something is preventing anything there from executing or I didn't close the above's connection properly.
// Targets Insert
//if (isset($target)):
$query = "insert into Targets values (9,'rommel')";
$db->query($query);
$targetinsert = $db->prepare('insert into Targets values (:cpid,:tname)');
foreach ($target as $tname):
$targetinsert -> bindParam(':cpid', $cpid, PDO::PARAM_INT);
$targetinsert -> bindParam(':tname', $tname, PDO::PARAM_STR);
endforeach;
$targetinsert -> execute();
//endif;
I have tried everything I know of and no luck. It is quite possible I did a minor mistake since I am new to PDO. Closecursor didn't seem to do anything either when I added it in.
You said you need to do execute many inserts, but for some reason you are executing your query only once.
Also, please remove these try..catch things from your code - they are the reason why you have no idea what's going wrong
I had to recreate a half functional model of my page but I found the problem, it was lastinsertid(). I was applying a PDOstatement class on a PDO class.
Before:
$cpid = $cpinsert->lastInsertID();
After:
$cpid = $db->lastInsertID();
I just had to call the Database instead of the prepare with that line of code.

Categories