I don't know what have I done wrong. It isn't working.
I'm struck on this so long. I couldn't figure it out.
$sql="INSERT INTO hr_daily_claim(date,
site,
from,
to,
rental,
vehicle,
claim_id,
parking,
beverages,
others)
VALUES(:date,
:site,
:from,
:to,
:rental,
:vehicle,
:claim_id,
:parking,
:beverages,
:others)";
$stmt = $db->prepare($sql);
$stmt->execute(array(
':date' => $date,
':site' => $site,
':from' => $from,
':to' => $to,
':rental' => $rental,
':vehicle' => $vehicle,
':claim_id' => $cliamId,
':parking' => $parking,
':beverages'=> $beverages,
':others' => $others ));
Please someone help me.
It give me no error. But affected rows = 0; not inserting at all.
below is the table structure
`id` int(11) NOT NULL AUTO_INCREMENT,
`claim_id` varchar(45) DEFAULT NULL,
`date` varchar(10) DEFAULT NULL,
`site` varchar(45) DEFAULT NULL,
`from` varchar(45) DEFAULT NULL,
`to` varchar(45) DEFAULT NULL,
`rental` int(11) DEFAULT NULL,
`vehicle` int(11) DEFAULT NULL,
`parking` int(11) DEFAULT NULL,
`beverages` int(11) DEFAULT NULL,
`others` int(11) DEFAULT NULL,
`timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
from, to are reserved words in MySQL. You need to wrap it in backticks.
$sql="INSERT INTO hr_daily_claim(
`date`,
`site`,
`from`, //<-------- This
`to`, //<-------- This
`rental`,
`vehicle`,
`claim_id`,
`parking`,
`beverages`,
`others`
)
Sidenote: There might be a typo in the $cliamId variable in ':claim_id' => $cliamId, which should probably read as ':claim_id' => $claimId,, so do check for that, because those variables are not posted in your question.
If one fails, your entire query will fail. Another thing to note is that $claimId and $claimid are not the same. Variables are case-sensitive.
write from and to words between quotes
`from`
`to`
just like in table structure code
Related
I know this is very silly question. But I am disappointed with the behaviour of this query. I am updating Customers in Opencart. When I have written and executed an Update query, few fields are being inserted and few are not. Especially I need to update 'status' and 'approved' columns. Please check the below Query.
UPDATE oc_customer SET customer_group_id=1,store_id=0,firstname='',lastname='HEATHER HUME',telephone='9876543210',fax='0',password='f53cbb1352950831a84035d320063383f345cfce',salt='rCF2EquoV',status='1',approved='1',date_added='2016-08-31',discount=62.00 WHERE customer_id='1418'
Please let me know what is wrong with this. It is updating Telephone column and not status,approved.
Below is the structure of my table
CREATE TABLE IF NOT EXISTS `oc_customer` (
`customer_id` int(11) NOT NULL,
`customer_group_id` int(11) NOT NULL,
`store_id` int(11) NOT NULL DEFAULT '0',
`firstname` varchar(32) NOT NULL,
`lastname` varchar(32) NOT NULL,
`email` varchar(96) NOT NULL,
`telephone` varchar(32) NOT NULL,
`cellphone` varchar(32) NOT NULL,
`fax` varchar(32) NOT NULL,
`password` varchar(40) NOT NULL,
`salt` varchar(9) NOT NULL,
`cart` text,
`wishlist` text,
`newsletter` tinyint(1) NOT NULL DEFAULT '0',
`address_id` int(11) NOT NULL DEFAULT '0',
`custom_field` text NOT NULL,
`ip` varchar(40) NOT NULL,
`status` tinyint(1) NOT NULL,
`approved` tinyint(1) NOT NULL,
`safe` tinyint(1) NOT NULL,
`token` text NOT NULL,
`date_added` datetime NOT NULL,
`discount` decimal(8,2) NOT NULL DEFAULT '0.00',
`tax_id` varchar(50) NOT NULL,
`subscribe` varchar(5) NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=1419 DEFAULT CHARSET=utf8;
My php code is
$query = "UPDATE oc_customer SET customer_group_id=1,store_id=0,firstname='$first_name',lastname='$last_name',telephone='$phone',fax='$fax',password='$password',salt='$salt',status=".(int)$status.",approved=".(int)$approved.",date_added='$date_added1',discount=$discount WHERE customer_id='$customer_id' ";
mysqli_query($con,$query);
$con is my connection variable.No problem with that.
PHP doesn't always play nice when you try to cast variables in string concatenation. You're also treating an integer as a string for customer_id.
Try this:
$query = "UPDATE oc_customer SET customer_group_id=1,store_id=0,firstname='$first_name',lastname='$last_name',telephone='$phone',fax='$fax',password='$password',salt='$salt',status=".((int)$status).",approved=".((int)$approved).",date_added='$date_added1',discount=$discount WHERE customer_id=$customer_id ";
As a side note, it's very bad security practice to inject variables into an SQL query like that. You should use parameters to avoid SQL injection attacks.
I run your code and found nothing wrong, my row was updated successfully with status and approved both fields.
but I will suggest you not to typecast your variables to integer because when you are typecasting them to integer at the same time you are concatenating them to a string which finally resulting in a string only so no need to typecast only make sure that you provide valid values to those variables through PHP, try following statement and revert if problem solved.
$query = "UPDATE oc_customer SET customer_group_id=1,store_id=0,firstname='$first_name',lastname='$last_name',telephone='$phone',fax='$fax',password='$password',salt='$salt',status=$status,approved=$approved,date_added='$date_added1',discount=$discount WHERE customer_id='$customer_id'";
Thanks for your response. There is no problem with the query. The problem is with special characters which we can't see either in our editors or in PhpMyAdmin. This may be helpful for someone who have stuck with same problem, i.e., Query executes in PhpMyAdmin and not in PHP Script.
Please type the query on your own. Please don't copy and paste it from anywhere. Not atleast from your own page again.
Please type everything on your own because copy paste may again copy the invisible special characters into query which again makes your query tough to debug.
So I'm running a PDO update working, and for some reason it won't update the table...
$business_id = 9874128;
$hidden = 1;
$query = "UPDATE business_property_overrides SET hidden=? WHERE business_id=?";
try {
$stmt = $pdo->prepare($query);
$stmt->execute(array($business_id, $hidden));
}
For some reason this won't update, even though I get no errors. The existing tables schema looks like this, and the data is:
There is an existing data set with business_id = 9874128 and hidden set to 0, but it won't update when I run the above code.
CREATE TABLE `business_property_overrides` (
`business_id` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(512) NOT NULL,
`apt_type` varchar(25) DEFAULT NULL,
`apt_num` varchar(9) DEFAULT NULL,
`street_address` varchar(255) DEFAULT NULL,
`city` varchar(255) DEFAULT NULL,
`state` varchar(255) DEFAULT NULL,
`zip` varchar(25) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`url` varchar(512) DEFAULT NULL,
`hours` varchar(100) DEFAULT NULL,
`openhours` varchar(100) DEFAULT NULL,
`location` point DEFAULT NULL,
`yelp` varchar(512) DEFAULT '0',
`twitter` varchar(512) DEFAULT '0',
`hidden` tinyint(1) DEFAULT '0',
`merged` int(11) DEFAULT NULL,
`closed` tinyint(1) DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `business_id` (`business_id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9874134 DEFAULT CHARSET=utf8;
The hidden is TINYINT 1 characters long, you are assigning it business_id which is 7 characters long, that is the error.
Change
$stmt->execute(array($business_id, $hidden));
To:
$stmt->execute(array($hidden,$business_id))
As I've already commented over here, or you can simply use the placeholders of taking no care about the occurence like as
$query = "UPDATE business_property_overrides SET hidden = :hidden WHERE business_id = :business_id";
try {
$stmt = $pdo->prepare($query);
$stmt->execute(array(":business_id" => $business_id, ":hidden" => $hidden));
}
I am trying to write insert a row into a database with a column that is a composite of other columns to help with searching. Here is my PHP code:
$compose=$house." ".$street." ".$district." ".$posttown." ".$county." ".$postcode;
$sql=sprintf("INSERT INTO Address (House,Street,District,PostTown,County,PostCode,Active,Composite) VALUES ('%s','%s','%s','%s','%s','%s',%s,'%s')",
$house,
$street,
$district,
$posttown,
$county,
$postcode,
$binactive,
$compose);
(I know I should be using prepared statements, that is part of the next refactoring)
The statement executes without errors but the Composite column is being evaluated as a number. That is, if $house starts with a number that number is inserted, if $house is a name then 0 is inserted.
$compose is being concatenated properly.
The column is definitely a varchar. Here is the table create script:
CREATE TABLE `Address` (
`idAddress` int(11) NOT NULL AUTO_INCREMENT,
`House` varchar(45) NOT NULL,
`Street` varchar(45) NOT NULL,
`District` varchar(45) DEFAULT NULL,
`PostTown` varchar(45) NOT NULL,
`County` varchar(45) NOT NULL,
`PostCode` varchar(45) NOT NULL,
`Composite` varchar(1000) NOT NULL,
`Active` binary(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`idAddress`),
UNIQUE KEY `idAddress_UNIQUE` (`idAddress`)
) ENGINE=InnoDB AUTO_INCREMENT=46 DEFAULT CHARSET=latin1;
So...where am I going wrong?
I'm trying to put a bit of dummy data directly into a table I have created:
$sql = "CREATE TABLE customers
(
customer_id int(11) NOT NULL AUTO_INCREMENT,
customer_joindate DATE NOT NULL,
customer_title varchar(8) NOT NULL,
customer_firstname varchar(20) NOT NULL,
customer_surname varchar(20) NOT NULL,
customer_dob DATE,
customer_email varchar(60),
customer_phone varchar(14),
customer_lastupdate TIMESTAMP,
PRIMARY KEY (customer_id)
)TYPE = INNODB;";
I would like the join date to be the current date and I believe the timestamp will handle itself? So I tried the following insert:
mysql_query("INSERT INTO customers (customer_joindate, customer_title, customer_firstname, customer_surname, customer_dob, customer_email, customer_phone)
VALUES (CURDATE(), 'Mr', 'John', 'Smith', '1985-11-01', 'john.smith#gmail.com', ''+4477665434')");
That doesn't seem to be inserting anything, could someone please tell me where I have gone wrong?
Try using 'ENGINE' instead of 'TYPE' :
CREATE TABLE 'customers'
(
'customer_id' int(11) NOT NULL AUTO_INCREMENT,
'customer_joindate' DATE NOT NULL,
'customer_title' varchar(8) NOT NULL,
'customer_firstname' varchar(20) NOT NULL,
'customer_surname' varchar(20) NOT NULL,
'customer_dob' DATE,
'customer_email' varchar(60),
'customer_phone' varchar(14),
'customer_lastupdate' TIMESTAMP,
PRIMARY KEY ('customer_id')
) ENGINE=INNODB;
For more information check the MySql Manual.
As far as I know 'TYPE' is deprecated for newer versions of MySql.
as suggested I used die(mysql_error()) to find the problems:
mysql_query("INSERT INTO customers (customer_joindate, customer_title, customer_firstname, customer_surname, customer_dob, customer_email, customer_phone)
VALUES (CURDATE(), 'Mr', 'John', 'Smith', '1985-11-01', 'john.smith#gmail.com', '+447766543498')")or die(mysql_error()); ;
I'm working on a codeigniter project and I'm trying to troubleshoot a sql issue. I have a query that updates a date field in my table and it's not updating it at all.
I have this table
CREATE TABLE `Customer` (
`customer_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(55) NOT NULL DEFAULT '',
`last_name` varchar(55) NOT NULL DEFAULT '',
`city` varchar(255) DEFAULT '',
`state` char(2) DEFAULT '',
`zip` int(5) DEFAULT NULL,
`title` varchar(255) NOT NULL DEFAULT '',
`image` varchar(255) DEFAULT '',
`blurb` blob,
`end_date` date DEFAULT NULL,
`goal` int(11) DEFAULT NULL,
`paypal_acct_num` int(11) DEFAULT NULL,
`progress_bar` enum('full','half','none') DEFAULT NULL,
`page_views` int(11) NOT NULL DEFAULT '0',
`total_pages` int(11) NOT NULL DEFAULT '0',
`total_conversions` int(11) NOT NULL DEFAULT '0',
`total_given` int(11) NOT NULL DEFAULT '0',
`conversion_percentage` int(11) NOT NULL DEFAULT '0',
`avg_contribution` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`customer_id`)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
and when I run this query to insert data, it runs fine and sets the date to 2012-11-01
INSERT INTO `Customer` (`first_name`, `last_name`, `end_date`) VALUES ('John', 'Smith2', '2012-11-01');
Then I get the customer_id and try to run this query
UPDATE `Customer` SET `end_date` = '2012-14-01' WHERE `customer_id` = '18';
and it sets the date end_date field to 0000-00-00.
Why is it changing the end date to 0000-00-00 rather than 2012-14-01?
2012-14-01 is first day of fourteenth month :)
(so its invalid date, thus casted to 0000-00-00 and Data truncated for column 'end_date' at row 1 warning was returned by mysql, which you can see by querying SHOW WARNINGS to mysql immediately after badly behaving query)
2012-01-14 is 14th of January.
use this:
UPDATE `Customer` SET `end_date` = date('Y-m-d') WHERE `customer_id` = '18';
Use date function to update this field.