when I try to insert something to my PSQL DB i get this error:
SQLSTATE[42601]: Syntax error: 7 ERROR: INSERT has more expressions
than target columns LINE 1: ...acturer, model, submodel) VALUES($1,
$2, $3, $4, $4, $5, $6) ^
My query is displayed below, to avoid that its somehow data problem I just insert number strings. So this looks really wired for me.
Query:
public function insertToPsql($manufacturer_code, $main_type, $subtype_code, $manufacturer, $model, $submodel)
{
try {
//$con = new PDO( PSQL_DB_HOST, PSQL_DB_HOST, PSQL_DB_HOST );
$con = new PDO("pgsql:dbname=audahistory;host=localhost", "postgres", "");
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "INSERT INTO car_type(manufacturer_code, main_type, subtype_code, manufacturer, model, submodel) VALUES(:manufacturer_code, :main_type, :subtype_code, :manufacturer, :model, :submodel)";
$stmt = $con->prepare( $sql );
$stmt->bindValue( 'manufacturer_code', "1", PDO::PARAM_STR );
$stmt->bindValue( 'main_type', "2", PDO::PARAM_STR );
$stmt->bindValue( 'subtype_code', "3", PDO::PARAM_STR );
$stmt->bindValue( 'manufacturer', "4", PDO::PARAM_STR );
$stmt->bindValue( 'model', "5", PDO::PARAM_STR );
$stmt->bindValue( 'submodel', "6", PDO::PARAM_STR );
$stmt->execute();
}
catch( PDOException $e ) {
echo $e->getMessage();
exit;
}
}
Any idea where cna be a problem?
Thanks for advise
Related
I am using PHP PDO prepared statements. I am passing in a string and returning the record from MYSQL. I am passing three variables to the method.
The query returns nothing. If I perform the same query in phpmyadmin it returns all the correct data. I believe it is the ampersand(&) in the $team variable but, don't know I to work around it. I am not using a link and it is not a form element. It is a straight call to the method.
The values of the three parameters are
$season = '2018-19';
$league = 20;
$team = "Texas A&M University-Kingsville";
Here is my method:
public static function getTeamGames($season, $league, $team){
$conn = parent::connect();
$sql = "SELECT * FROM rfw_games WHERE season = :season &&
league = :league && home = :team";
try {
$st = $conn->prepare( $sql );
$st->bindValue( ":season", $season, PDO::PARAM_STR );
$st->bindValue( ":team", $team, PDO::PARAM_STR );
$st->bindValue( ":league", $league, PDO::PARAM_INT );
$st->execute();
$games = array();
foreach ( $st->fetchAll() as $row ) {
$games[] = new Game( $row );
}
parent::disconnect( $conn);
return $games;
} catch (PDOException $e ) {
parent::disconnect( $conn );
die( "Query failed: " . $e->getMessage() );
}
}
$weeklyGames = Game::getTeamGames( $season, $league, $tName );
I really appreciate the help of everyone.
Thank you in advance.
I was able to fix the issue.
I had to use html_entity_decode on the $team parameter.
I changed my method to the following:
public static function getTeamGames($season, $league, $team){
$dTeam = html_entity_decode($team);
$conn = parent::connect();
$sql = "SELECT * FROM rfw_games WHERE season = :season && league = :league && home = :team";
try {
$st = $conn->prepare( $sql );
$st->bindValue( ":season", $season, PDO::PARAM_STR );
$st->bindValue( ":team", $dTeam, PDO::PARAM_STR );
$st->bindValue( ":league", $league, PDO::PARAM_INT );
$st->execute();
$games = array();
foreach ( $st->fetchAll() as $row ) {
$games[] = new Game( $row );
}
parent::disconnect( $conn);
return $games;
} catch (PDOException $e ) {
parent::disconnect( $conn );
die( "Query failed: " . $e->getMessage() );
}
}
I need help on converting mysql_query to PDO. The MySQL database is not updating when I edit columns. I've tried translating the following code:
<?php
include("connect.php");
if($_GET['id'] and $_GET['data'])
{
$id = $_GET['id'];
$data = $_GET['data'];
$key = $_GET['key'];
if(mysql_query("update information set $key='$data' where id='$id'"))
echo 'success';
}
}
?>
Into this:
<?php
include("connect.php");
if(isset($_GET))
{
$id = $_GET['id'];
$data = $_GET['data'];
$key = $_GET['key'];
}
try {
$pdo = new PDO( DSN, DB_USR, DB_PWD );
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->query( "SET NAMES utf8" );
$stmt = $pdo->prepare(
"UPDATE information
SET
key=:data where id=:id"
);
$stmt->bindValue( ':id', $id, PDO::PARAM_INT );
$stmt->bindValue( ':key', $data, PDO::PARAM_STR );
$stmt->execute();
} catch (PDOException $e){
var_dump($e->getMessage());
}
$pdo = null;
You used :key in your bindValue() call when it should be :data. You also need to put $key into the query (you can't use a placeholder for a column name, so this requires variable substitution).
$stmt = $pdo->prepare(
"UPDATE information
SET
$key = :data where id=:id"
);
$stmt->bindValue( ':id', $id, PDO::PARAM_INT );
$stmt->bindValue( ':data', $data, PDO::PARAM_STR );
$stmt->execute();
You should validate $key before substituting it, to prevent SQL injection. Something like:
$allowed_keys = array('col1', 'col2', 'col3');
if (!in_array($key, $allowed_keys)) {
die("Bad key $key");
}
i fight with DB, trying to insert true and false values to my table with column boolean, but always getting just error:
Invalid parameter number
tested with:
$value = true
$value = "true"
$value = 1
Can somebody please advise me?
Thanks
EDIT:
full code would looks like:
// adding value to variabile
if (empty($row['vin']))
$vin = 0;
else
$vin = 1;
//calling insert method
$this->insertToTable($model_code, $typ, $kind, $ts, $vin, $smr, $ire, $manufacturer_code);
//full insert method:
public function insertToTable($code, $name, $kind, $ts, $vin, $smr, $ire, $manufacturer_code)
{
try {
$con = new PDO( DB_HOST, DB_USER, DB_PASS );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "INSERT INTO r_vehicle_model(code, name, kind, ts, vin, smr, ire, manufacturer_code) VALUES(:code, :name, :kind, :ts, :smr, :ire, :manufacturer_code)";
$stmt = $con->prepare( $sql );
$stmt->bindValue( 'code', $code, PDO::PARAM_STR );
$stmt->bindValue( 'name', $name, PDO::PARAM_STR );
$stmt->bindValue( 'kind', $kind, PDO::PARAM_STR );
$stmt->bindValue( 'ts', $ts, PDO::PARAM_STR );
$stmt->bindValue( 'vin', $vin, PDO::PARAM_STR );
$stmt->bindValue( 'smr', $smr, PDO::PARAM_STR );
$stmt->bindValue( 'ire', $ire, PDO::PARAM_STR );
$stmt->bindValue( 'manufacturer_code', $manufacturer_code, PDO::PARAM_STR );
$stmt->execute();
}
catch( PDOException $e ) {
echo $e->getMessage();
}
}
full error:
SQLSTATE[HY093]: Invalid parameter number: :vin
Forget :vin in insert query. Number of parameter in values is not equal to bindValue
$sql = "INSERT INTO r_vehicle_model(code, name, kind, ts, vin, smr, ire, manufacturer_code) VALUES(:code, :name, :kind, :ts, ,:vin ,:smr, :ire, :manufacturer_code)";
You're currently using PDO::PARAM_STR to specify that all the parameters you're passing are strings.
You should make choose the appropriate type for the field, so for a boolean consider using PDO::PARAM_BOOL
http://php.net/manual/en/pdo.constants.php
i am having some problem with my code, i try to simple insert some logs when user use search on my site.
Log should contains:
date in unix time -> using function for it
username -> its stored in Session
vin -> its string he post in input field
Top of the class
public $vin = null;
public function __construct( $data = array() )
{
if( isset( $data['vin'] ) )
$this->vin = stripslashes( strip_tags( $data['vin'] ) );
}
public function storeFormValues( $params )
{
$this->__construct( $params );
}
function code:
public function logFromSearch() {
$correct = false;
//$this->username = $_SESSION]['username'];
$date = new DateTime();
try {
$con = new PDO( DB_HOST, DB_USER, DB_PASS );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "INSERT INTO logs(date, user, vin) VALUES(:date, :user, :vin)";
$stmt = $con->prepare( $sql );
$stmt->bindValue( "date", $date->getTimestamp(), PDO::PARAM_STR );
$stmt->bindValue( "user", $this->username, PDO::PARAM_STR );
$stmt->bindValue( "vin", $this->vin, PDO::PARAM_STR );
echo $this->vin;
echo $date->getTimestamp();
$stmt->execute();
return "Everything is OKEY";
} catch( PDOException $e ) {
return $e->getMessage();
}
}
Its not importing anything to database dispite the fact it should in my opinion. I try to debug it so i found out my way of adding username from session is not possible way and also find out that this->vin and $date->getTimestamp() is returning actual data which it should insert to database so can somebody help me to find where is the problem? (in same class i am using other function for database connection and they work find so its not in setting up connecting / db name and host and so on because its in different file)
P.S. Errors : I dont get any errors just single notice Notice: Undefined property: Data::$username which is understandable.
Stuck again with object/classes. I have a function called submitticket which will submit a new row into my ticket table.
public function submitticket() {
$correct = false;
try {
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "INSERT INTO tickets (ticketid, EPMSAccount, requester, recipient, jobnumber, subject, body, responseid, type, priority) VALUES (:ticketid, :EPMSAccount, :requester, :recipient, :jobnumber, :subject, :body, :responseid, :type, :priority)";
$stmt = $con->prepare( $sql );
//print_r($sql);
$stmt->bindValue( "ticketid", $this->ticketid, PDO::PARAM_STR );
$stmt->bindValue( "EPMSAccount", $this->EPMSAccount, PDO::PARAM_STR );
$stmt->bindValue( "requester", $this->requester, PDO::PARAM_STR );
$stmt->bindValue( "recipient", $this->recipient, PDO::PARAM_STR );
$stmt->bindValue( "jobnumber", $this->jobnumber, PDO::PARAM_STR );
$stmt->bindValue( "subject", $this->subject, PDO::PARAM_STR );
$stmt->bindValue( "body", $this->body, PDO::PARAM_STR );
$stmt->bindValue( "responseid", $this->responseid, PDO::PARAM_STR );
$stmt->bindValue( "type", $this->type, PDO::PARAM_STR );
$stmt->bindValue( "priority", $this->priority, PDO::PARAM_STR );
echo '<pre>';
print_r($stmt);
echo '</pre>';
$stmt->execute();
return "Ticket created!";
}catch( PDOException $e ) {
return $e->getMessage();
}
This is how I'm executing it in my code:
// Submit ticket if submit clicked
if($_POST['submit'] == 'Submit Ticket'){
$_POST['requester'] = $_SESSION['name'];
$_POST['EPMSAccount'] = $_SESSION['EPMSAccount'];
$tkt = new Ticket; // New instance of ticket class
$tkt->storeFormValues( $_POST );
$tkt->storeFormValues( $_SESSION );
$tkt->submitticket();
echo '<pre>';
var_dump($tkt);
print_r ($stmt);
echo '</pre>';
} else {
echo 'nothing attempted';
}
I know this query should be correct, but its not writing anything to my table. Here is what the var_dump and print_r is giving me:
PDOStatement Object
(
[queryString] => INSERT INTO tickets (ticketid, EPMSAccount, requester, recipient, jobnumber, subject, body, responseid, type, priority) VALUES (:ticketid, :EPMSAccount, :requester, :recipient, :jobnumber, :subject, :body, :responseid, :type, :priority)
)
object(Ticket)#2 (12) {
["ticketid"]=>
NULL
["EPMSAccount"]=>
string(7) "SHAWMUT"
["jobnumber"]=>
string(6) "123456"
["estnumber"]=>
NULL
["requester"]=>
string(14) "Gabriel Peluso"
["recipient"]=>
string(16) "Customer Service"
["subject"]=>
string(4) "2323"
["body"]=>
string(5) "23232"
["order"]=>
NULL
["responseid"]=>
NULL
["type"]=>
NULL
["priority"]=>
string(6) "Normal"
}
I can't figure out how to see the actual SQL query with the binded PDO values. Is it possible to product that? Any way I can find out why its not writing to the table?
Check that $stmt is not false when set, also that $stmt->execute(); does not return false.
If they do then use the errorInfo() method available for both PDO connections and statements to debug.
You are not actually doing anything with the return value (the success message or the exception message).
To see what is happening, you could change:
$tkt->submitticket();
to:
echo $tkt->submitticket();
Apart from that, $stmt is a local variable in your function, so if you want to see its contents, you need to var_dump it in the function itself.