Jump to content

Works in PhpMyAdmin but NOT from web page


maralynnj

Recommended Posts

Here's what I've got:

 

SQL argument is attempting to insert data into a database called 'notifications'.

 

PHP CODE:

include 'AAPWconfig.php'; // host, user, pass, database

include 'AAPWopendb.php'; // conn mysql_connect

$sqlNTF="INSERT INTO notifications SET 'kind_ntf'='$IDntfK', 'status_ntf'='new', 'assigned_ntf'='$assigned', 'addedby_ntf'='$IDusr', 'notes_ntf'='$notes', 'date_ntf'=now(), 'same_ntf'='same'";

$resultNTF=mysql_query($sqlNTF);

if($resultNTF){echo "INSERT argument NTF is successful ";} else {echo "INSERT argument NTF is not successful ";}

echo $sqlNTF;

$IDntf = mysql_insert_id();

mysql_close();

 

It informs me that "INSERT argument NTF is not successful.

 

And the $sqlNTF echos out to:

INSERT INTO notifications SET 'kind_ntf'='21', 'status_ntf'='new', 'assigned_ntf'='20', 'addedby_ntf'='48', 'notes_ntf'='Submitted By: Joe Troubleshoot

Email: [email protected]

View User

Additional Notes:

General Information Request to include the following:

~Things to know about water quality

~Central water home / business integration

~Other (described below):

Additional Notes:

Test Comments

', 'date_ntf'=now(), 'same_ntf'='same'

 

The thing is, this exact query has worked for me on other pages. The mysql works just fine if I use PhpMyAdmin to query it, AND there are other querys on the same page with the same supporting php code (including config and opendb). So. I am at a loss...actually...i want to go plant trees in Alaska and give up on the computer altogether. Any brilliant solutions to my little php/mysql problem?? I'm almost afraid to hope that it's something obvious that I'm just missing and missing and missing and missing. Anyway...thanks!

 

you should always use mysql_error() if the query does not work to see whats wrong... try this though

 

 

INSERT INTO notifications (kind_ntf, status_ntf, assigned_ntf ...etc) VALUES ('".$IDntfK."', '".new."', '".$assigned."' ...etc)

 

 

btw, you were using the format that one would normally use to UPDATE a mysql row (minus the WHERE)

mysql_error echos out to:

 

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 ''kind_ntf'='21', 'status_ntf'='new', 'assigned_ntf'='20', 'addedby_ntf'='49', 'n' at line 1

 

I will try your suggestion on the syntax change and let you know how it goes. thanks!

Here's the revised query:

include 'AAPWconfig.php'; // host, user, pass, database

include 'AAPWopendb.php'; // conn mysql_connect

$sqlNTF="INSERT INTO notifications SET (id_ntf, kind_ntf, status_ntf, assigned_ntf, addedby_ntf, notes_ntf, date_ntf, same_ntf) VALUES ( NULL, '".$IDntfK."', 'new', '".$assigned."', '".$IDusr."', '".$notes."', now(), 'same' )";

if($resultNTF){echo "INSERT argument NTF is successful ";} else {echo "INSERT argument NTF is not successful ";}

echo $sqlNTF."<br /><br />";

echo mysql_error();

$IDntf = mysql_insert_id();

mysql_close();

 

Still no luck.

 

If I take the output from the "echo $sqlNTF" which echos my sql statement back to me:

INSERT INTO notifications SET (id_ntf, kind_ntf, status_ntf, assigned_ntf, addedby_ntf, notes_ntf, date_ntf, same_ntf) VALUES ( NULL, '21', 'new', '20', '50', 'Submitted By: Bob Test

Email: [email protected]

View User

Additional Notes:

General Information Request to include the following:

Additional Notes:

Test comments

', now(), 'same' )

 

It returns the following error:

#1064 - 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 '(id_ntf, kind_ntf, status_ntf, assigned_ntf, addedby_ntf, notes_ntf, date_ntf, s' at line 1

 

I'm obviously off on my syntax somewhere...but i can't tell where...could it be in my use of the now()?? but i use that all the time...seems strange...any suggestions?

I honestly am not sure why this worked, but it did. You probably know.

 

include 'AAPWconfig.php'; // host, user, pass, database

include 'AAPWopendb.php'; // conn mysql_connect

$sqlNTF="INSERT INTO notifications SET kind_ntf='$IDntfK', status_ntf='new', assigned_ntf='$assigned', addedby_ntf='$IDusr', notes_ntf='$notes', date_ntf=now(), same_ntf='same'";

$resultNTF=mysql_query($sqlNTF);

$IDntf = mysql_insert_id();

mysql_close();

 

Thank you for your help!!

Indeed, MySQL interprets anything enclosed by single quote (') as a literal. Thus you weren't actually supplying field names, merely supply strings which happened to contain the name of the field, and what was actually happening would have been that it was doing a comparison,

e.g.

'kind_ntf' = '$IDntfK'  => would be false

 

So you were in fact probably doing:

INSERT INTO notifications SET false, false, false, false, false, false, false

 

Hope that clarifies it for you...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.