I don't understand this error
Erreur de syntaxe près de ' , '196,000,000', '6357007', '6357006', '',
'mr. hasan', '', '', '' à la ligne 2
Here is my table create:
CREATE TABLE `comp` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`COMPANY_NAME` varchar(500) NOT NULL,
`ACTIVITY` text NOT NULL,
`CITY` varchar(150) NOT NULL,
`NUM_COMPANY_FOLLOW` int(11) DEFAULT NULL,
`NUM_BRANCH` int(11) DEFAULT NULL,
`ASSETS` text,
`PHONE1` varchar(100) NOT NULL,
`PHONE2` varchar(100) DEFAULT NULL,
`E_MAIL` varchar(250) DEFAULT NULL,
`DIRECTOR_NAME` varchar(500) NOT NULL,
`COMPANY_SITE` varchar(250) DEFAULT NULL,
`COMPANY_ADDRESS` text,
`HEAD_LOCATION` varchar(100) DEFAULT NULL,
`HEAD_DIRECTORS_NAME` text NOT NULL,
`VICE_HEAD_NAME` varchar(500) NOT NULL,
`BOARD_MEMBER` text,
`BRIEF_DESC` text,
`EVALUE` varchar(250) DEFAULT NULL,
`NOTES` text,
`USERID_ADD` int(11) DEFAULT NULL,
`IP_ADD` varchar(15) DEFAULT NULL,
`DATE_ADD` int(11) DEFAULT NULL,
`USERID_EDIT` int(11) DEFAULT NULL,
`IP_EDIT` varchar(15) DEFAULT NULL,
`DATE_EDIT` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=cp1256
Here is my code:
$sql = "INSERT INTO comp (ID, COMPANY_NAME, ACTIVITY, CITY, NUM_COMPANY_FOLLOW, NUM_BRANCH, ASSETS, PHONE1, PHONE2, E_MAIL, DIRECTOR_NAME, COMPANY_SITE, COMPANY_ADDRESS, HEAD_LOCATION, HEAD_DIRECTORS_NAME, VICE_HEAD_NAME,BOARD_MEMBER, BRIEF_DESC, EVALUE, NOTES, USERID_ADD, IP_ADD, DATE_ADD)
VALUES (NULL, '$COMPANY_NAME', '$ACTIVITY', '$CITY', $NUM_COMPANY_FOLLOW, $NUM_BRANCH, '$ASSETS', '$PHONE1', '$PHONE2',
'$E_MAIL', '$DIRECTOR_NAME', '$COMPANY_SITE', '$COMPANY_ADDRESS', '$HEAD_LOCATION', '$HEAD_DIRECTORS_NAME', '$VICE_HEAD_NAME',
'$ALL_BOARD_MEMBER', '$BRIEF_DESC', '$EVALUE', '$NOTES', $this_userID, '$IP_ADD', $DATE_ADD)";
$result = $db->Execute($sql);
echo mysql_error();
print sql :
INSERT INTO comp
(
ID,
COMPANY_NAME,
ACTIVITY,
CITY,
NUM_COMPANY_FOLLOW,
NUM_BRANCH,
ASSETS,
PHONE1,
PHONE2,
E_MAIL,
DIRECTOR_NAME,
COMPANY_SITE,
COMPANY_ADDRESS,
HEAD_LOCATION,
HEAD_DIRECTORS_NAME,
VICE_HEAD_NAME,
BOARD_MEMBER,
BRIEF_DESC,
EVALUE,
NOTES,
USERID_ADD,
IP_ADD,
DATE_ADD
)
VALUES
(
NULL,
'ANAAM',
'hello1',
'jeddah',
,
,
'',
'6357007',
'6357006',
'',
'mr mohammed',
'',
'',
'',
'mr adnan',
'mr naser',
'',
'',
'',
'',
1,
'127.0.0.1',
1415180296
)
If you look at the value part of your query:
(NULL, 'ANAAM', 'hello1', 'jeddah', , , '', '6357007', '6357006', '', 'mr mohammed', '', '', '', 'mr adnan', 'mr naser', '', '', '', '', 1, '127.0.0.1', 1415180296)
You'll see , , ,, that creates an error as the data is empty. So put brackets around the numeric values like with your ascii values. Or use a default value (0?).
Also, remove the ID value in your sql-query, it's auto-increment.
Editted code:
$sql = "INSERT INTO comp (COMPANY_NAME, ACTIVITY, CITY, NUM_COMPANY_FOLLOW, NUM_BRANCH, ASSETS, PHONE1, PHONE2, E_MAIL, DIRECTOR_NAME, COMPANY_SITE, COMPANY_ADDRESS, HEAD_LOCATION, HEAD_DIRECTORS_NAME, VICE_HEAD_NAME,BOARD_MEMBER, BRIEF_DESC, EVALUE, NOTES, USERID_ADD, IP_ADD, DATE_ADD)
VALUES ('$COMPANY_NAME', '$ACTIVITY', '$CITY', '$NUM_COMPANY_FOLLOW', '$NUM_BRANCH', '$ASSETS', '$PHONE1', '$PHONE2',
'$E_MAIL', '$DIRECTOR_NAME', '$COMPANY_SITE', '$COMPANY_ADDRESS', '$HEAD_LOCATION', '$HEAD_DIRECTORS_NAME', '$VICE_HEAD_NAME',
'$ALL_BOARD_MEMBER', '$BRIEF_DESC', '$EVALUE', '$NOTES', '$this_userID', '$IP_ADD', '$DATE_ADD')";
$result = $db->Execute($sql);
echo mysql_error();
Looks data-dependent and the best way to fix our code is to rewrite the statements using dprepared statements. This would also avoid the vulnerability to SQL-Injections. Have a look at the answer to this question: How can I prevent SQL injection in PHP?
This means that you have an error in your syntax near ' , '196,000,000', '6357007', '6357006', '', 'mr. hasan', '', '', '' at line 2. In other words, it's a SQL error
This is because of the brief description in your query. The value of the variable is most certainly containing bad characters. Make sure you escape the string, if you have things like apostrophes or stuff like that...
Hope this helps! :D
We need definitely more code, like the echo of $sql.
But i've seen an additional error in the statement.
You define the ID as NOT NULL, auto increment and primary key. In the query, you set NULL for id. That will return an error.
Remove the column "ID" from the statement.
Related
There I have some multiple query and want to execute in Codeigniter.
Below are the code I tried.
function seememberfilter($course,$n)
{
$this->load->database();
$sql = array();
$sql[] = "CREATE TEMPORARY TABLE temp1";
$sql[] = "select id,rand, name,lname, email,phone from `member` where course like ('%$course%')";
$sql[] = "CREATE TEMPORARY table temp2";
$sql[] = "select distinct t.id,t.rand, t.name,t.lname, t.email,t.phone, a.quest_id from temp1 t left join assignment a on t.rand= a.rand";
$sql[] = "CREATE TEMPORARY table randid";
$sql[] = "select rand, quest_id from temp2 where quest_id=$n";
$sql[] = "select t.id,t.rand, t.name,t.lname, t.email,t.phone, r.quest_id from temp1 t left join randid r on t.rand= r.rand";
foreach ($sql as $sql_command)
{
if ($debugging == 0)
{
$this->db->query($sql_command);
}
elseif ($debugging == 1)
{
echo $sql_command;
}
}
return $query->result();
}
Above code is in model and below code is in Controller
public function assign(){
$this->load->model("user_model");
$course=$this->input->post('course');
$n=$this->input->post('id');
$data['sel'] = $this->user_model->seememberfilter($course,$n);
$this->load->view('assign',$data);
}
mysql query are executing well in phpmyadmin. I checked it
What I want: I want to execute mysql query in CI successfully .
I am editing this and giving you sql table complete details
DROP TABLE IF EXISTS `member`;
CREATE TABLE IF NOT EXISTS `member` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`rand` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`lname` varchar(255) NOT NULL,
`phone` varchar(255) NOT NULL,
`dob` varchar(255) NOT NULL,
`course` text NOT NULL,
`gender` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
`state` varchar(255) NOT NULL,
`zip` varchar(255) NOT NULL,
`comment` text NOT NULL,
`Aboutme` text NOT NULL,
`dat` varchar(255) NOT NULL,
`tim` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
INSERT INTO `member` (`id`, `rand`, `email`, `name`, `lname`, `phone`, `dob`, `course`, `gender`, `address`, `city`, `state`, `zip`, `comment`, `Aboutme`, `dat`, `tim`) VALUES
(1, 'jrf20180961828', 'kk122344545#hotmail.com', 'Kishan', 'Yadav', '9717111111', '2018-09-01', 'paper first,Political Science,Philosophy,', 'male', 'Diamond Auto Mobiles', 'Belthara road', 'ballia', '221715', 'Good person.', '', '06-09-2018', '11:24:16pm'),
(2, 'jrf20180914721', 'kk1#gmail.com', 'Rohan', 'Yadav', '9717111111', '2018-09-01', 'paper first,Philosophy,History,', 'male', 'Diamond Auto Mobiles', 'Belthara road', 'ballia', '221715', 'Good Person.', '', '06-09-2018', '11:25:44pm'),
(5, 'jrf20180958284', 'ykishan94612121#gmail.com', 'kiran', 'Singh', '9717111111', '2018-07-07', 'paper first,Political Science,History,', 'female', 'Diamond Auto Mobiles', 'Belthara road', 'ballia', '221715', 'nnbmnm', '', '10-09-2018', '10:11:34pm'),
(4, 'jrf20180932851', 'kk12#gmail.com', 'Pankaj', 'Yadav', '9717111111', '2018-09-01', 'paper first,Philosophy,Psychology,History,', 'male', 'Diamond Auto Mobiles', 'Belthara road', 'ballia', '221715', 'nmbmbn', '', '10-09-2018', '10:10:19pm'),
(6, 'jrf20180929250', 'hjh#gmail.com', 'John', 'Corter', '9717111111', '2018-09-06', 'paper first,History,', 'male', 'Diamond Auto Mobiles', 'Belthara road', 'ballia', '221715', 'sxs', '', '11-09-2018', '12:09:49am');
COMMIT;
And second table is
DROP TABLE IF EXISTS `assignment`;
CREATE TABLE IF NOT EXISTS `assignment` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`rand` varchar(255) NOT NULL,
`course` varchar(255) NOT NULL,
`quest_id` varchar(255) NOT NULL,
`time` varchar(255) NOT NULL,
`date` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
INSERT INTO `assignment` (`id`, `rand`, `course`, `quest_id`, `time`, `date`) VALUES
(1, 'jrf20180914721', 'paper first', '2', '10:05:48pm', '2018-09-10'),
(2, 'jrf20180961828', 'paper first', '2', '10:06:49pm', '10-09-2018'),
(3, 'jrf20180914721', 'History', '2', '10:08:03pm', '10-09-2018'),
(4, 'jrf20180958284', 'History', '2', '10:12:05pm', '10-09-2018'),
(5, 'jrf20180914721', 'paper first', '1', '10:19:23pm', '10-09-2018'),
(6, 'jrf20180932851', 'History', '3', '12:07:48am', '11-09-2018');
COMMIT;
And my output should be kind of this, I join two table to get this output.
Attached pic of my output
I get this output using above query. Sorry for my english.
After chat in db-fiddle:
select distinct t.id, t.rand, name, lname, email, phone, if(quest_id='2','2',NULL) as quest-id
from `member` t
left join assignment a on t.rand= a.rand
where t.course like '%History%'
and (a.course like '%History%') or a.id is null
Member constrained to course, assignment same course, quest(ion)_id 2 displayed if it was found, otherwise leave the field as NULL.
You can use transaction to separate Query execution
$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');
$this->db->trans_complete();
I'm trying to import data from a csv file into a blank database. I'm using a php script for that.
I'm not concerned about the security of the query at the moment, because I'm the only one using the script. I just created a small testing script for myself.
The csv file has duplicated items.
It won't insert anything, and the error log won't tell me anything.
This is my query:
if ($data[16] != NULL) {
$cursosViejos = ', '.$data[16];
} else {
$cursosViejos = '';
}
$x = $data[3];
$importar = $conectar->prepare('
INSERT INTO usuarios (
deprecated_userID,
deprecated_userName,
userEmail,
deprecated_userCursos,
deprecated_userRoles,
userApellido,
userDNI,
userDomicilioCalleyNumero,
userDomicilioLocalidad,
userEstadoCivil,
userEstudios,
userProfesion,
userTelefono,
deprecated_cuestionarioFinal,
userAnoticiado,
deprecated_entregaDoc
)
VALUES (
?,
?,
?,
concat(?,?),
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
)
ON DUPLICATE KEY UPDATE
');
$importar->bindParam(1, $data[0]);
$importar->bindParam(2, $data[1]);
$importar->bindParam(3, $data[2]);
$importar->bindParam(4, $x);
$importar->bindParam(5, $cursosViejos);
$importar->bindParam(6, $data[4]);
$importar->bindParam(7, $data[5]);
$importar->bindParam(8, $data[6]);
$importar->bindParam(9, $data[7]);
$importar->bindParam(10, $data[8]);
$importar->bindParam(11, $data[9]);
$importar->bindParam(12, $data[10]);
$importar->bindParam(13, $data[11]);
$importar->bindParam(14, $data[12]);
$importar->bindParam(15, $data[13]);
$importar->bindParam(16, $data[15]);
$importar->bindParam(17, $data[17]);
$ok = $importar->execute();
This is the table that will receive the data:
CREATE TABLE usuarios(
userID int unsigned not null auto_increment primary key,
userEmail char(50) null,
userApellido char(50) null,
userNombres char(20) null,
userDNI char(15) null,
userPass char(65) null,
userFechaGeneracion char(25) null,
userLastLogin char(25) NULL,
userTelefono char(200) null,
userCelular char(200) null,
userDomicilioCalleyNumero char(100) null,
userDomicilioLocalidad char(80) null,
userDomicilioProvincia char(80) null,
userProfesion char(180) null,
userEstadoCivil char(80) null,
userEstudios char(80) null,
nacionalidad char(80) null,
userAnoticiado char(80) null,
docFoto char(100) null,
docDNI char(100) null,
docCertif char(100) null,
docVerificada int(1) not null DEFAULT '0',
comentariosAdmin text null,
deprecated_userName char(50) null UNIQUE,
deprecated_userID int(6) null,
deprecated_userStatus int(6) null,
deprecated_userNode int(6) null,
deprecated_userRoles char(150) null,
deprecated_userCursos varchar(7000) null,
deprecated_entregaDoc char(150) null,
deprecated_cuestionarioFinal char(150) null,
userDatosPublicos text null,
userTelsPublicos varchar(200) null DEFAULT NULL,
UNIQUE(userEmail)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Is there any syntax error that I'm not seeing? Because other imports that I've done with the same script did worked.
Here's some sample data from the csv file:
"27","casia","mrcasia#yahoo.com.ar","","alumno_presencial","CASIA,
María Rosa","","","","","","","","","","","",""
"28","alefrei","dralejandra#gmail.com","Curso Interdisciplinario On
Line de Administración de Consorcios","alumno_online,
online1","","","","","","","","","","","","","" "57","Arq. Emilio
Gomez","eaos#yahoo.com","","autor","Arq. Emilio Fernando
Gómez","","","","","","","","","","","",""
"27","casia","mrcasia#yahoo.com.ar","","alumno_presencial","CASIA,
María Rosa","","","","","","","","","","","",""
"27","casia","mrcasia#yahoo.com.ar","","alumno_presencial","CASIA,
María Rosa","","","","","","","","","","","",""
UPDATE:
I've put the code in a try/catch and I have this error shown:
SQLSTATE[42000]: Syntax error or access violation: 1064
THe CONCAT statement you use to build your deprecated_userCursos column is not using any quotes for the strings. As you've parameterized the rest of the query, why not parameterize that? e.g. concat(?,?), and then
$importar->bindParam(4, $x);
$importar->bindParam(5, $cursosViejos);
//adjust the other param indices which follow...
I've found the answer. Just in case it help other, I'll post it here.
The problem is with the syntax, in the last ON UPDATE part.
It's incomplete:
ON DUPLICATE KEY UPDATE
When it should have exactly how to do the update. The statement is incomplete, because the UPDATE part is not there. I should tell mySQL how to make the update.
Here's more information on the subject.
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
This is my table:
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(20) NOT NULL default '',
`pass` varchar(32) NOT NULL default '',
`lang` varchar(2) default NULL,
`locale` varchar(2) default NULL,
`pic` varchar(255) default NULL,
`sex` char(1) default NULL,
`birthday` date default NULL,
`mail` varchar(64) default NULL,
`created` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `mail` (`mail`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=27 ;
And this is my query:
$query = "INSERT IGNORE INTO `users` (`name`, `mail`, `birthday`, `lang`, `locale`, `sex`, `pic`) VALUES ('".$name."', '".$email."', '".date_format($birthdaynew, 'Y-m-d H:i:s')."', '".substr($locale, 0, 2)."', '".substr($locale, -2, 2)."', '".$sex."', 'pic/".$uid.".jpg')";
$rows = mysql_query($query) or die("Failed: " . mysql_error());
$_SESSION['id'] = mysql_insert_id(); // I have tryed also mysql_insert_id($db_con) where $db_con is the link to db.
$_SESSION['name'] = $name;
$_SESSION['name'] contains correctly the name but $_SESSION['id'] contains 0.
Why ?
I'm going crazy!
Is there a particular reason why you are using INSERT IGNORE?
If you use INSERT IGNORE, then the row won't actually get inserted if there is a duplicate key (PRIMARY or UNIQUE), or inserting a NULL into a column with a NOT NULL constraint.
Referring to the pass column, as you have not defined anything to insert into it, and it has NOT NULL constraint.
EDIT:
Referring also to the mail column, as you have a UNIQUE constraint on it.
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()); ;