MySQL syntax error inserting php strings into table - php

I'm parsing a website's table with QueryPath and trying to put my results into a MySQL database.
The table looks like this:
mysql_query("CREATE TABLE Airplanes (
flightID VARCHAR( 50 ) PRIMARY KEY NOT NULL,
flightLink TEXT( 20000 ) NOT NULL,
orig TEXT( 20 ) NOT NULL,
dest VARCHAR( 20 ) NOT NULL ,
time VARCHAR( 5 ) NOT NULL
);
");
I was trying to save airplanes using their flight numbers as IDs.
This is how I extract the table and the echos for shoving the variables' contents.
$flightData = $row->find('td');
// $flightID = str_replace(" ", "", $flightData->eq(1)->text());
$flightID = mysql_real_escape_string( trim( $flightData->eq(1)->text() ) );
$flightLink = mysql_real_escape_string( $flightData->eq(1)->html() );
$orig = mysql_real_escape_string( "ROME (FCO)" );
$dest = mysql_real_escape_string( trim( $flightData->eq(2)->text() ) );
$time = mysql_real_escape_string( trim( $flightData->eq(4)->text() ) );
echo '$flightID: ';
echo var_dump($flightID)."<br>";
echo '$orig: ';
echo var_dump($orig)."<br>";
echo '$dest: ';
echo var_dump($dest)."<br>";
echo '$time: ';
echo var_dump($time)."<br>";
Didn't ask to echo $flightLink, that would have been pretty long.
This is the output on the variables:
$flightID: string(7) "JN 5215"
$orig: string(10) "ROME (FCO)"
$dest: string(14) "TEL AVIV (TLV)"
$time: string(5) "23:45"
This is my SQL-query:
$insertQuery = mysql_query("INSERT INTO Airplanes (flightID, flightLink, orig, dest, time) VALUES( '$flightID', '$flightLink', '$orig', '$dest', '$time' ) ON DUPLICATE KEY UPDATE;");
if($insertQuery == false) die("Problem inserting flight data into table. ".mysql_error($connection));
And this is the error message I get on the input query:
Problem inserting flight data into table. 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
I've seen loads of other guys having trouble feeding MySQL with strings, almost all of them failed on quotation marks, so I bet it's gonna be something about that. Still couldn't find it though.
Also grateful for feedback on improving the MySQL-table, just getting into this and not too sure about the data types.

Your INSERT ... ON DUPLICATE KEY UPDATE syntax is invalid because you have to tell what columns you want to update when a duplicate is encountered
INSERT INTO Airplanes (flightID, flightLink, orig, dest, time)
VALUES( '$flightID', '$flightLink', '$orig', '$dest', '$time' )
ON DUPLICATE KEY UPDATE
^^^^ missing part of ON DUPLICATE clause
It should be something like
INSERT INTO Airplanes (flightID, flightLink, orig, dest, time)
VALUES( '$flightID', '$flightLink', '$orig', '$dest', '$time' )
ON DUPLICATE KEY UPDATE flightLink = VALUES(flightLink),
orig = VALUES(orig),
dest = VALUES(dest),
time = VALUES(time)

Related

Invalid arguments passed in implode()

So I have a CSV file that I'm trying to make into a table.
I gave up on the import GUI after too many errors, and am trying to accomplish the import through a php file.
//create table with KNOWN values
mysql_query("CREATE TABLE uri_faculty
(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
id INT(15),
lname VARCHAR(50),
fname VARCHAR(50),
mi VARCHAR(3),
Spc_Title VARCHAR(50),
title VARCHAR(50),
deptid INT(5),
dept VARCHAR(50),
degree1 VARCHAR(10),
earned1 INT(4),
school1 VARCHAR(75),
degree2 VARCHAR(10),
earned2 INT(4),
school2 VARCHAR(75),
degree3 VARCHAR(10),
earned3 INT(4),
school3 VARCHAR(75),
degree4 VARCHAR(10),
earned4 INT(4),
school4 VARCHAR(75),
degree5 VARCHAR(10),
earned5 INT(4),
school5 VARCHAR(75),
degree6 VARCHAR(10),
earned6 INT(4),
school6 VARCHAR(75)
)");
//Get CSV file
$getfile = 'faculty_delim2.csv';
$csvfopen = fopen($getfile, "r");
//loop to fill csvget with arrays
for($i=0;!feof($csvfopen);$i++){
$array = fgetcsv($csvfopen);
$insert = implode("','", $array);
//to exclude the first 2 lines (titles of document)
if($i>=1){
//values to be inserted into SQL are displayed
//echo var_dump($array[$i])." <br> ";
$sqlval = $insert;
// var_dump($sqlval);
//the while loop will constantly place values into the database until the file is finished
mysql_query("INSERT INTO uri_faculty (id,lname,fname,mi,Spc_Title,title,deptid,dept,
degree1,earned1,school1,
degree2,earned2,school2,
degree3,earned3,school3,
degree4,earned4,school4,
degree5,earned5,school5,
degree6,earned6,school6,) VALUES ('$sqlval')
");
}
}
fclose($csvfopen);
echo "complete";
?>
I keep getting an error saying that implode is receiving incorrect parameters, yet every bit of documentation I've found says that I am correct.
I changed the permissions of the file, and it is in the right place.
Instead of this:
for($i=0;!feof($csvfopen);$i++){
$array = fgetcsv($csvfopen);
I would write this:
while ($array = fgetcsv($csvfopen)) {
The loop will automatically finish when there are no more rows to read. Your error is probably an edge case, where the file still thinks it is not at feof but in spite of that, there are no more rows, and fgetcsv returns false.
By the way, two other issues:
You will run into another error soon:
. . . degree6,earned6,school6,) VALUES . . .
You must not put a comma after the last column. Write this instead:
. . . degree6,earned6,school6) VALUES . .
You are wide open to SQL injection issues. What happens when one of your CSV fields contains an apostrophe? You should learn how to use PDO with query parameters. Or failing that, use escaping:
$insert = implode("','", array_map('mysql_real_escape_string', $array));
Finally, you should consider skipping fgetcsv and use LOAD DATA INFILE. Then all your CSV issues, and escaping issues just go away.
mysql_query("LOAD DATA LOCAL INFILE 'faculty_delim2.csv' INTO TABLE uri_faculty IGNORE 2 LINES");

Error while inserting data into Mysql database

I am trying to insert data into Mysql table, but it is giving me an error as-
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 'Scoretab VALUES ('UX 345','22','0.8562675')' at line 1
This is the php-mysql snippet that im using :
if($value >= 0.70){
$mu_id = $ros['c_id'];
$moc_id = $ram['t_id'];
$query="INSERT INTO Scoretab VALUES ('$mu_id','$moc_id','$value')";
$op1 = mysql_query($query) or die(mysql_error());
}
This is my table structure:
CREATE TABLE IF NOT EXISTS `Scoretab` (
`mu_id` varchar(10) NOT NULL,
`moc_id` int(5) NOT NULL,
`score` decimal(5,4) NOT NULL,
UNIQUE KEY `mu_id` (`mu_id`)
)
There could potentially be a few problems with this query
$query="INSERT INTO Scoretab VALUES ('$mu_id','$moc_id','$value')";
Does the number of columns match the fields your trying to insert? Have you tried using using specific column identifier Scoretab (col,col,col) values (val, val, val)
Does any of your values contain an unescaped apostrophe? You might want to consider using mysql_real_escape_string for $mu_id and intval for $moc_id maybe!
$value is a float you don't need to ad apostrophes while inserting
Are you sure you are connected to the same database you have this table in?
this could be a possible working solution (edit)
if ($value >= 0.70)
{
$mu_id = mysql_real_escape_string($ros['c_id']);
$moc_id = intval($ram['t_id']);
$query = "INSERT INTO `Scoretab` VALUES ('$mu_id', $moc_id, $value)";
$op1 = mysql_query($query) or die(mysql_error());
}
try this
$query="INSERT INTO Scoretab (mu_id,moc_id,score) VALUES ('$mu_id','$moc_id','$value')";
The error seems to be before the table name Scoretab. Did you check your syntax carefully?
Sometimes we don't see what's right in front of our eyes! :D
Just replicated the example and everything worked for me.

Inserting/Updating MySQL table via PHP, row will update but not insert.

Im not very experienced in PHP and would appreciate any help! Im am sending a username, longitude and latitude to my server using HTTP Post. I first check if the user exists in the user_info table. If it does then i try to update the maps_user_location table with that username. What i want to happen is that if the query fails, due to the username not being found in maps_user_location table, then i want it to be added. I've tried to do it with the code below.
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_localhost, $localhost);
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$username = $_POST['username'];
if ($latitude != 0 && $longitude != 0) {
$query_userCheck = "SELECT * FROM user_info WHERE login_username = '".$username."';";
if ( mysql_query($query_userCheck) ) {
$query_updateLocation = "UPDATE `maps_user_location` SET `lat` = '".$latitude."',`long` = '".$longitude."' WHERE
`login_username` = '".$username."';";
if ( !mysql_query($query_updateLocation) ){
$query_newLocation = "INSERT INTO `maps_user_location` ( `id` ,`login_username` ,`lat` ,`long` ,`track`)
VALUES ( NULL , '".$username."', '".$latitude."', '".$longitude."', '0');";
mysql_query($query_newLocation);
}
}
}
When i execute the code, it goes through without a hitch. The problem is that a row is never inserted if the username does not exist already, the row does update if the username exists though. Additionally, when i add a row manually, the id column seems to increment after every request (the sequence will go 1,2,3,4,10,11,12...etc [the ids 5-9 do not exist]). I have a feeling that it has to do with the 'WHERE' in '$query_updateLocation'. Anyone have any clue as to why this happens?
You can simply do that in a single query, Use INSERT...ON DUPLICATE KEY UPDATE
INSERT INTO maps_user_location ( login_username , lat, long, track)
VALUES ('".$username."', '".$latitude."', '".$longitude."', '0')
ON DUPLICATE KEY UPDATE
lat = '".$latitude."' ,
long = '".$longitude."'
but before anything else, make sure that you have a UNIQUE CONSTRAINT on the column login_username. If you have not added, you can alter your table using this command
ALTER TABLE maps_user_location ADD UNIQUE (login_username);

MySQL > sum up into a single query (incl. google maps)

This Q is posted as a follow up to a comment on one of my Qs by #Vincent Savard.
Currently I'm trying (with some success - hitting timeout after 60 sec. for google-maps XML requests) to split data from a (pretty) large table into lots of smaller tables - plus converting/modifying/etc them on the fly. For this task I'm using php close to the following example:
// The following happens inside some functions.
// The main table has some "groups" of content.
// fn_a creates the new small tables
"
CREATE TABLE {$small_table_a}
col_group_a_id int UNSIGNED NOT NULL AUTO_INCREMENT,
col_group_a_fname tinytext,
col_group_a_lname tinytext,
col_group_a_valA tinytext,
col_group_a_valB tinytext,
col_group_a_address tinytext,
col_group_a_lat decimal(9,3),
col_group_a_lng decimal(9,3),
PRIMARY KEY (id)
"
"
CREATE TABLE {$small_table_b}
col_group_b_id int UNSIGNED NOT NULL AUTO_INCREMENT,
col_group_b_fname tinytext,
col_group_b_lname tinytext,
col_group_b_valA tinytext,
col_group_b_valB tinytext,
col_group_b_address tinytext,
col_group_b_lat decimal(9,3),
col_group_b_lng decimal(9,3),
PRIMARY KEY (id)
"
// fn_b loads the content from the big table, modifies it and saves it row per row into the small tables
$sql = "
SELECT *
FROM {$big_table}
"
foreach ( $sql as $data )
{
$id = $data->id;
$group_a_fname = $data->group_a_fname;
$group_a_lname = $data->group_a_lname;
$group_a_lname = "{$group_a_fname}, {$group_a_lname}";
$group_a_valA = $data->group_a_valA ? $data->group_a_valA : '-';
$group_a_valA = $data->group_a_valB ? $data->group_a_valB : 'none';
$group_a_valA = $data->group_a_address;
$group_b_fname = $data->group_b_fname;
$group_b_lname = $data->group_b_lname;
$group_b_name = "{$group_b_fname}, {$group_b_lname}";
$group_b_valA = $data->group_b_valA ? $data->group_b_valA : '/';
$group_b_valA = $data->group_b_valB ? "€ {$data->group_b_valB}" : null;
"
INSERT INTO {$small_table_a} ... VALUES ...
"
}
// fn_c pulls in data from the small tables, asks the google map API for lat & lng and _should_ update the small table
$sql = "
SELECT *
FROM {$small_table_a}
"
foreach ( $sql as $data )
{
$output['id'] = $data->id;
$address = urlencode( $data->address );
$url = "http://maps.google.com/maps/api/geocode/xml?address={$address}&sensor=false";
$content = file_get_contents( $url );
$file_data = new SimpleXMLElement( $content );
$file_data = $file_data->result ? $file_data->result : null;
if ( ! $file_data )
continue;
$location = $file_data->geometry->location;
$output['lat'] = (string) $location->lat;
$output['lng'] = (string) $location->lng;
}
foreach ( $output as $data )
{
"
UPDATE {$table}
SET lat=SET lat={$data['lat']}, lng={$data['lng']}
WHERE id=$data['id']
}
Question: How could I do this in one query? Or how could I reduce DB-queries? And how would I add the lat/lng to the tables without interrupting the query building when my geocoding limit was exceeded for today - I don't want to drop everything just because I've gone over my limit.
Thanks!
Note: The example was written by hand straight out of my mind. Failures may be in there.
We need to know what the INSERT INTO query is in you foreach loop, because this is the one that can be summed into one query. Basically, here is the idea:
INSERT INTO {$small_table} -- you can specify which columns to fill,
-- i.e. INSERT INTO table (col_a, col_b)
SELECT group_a_fname, group_a_lname,
group_a_valA, group_a_valB,
group_a_address, group_b_fname,
group_b_lname, group_b_valA, group_b_valB -- etc
FROM {$big_table};
Obviously, you'll have to adapt the query to fill your needs. You just need to grasp the idea behind it : you can insert rows with a SELECT query.
The UPDATE query is different because you have to rely on external data (a website). I don't think there is an easy way to do it in one query, but I may be wrong.

Query won't insert username into database

So I've got this query:
mysql_query(
"INSERT INTO wall_post (post,username,userip,date_created)
VALUES(
'".checkValues($_REQUEST['value'])."',
'".$_SESSION['user']."',
'".$userip."',
'".strtotime(date("Y-m-d H:i:s"))."'
)"
);
and I also tried to make the query this way:
mysql_query(
"INSERT INTO wall_post (post,username,userip,date_created)
VALUES(
'".checkValues($_REQUEST['value'])."',
$_SESSION['user'],
'".$userip."',
'".strtotime(date("Y-m-d H:i:s"))."'
)"
);
I don't see any error message from the database when the insert fails.
It won't insert the username into the database but when I echo $_SESSION['user'] it would still show me its content, please I would appreciate some help.
The table structure is:
CREATE TABLE wall_post (
p_id int(11) NOT NULL auto_increment,
username varchar(50) NOT NULL,
post varchar(255) NOT NULL,
image varchar(50) NOT NULL,
date_created int(11) NOT NULL,
userip varchar(200) NOT NULL, PRIMARY KEY (p_id)
)
The value which contains $_SESSION['user'] is theil, it doesn't have any special character, but if I replace $_SESSION['user'] with a string like $user = "test"; it will insert the value "test" into the database
mysql_query for insert statements either returns True on success or False on error. You have to check the return value if it was successful, and if it wasn't successful get the error via mysql_error:
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
It should be easy to fix from there.
The image column is set to NOT NULL, but you are not inserting anything into it. I suspect removing the NOT NULL clause, or setting a default value for the column might fix your problem.
Additional tip. use MYSQLS NOW() for the date. Just let the database handle that bit :)
just check what the value is and make sure there are no special characters in there.
You can also try "'.mysql_real_escape_string($_SESSION['user']).'"
the problem might be special characters.
From all the comments try this
$name = isset($_REQUEST['user']) ? $_REQUEST['user'] : '';
mysql_query('INSERT INTO wall_post (post,username,userip,date_created) VALUES("'..checkValues($_REQUEST['value']).'",
"'.$name.'","'$ipAddress'","'.$timestamp.'")');
From one of your comments above, I learnt that if you echo your query, it shows as
INSERT INTO wall_post (post,username,userip,date_created)
VALUES('','theil','127.0.0.1','1309975742')
Did you do this echo just before the statement where you run the query? If not, I'd request you to please do the echo just before the call, like this:
echo "INSERT INTO wall_post (post,username,userip,date_created) VALUES(
'".checkValues($_REQUEST['value'])."',
'".$_SESSION['user']."',
'".$userip."',
'".strtotime(date("Y-m-d H:i:s"))."')";
mysql_query("INSERT INTO wall_post (post,username,userip,date_created) VALUES(
'".checkValues($_REQUEST['value'])."',
'".$_SESSION['user']."',
'".$userip."',
'".strtotime(date("Y-m-d H:i:s"))."')"
);
Your query seems to be absolute fine and should run fine. The only reason why username might not be saving into the database is that `$_SESSION['user'] is empty or does not exist.
Did you try running this echoed query - INSERT INTO wall_post (post, username, userip, date_created) VALUES('', 'theil', '127.0.0.1', '1309975742') - directly into MySQL, either on the prompt or any other client that you might be using?

Categories