unable to save polygon coordinates in database [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I want to save the JSON coordinates of polygon in the PostGIS database.
This is my PHP code-
$coordinates = ($_POST['Coordinates']);
$query = "INSERT INTO table_name (column_name) VALUES
(ST_SetSRID(ST_GeomFromGeoJSON ('{
['".($coordinates) ."']
}'), 4326))"
$success = pg_query($conn,$query);
where Coordinates are-
{"type":"polygon","coordinates":[[[-97.53662109375,43.67581809328341],[-98.096923828125,42.60970621339408],[-96.427001953125,42.924251753870685],[-97.53662109375,43.67581809328341]]]}
but the error is-
"Parse error: syntax error, unexpected '$success' (T_VARIABLE)"
column_name is polygon type column in table.
please someone correct me how to resolve syntax error.
Thanks.

T_VARIABLE error mainly occurs because of the syntax error. You missed a Semicolon before $success line. Please check the syntax
$coordinates = ($_POST['Coordinates']);
$query = "INSERT INTO table_name (column_name) VALUES
(ST_SetSRID(ST_GeomFromGeoJSON ('{
['".($coordinates) ."']
}'), 4326))";
$success = pg_query($conn,$query);

Related

codeigniter count_all_results with join table [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I have a problem getting the number of rows from my tables.
Here is my mysql query using codeiginiter:
$this->db->where('rd', 1);
$this->db->where('id_user_first', $user_type);
$this->db->from("messaging");
$this->db->join('answers', "answers.id_message = messaging.id AND answers.id_user != '$user_type'");
$count_reponse = $this->db->count_all_results();
I get this error message:
Parse error: syntax error, unexpected '' ''
(T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in
You can try this solution for your problem :
Please changes query
$this->db->select('messaging.*, answers.*');
$this->db->from("messaging");
$this->db->join('answers', "answers.id_message = messaging.id");
$this->db->where('answers.id_user <>', $user_type);
$this->db->where('messaging.rd', 1);
$this->db->where('messaging.id_user_first', $user_type);
$count_reponse = $this->db->count_all_results();
You need to escape your quotes or change the inner double-quotes to single quotes.
$this->db->join('answers', 'answers.id_message = messaging.id AND answers.id_user != '.$user_type.'','left');
Try This query:
$this->db->where('rd', 1);
$this->db->where('id_user_first', $user_type);
$this->db->from("messaging");
$this->db->join('answers', 'answers.id_message = messaging.id', 'left');
$this->db->where('answers.id_user !=', $user_type);
$count_reponse = $this->db->count_all_results();
I hope it will help. Please leave a comment if any error came.

Need help in php multidimensional array and json response [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
So I am new to php and json and app development. I tried googling for answers but those answers just generate more errors. I am posting an user_id from the app and trying to get all the gyms related to the users. I have had no problem getting response from other php files.
I am trying to get back a 2d array of 3 columns, gym_id, gym_name, and user_id. The code below results in an error of "Parse error: syntax error, unexpected end of file ... on line 25". I am sorry if this is too amateur, but I simply have ran out of ways to proceed.
Thanks in advance for help.
<?php
$con = mysqli_connect(...);
$user_id= $_POST["user_id"];
$statement = mysqli_prepare($con, "SELECT Gym.gym_id, Gym.name, user_gym_rel.user_id FROM Gym INNER JOIN user_gym_rel ON Gym.gym_id = user_gym_rel.gym_id WHERE user_gym_rel.user_id = '$user_id');
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $gymid, $gymname, $userid);
$response = array();
i=0
while(mysqli_stmt_fetch($statement)){
$response[i] = array();
$response[i]['gym_id'] = $gymid;
$response[i]['gym_name'] = $gymname;
$response[i]['user_id'] = $userid;
i++;
}
echo json_encode($response);
?>
Ok so thanks for the speedy reply, added the ", changed the variable i to $i. and everything works! thanks for the help everyone :)

T_CONSTANT_ENCAPSED_STRING error. [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
Hello, I'm very new to PHP and im getting this error...:
Parse error: syntax error, unexpected ''arak''
(T_CONSTANT_ENCAPSED_STRING) in /testSQL.php on line 6
...for this line:
$query = UPDATE 'arak' SET `ara` = '$ar1' Limit 0,1;
A little help would be appriciated :)
You have to quote the string by ", protect the table name by ` and protect value with '
$query = "UPDATE `arak` SET `ara` = '$ar1' Limit 0,1";
Be careful, $ar1 must be protected. For example, if $ar1 = '33\'33', you could have problem.
$ar1 = addslashes($ar1);
$query = "UPDATE `arak` SET `ara` = '$ar1' Limit 0,1";
Addslashes is a first step to prevent SQL Injection, but it is not enough as you can read it

syntax error, unexpected 'order' (T_STRING) [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I put this code in my php file and it gives me the error syntax error, unexpected 'order' (T_STRING)
What am I doing wrong?
$sqldelreq="DELETE FROM `requests` WHERE tablecode = 1 and type = "order"";
$result2=mysql_query($sqldelreq);
if($result2)
{
header("Location: http://localhost/mjjapp/index.php");
}
I think the query should be:
"DELETE FROM `requests` WHERE tablecode = 1 and type = 'order'";
Please note the single quotes around order.
You should do proper escape
$sqldelreq = "DELETE FROM `requests` WHERE `tablecode` = 1 and `type` = 'order'";
Also your if is invalid. It only means that query was successful if you wan't to check if any rows were deleted you need to check how many rows were affected with mysql_affected_rows() function.
Moreover, consider using mysqli or pdo. Mysql_* functions are deprecated.
https://dev.mysql.com/doc/refman/5.6/en/keywords.html
https://dev.mysql.com/doc/refman/5.0/en/string-literals.html
Correct syntax usually goes a long way; try:
$sqldelreq = "DELETE FROM `requests` WHERE `tablecode` = 1 AND `type` = 'order';";
i found this running well thanks guys for ideas
$sqldelreq="DELETE FROM requests WHERE tablecode = 1 and type = 'order';";

How to assign Array to a String in PHP? [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I am trying to assign the contents of a 2D array to a string in PHP.
$sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '$output['username']';";
I know the problem exists in how I'm writing the $output variable assignment.
The following line of code outputs the correct data from the variable:
echo $output['username'];
The following error is being thrown:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
Parameters surrouned by curly brackets will work well in your case. Here is what i mean. {$array['key']}
And for your example:
$sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '{$output['username']}';";
You need to concatenate this string as the multiple single quotes from accessing the array element are mixing up the string.
$sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '" . $output['username'] ."';";
Try this:
$sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '".$output['username']."'";
You can also try this:
$user_name = $output['username'];
$sql = "SELECT Order_Code FROM Order WHERE CUST_CODE = $user_name";

Categories