469,904 Members | 2,067 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,904 developers. It's quick & easy.

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 1648
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bob Bedford | last post: by
3 posts views Thread by Paradigm | last post: by
2 posts views Thread by saifmsg | last post: by
reply views Thread by umangjaipuria | last post: by
9 posts views Thread by David Eades | last post: by
3 posts views Thread by Waruna | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.