473,396 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

INSERT query runs in mysql client, but not in PHP.

Hi, I'm learning the ropes with PHP and MySQL at the moment, and I've
run into a puzzle. I'm using PHP to process a form and insert some
simple information into a table. The code doesn't have any glaring
errors, and the mysql_query() doesn't error out, but the info never
gets inserted. And if I run the same query inside the mysql client the
data goes in fine. The query looks like this in PHP:

<?
function addNewStory($title_f, $user_f, $intro_f, $full_f){
global $conn; //included from database.php
$q = "INSERT INTO news_stories (story_title, user_id, date_posted,
intro_text, full_text) VALUES ('$title_f', $user_f, CURRENT_DATE(),
'$intro_f', '$full_f')";
return mysql_query($q, $conn);
}

$user = $_SESSION['user_id'];
$title = $_POST['title'];
$intro = $_POST['intro'];
$full = $_POST['full];

if(!$title || !$intro || !$full){
die('error message');
}else{
addNewStory($title, $user, $intro, $full);
}
?>
The related table is:
+-------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+----------------+
| story_id | int(8) | NO | PRI | | auto_increment |
| story_title | varchar(62) | NO | | | |
| user_id | int(8) | NO | MUL | | |
| date_posted | date | NO | | | |
| intro_text | varchar(512) | NO | | | |
| full_text | varchar(2048) | NO | | | |
+-------------+---------------+------+-----+---------+----------------+

Apr 26 '06 #1
6 1772
smedstadc schrieb:
Hi, I'm learning the ropes with PHP and MySQL at the moment, and I've
run into a puzzle. I'm using PHP to process a form and insert some

[skip]

Stupid question of mine:
Did you open a Database connection (mysql_connect()) and is it really
active while you try to execute your SQL?

I once had a similar problem and searched for hours until I realized
that the error wasn't within the SQL- Statement or the rest of the PHP-
conde but a missing mysql_connect() at the beginning.

HTH,

Andy

Apr 26 '06 #2
smedstadc wrote:
Hi, I'm learning the ropes with PHP and MySQL at the moment, and I've
run into a puzzle. I'm using PHP to process a form and insert some
simple information into a table. The code doesn't have any glaring
errors, and the mysql_query() doesn't error out, but the info never
gets inserted. And if I run the same query inside the mysql client the
data goes in fine. The query looks like this in PHP:


You don't seem to actually check the return value from mysql_query().
You return it from your function, but where you call addNewStory(), you
don't check what it returns.

You could try for example something like this:

<?php
$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
die('Invalid query: ' . mysql_error());
}

?>
Apr 26 '06 #3
Aggro wrote:
You don't seem to actually check the return value from mysql_query().
You return it from your function, but where you call addNewStory(), you
don't check what it returns.


I support this advice, and here's another comment: while debugging, it
is useful to output the query that caused the error, _after_
interpolating the PHP variables into it. Often, mismatched quotes and
other errors introduced by the variables become obvious if you look at
the SQL that is actually executed.

Regards,
Bill K.
Apr 26 '06 #4
Bill Karwin wrote:
Often, mismatched quotes and
other errors introduced by the variables become obvious if you look at
the SQL that is actually executed.


For example, can the value of any of these variables contain
single-quotes or apostrophe characters?

$title = $_POST['title'];
$intro = $_POST['intro'];
$full = $_POST['full];

Read http://www.php.net/manual/en/functio...ape-string.php
for an explanation, and examples of fixing the problem.

Regards,
Bill K.
Apr 26 '06 #5
smedstadc wrote:
$full = $_POST['full];


How did I miss that. You are missing an ' after the "full". That should
give you an syntax error message. If you are not getting it, you
seriously need to find out how to turn error messages on, on your server.

btw. Your problem is more related to php than it is to mysql. In
php-related newsgroup they would have noticed that propably sooner and
propably all the other errors we haven't noticed yet.
Apr 26 '06 #6
Thanks for all the advice, some of you said that in one of my $_POST
variables I was missing a single quote. That was simply a mistake
typing my message into google groups.

The link about escape sequences and strings was very helpful with
another problem I was having. But I found the problem with my form in
the meantime. In another php file I had some code to set a
$_SESSION['blahname'] variable written in the wrong place. It was off
by one brace, and didn't throw any errors. I put it in the right place
and the query worked fine now that I wasn't trying to insert a NULL
value into a column that was constrained not to take NULL's.

So, my problem was actually related to SQL, though none of you could
probably tell. My advice to anyone who stumbles upon this thread is to
double check their $_SESSION variables. In my case my syntax was
correct, but a one line logic error on line 143 foiled my SQL query in
a completely different place. I'm a little embarrassed to say it took
me hours to spot the mistake it was so buried in there.

Apr 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
6
by: a-ok | last post by:
Hi, My client has a product database od around 20000 items. And it is updated every few days when he gets a catalog from the supplier. It's supposed to work like this: if there already is a...
2
by: Bob Bedford | last post by:
I've a query with insert records in a table. I've 2 fields: DateTimeInsertion and DateTimeLastModification. When I create the record, I must have those 2 fields set the same value. Now I've...
3
by: Paradigm | last post by:
I am using Access 2K as a front end to a MYSQL database. I am trying to run a Union query on the MYSQL database. The query is (much simplified) SELECT as ID from faxdata UNION SELECT as ID ...
2
by: saifmsg | last post by:
Hello everyone, I am using a custom php class where i have wrapped all the mysql connection settings and query functions. Instead of calling direct mysql functions i creat an instance of this...
0
by: umangjaipuria | last post by:
I have two job queues doing complimentary work and writing their output once a minute into a file. The files for each minute have to processed in pairs - one from the first job queue and one from...
9
by: David Eades | last post by:
Hi all Complete newbie here, so apologies if this is the wrong forum. I've been asked to use mysql and asp to make a simple bidding system (rather like a simple ebay), whereby users can use a...
14
by: Ben | last post by:
I don't know whether anyone can help, but I have an odd problem. I have a PSP (Spyce) script that makes many calls to populate a database. They all work without any problem except for one...
3
by: Waruna | last post by:
Is there a way to block insert into mysql(5.0) using c api of mysql db.. i.e. say there is a table with 2 columns, one contains char other int then i want to insert 500 records at once,, as i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.