php


Insert into table doesnt work


i have this code, but i cant get it to insert any thing in my sql table?
the echoing out part works fine..
/* We have succesfully resized and created thumbnail image
We can now output image to user's browser or store information in the database */
$s_id = $_GET['id'];
$conn = mysqli_connect('localhost', 'user', 'pass', 'table');
$query = "INSERT INTO table ( sp_s-id, sp_thump, sp_orig ) VALUES
( '$s_id', '$thumb_save_folder', '$image_save_folder' )";
//added $conn variable in order to connect to our database.
mysqli_query($conn, $query);
mysqli_close($conn);
echo '<div align="center">';
echo '<img src="uploads/'.$thumb_prefix . $new_file_name.'" >alt="Thumbnail">';
echo '<br />';
echo '<img src="uploads/'. $new_file_name.'" alt="Resized Image">';
echo $s_id;
echo '</div>';
imagedestroy($image_res); //freeup memory
Replace this:
$query = "INSERT INTO table ( sp_s-id, sp_thump, sp_orig ) VALUES
( '$s_id', '$thumb_save_folder', '$image_save_folder' )";
With this:
$query = "INSERT INTO `table` ( `sp_s-id`, `sp_thump`, `sp_orig` ) VALUES
( '$s_id', '$thumb_save_folder', '$image_save_folder' )";
SQL server (as well as PHP or most other languages) can't handle column/table/variable names with a minus (-) in it, so you'd need to put them into quotes/ticks. It's highly recommendable though to follow the rule [_A-Za-z][0-9_A-Za-z]* for column/table/variable names (meaning only numbers, characters (A-Z) and underscores, but not starting with a number).
Also, you should ALWAYS validate or type cast user input, like this (validate):
$s_id = $_GET['id'];
if (!is_numeric($s_id)) {
// invalid input / possible hacking attempt?
}
Or this (type cast):
$s_id = (int) $_GET['id'];
You syntax is wrong. Try this.
If you're not using object oriented pattern, I suggest using mysql over mysqli.
$conn = mysql_connect('localhost', 'username', 'password');
$db = mysql_select_db('databasename', $conn);
$query ="INSERT INTO tablename ( sp_s-id, sp_thump, sp_orig ) VALUES ( '{$s_id}', '{$thumb_save_folder}', '{$image_save_folder}' )";
$result = mysql_query($query);
mysql_close($conn);
Try this. It should insert into the table. Make sure to put your variables inside {} when using within double quotes.

Related Links

define('QA_BASE_DIR', dirname(empty($_SERVER['SCRIPT_FILENAME']) ? __FILE__ : $_SERVER['SCRIPT_FILENAME']).'/');
Watson Concept Expansion Service
simplexml php extract data in a for loop
Parsing a large XML file, crashing IIS
Insert_batch CodeIgniter
When is it safe to use PHP Get requests
Connection between MySQL Event and Table
Simple HTML/CSS website requires a form content placement in an email message
navigation on null route parameters ZF2
Merge Dependency Injection Parameters by key
Compare data from two tables and check the same data
404 Error on ZF2 skeleton application on all tutorials
Angularjs $http.post, long operations on server with PHP
wp_enqueue_style places the output CSS directly in the HTML
PHP Looping through arrays with for loop
How can I style this php with divs?

Categories

HOME
openshift
visual-studio
magento2
generics
verification
opencv-python
paypal-ipn
loopbackjs
cryptography
google-search-console
onvif
stack-overflow
yahoo
xbox360
global-variables
amazon-mws
social-tables
azure-machine-learning
firemonkey-style
web-config-transform
multi-dimensional-scaling
uilocalnotification
macromedia
cube
boolean-logic
source-insight
toad
infopath2010
delayed-job
john-the-ripper
dreamfactory
runtimeexception
macports
telephonymanager
python-sounddevice
praat
klee
angular-datatables
viewmodel
cloudwatch
nonlinear-functions
asp.net-web-api-routing
ptvs
clrs
zend-server
nrf51
ogg
haraka
livereload
data-management
callouts
node-glob
flipclock
phpquery
bayesglm
self
openshift-cartridge
nette
astropy
bjam
mixins
netdatacontractserializer
webproject
mgwt
mod-auth-openidc
lifetime
password-recovery
utf8-decode
windows-rt
lumia-imaging-sdk
accessory
redpitaya
jms-serializer
django-sites
proxy-server
saga
vlab
coalesce
form-for
node.js-stream
rikulo
kaazing
monocross
opends
fitch-proofs
affinetransform
gwt-celltable
will-paginate
rtd
django-pagination
git-log
main-method
levels

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile