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: joe@maralynnj.com

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!

 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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: bob@maralynnj.com

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?

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.