php


You have an error in your SQL syntax…?


//Can anyone tell me where is the error in the below sql query.
$sql = "insert into $table ($columns) values ($values)";
/Its gives me this error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version >for the right syntax to use near ')' at line 1
You will have to check if there is no starting and trailing comma's in the $columns or $values variables.
Plus, just to be sure, put appropriate quotes around the columns and values individually.
public function insert($data, $table) {
$columns = "";
$values = "";
foreach ($data as $column=>$value)
{
$columns .= "`" . $column . "`, ";
$values .= "'" . $value . "', ";
}
$columns = substr($columns, 0, strlen($columns)-2);
$values = substr($values, 0, strlen($values)-2);
$sql = "insert into ".$table." (".$columns.") values (".$values.")"; mysql_query($sql) or die (mysql_error()); return mysql_insert_id(); }
Use ` backticks for MYSQL reserved words. Your query should be as follows.
$sql = "insert into `$table` (`$columns`) values (`$values`)";

Related Links

Trying to take php variable from 1 php file to another and then from there to a form entry to be used by a third php file
phpinfo() showing different upload_max_filesize than in php.ini
Issues with HTML PHP Mailer
post file to third party API using curl
Inline ajax's response fails in checking condition even though the condition is true
How to remain number as string in PHP
How to append array to associative array in a foreach loop
How to sort returned sql result by id that appears the most time in that result
PDO php two statements in one function, if both are true commit else rollback
Set new values on the url
symfony2.3 doesn't load messages yml for one locale (other locales are working fine)
Regex Number reservations
PHPExcel creat a new file
What is an example of a non utf-8 character(not the codepoint)?
Issues calculating signature for Amazon Marketplace API
Why am I receiving HY093 error [PDO]

Categories

HOME
visual-studio
extjs
svn
opencv-python
d3.js
systemd
antd
website
android-emulator
dronekit-python
joomla3.2
timber
anchor
wine
smartgwt
bundler
coroutine
adobe-illustrator
angular2-highcharts
akka-persistence
bosun
ipa
background-color
android-cardview
tsung
bitbake
microprocessors
source-insight
tightvnc
microsoft-certifications
nppexec
cordys-opentext
jade4j
cargo
twilio-php
forms-authentication
android-alarms
task-parallel-library
system-on-chip
classnotfoundexception
haar-classifier
elasticsearch-aggregation
md5-file
jgrapht
datediff
spring.net
skip-lists
android-sharing
test-data
dtd
gmaps.js
msxml
petsc
ogg
cocoa-bindings
powerpoint-2013
taco
voronoi
wepay
myfaces
smart-tv
largenumber
apache-commons-digester
directx-9
highest
algebraixlib
clarion
azure-xplat-cli
python-curses
tidyr
jolie
symja
password-recovery
multivariate-testing
squeezebox
stripe.net
flipboard
pre
frontbase
apportable
aero
django-filebrowser
cyrillic
gitx
story
stretch
voice-recording
taskkill
gamesalad
dynamic-c
server-variables
office-automation
jquery-1.4
digiflow
principles
source-code-protection
main-method
iphone-sdk-3.1.3

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile