RGF Drone
RGF Drone

Reputation: 37

PHP keeps saying invalid column name

Here is my code:

<?php $txtCommentor = $_POST['txtCommentor'] //from form?>
<?php $txtComment = $_POST['txtComment'] //from form?>
<?php $jobno = $_GET['jobno'] //from URL?>

<?php
//Query and connect
$query = "INSERT INTO delivery_comments (commentor, comment, jobno) VALUES ($txtCommentor,$txtComment, $jobno) ";?>
<?php $results = sqlsrv_query($conn, $query);?>
<?php echo $query?>
<?php
if( $results === false ) {
     die( print_r( sqlsrv_errors(), true));
}
?>

When I run the page I get this these results:

INSERT INTO delivery_comments (commentor, comment, jobno) 
VALUES (frontdesk,bvhjfhj, 85450) 

Then I get the error:

Array ( [0] => Array ( [0] => 42S22 [SQLSTATE] => 42S22 [1] => 207 [code] => 207 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'frontdesk'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'frontdesk'. ) [1] => Array ( [0] => 42S22 [SQLSTATE] => 42S22 [1] => 207 [code] => 207 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'bvhjfhj'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'bvhjfhj'. ) )

Notice the echoed insert statement is first. This is correct. Then the rest of the error keeps telling me the data I want to insert is an invalid column name. This doesn't make any sense. That info is not supposed to be the column name but the data inserted into a column.

Upvotes: 0

Views: 2788

Answers (2)

RGF Drone
RGF Drone

Reputation: 37

This is what I ended up using:

$query = "INSERT INTO delivery_comments (commentor, comment, jobno) VALUES ('$txtCommentor','$txtComment', '$jobno') ";

Upvotes: -1

exussum
exussum

Reputation: 18550

<?php $txtCommentor = $_POST['txtCommentor'] //from form?>
<?php $txtComment = $_POST['txtComment'] //from form?>
<?php $jobno = $_GET['jobno'] //from URL?>

<?php
$params = array( $txtCommentor, $txtComment, $jobno);
//Query and connect
$query = "INSERT INTO delivery_comments (commentor, comment, jobno) VALUES (?,?,?) ";?>
<?php $results = sqlsrv_query($conn, $query,$params);?>

The above is the sanitized version of your query.

You should have much more success with that

Upvotes: 2

Related Questions