Closed Thread Icon

Preserved Topic: php/sql - how to insert into a table? Pages that link to <a href="https://ozoneasylum.com/backlink?for=20920" title="Pages that link to Preserved Topic: php/sql - how to insert into a table?" rel="nofollow" >Preserved Topic: php/sql - how to insert into a table?\

 
Author Thread
GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 05-24-2001 18:39

this is a stupid/newbie question, but i just dont get it. i wanna insert some values into a table and im using this code:


$link = mysql_pconnect("db.embege.f2s.com","embege","guess again....hehe");

mysql_select_db("embege",$link) or die("Unable to select database");

mysql_query("INSERT INTO guests (guest_name,guest_email,guest_message) VALUES ($fname, $femail, $fmessage)")
or die("Insert Failed!");


i get "insert failed". whats wrong?

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 05-24-2001 19:03

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 05-24-2001 19:09

thanks for the quick answer, ini! it works!

now i have just another simple question:
how do i redirect to a page in php?
(javascript would be: document.location="...")

im trying: header("Location: http:.... ");
but, that gives me an error: headers already send

linear
Paranoid (IV) Inmate

From: other places
Insane since: Mar 2001

posted posted 05-24-2001 20:27

Your code is correct, but if PHP has sent *any* text at that point, it has sent headers already. So that command has to preced any code that outputs. Including HTML outside the <?php ?> delimiters.

Also, ALWAYS check the return code from that query.
Like this:
$result = mysql_query(SQL1, $db);
// obligatory check of result code, verbose error reporting here
if (!$result) {
$errno = mysql_errno($db);
$error = mysql_error($db);
echo "Result = $errno : $error \n";
exit;
}

You get a detailed error message that way.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 05-24-2001 20:52

Also, another good thing is to call exit() after header("Location: blah")...

jiblet
Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 05-26-2001 22:31

Hmm, no one mentioned that InI is wrong. Well, half wrong. The single quotes are indeed necessary for the insert. But concatenating the variables is UNNECESSARY. The way you originally had it was only missing the isngle quotes. For those who don't know, you can specify strings with either single or double quotes in PHP. If you use single quotes, the string is NOT PARSED and is literally exactly what you put in it. If you use double quotes then it parses for variables and special characters, such as \n for linefeed, and \t for tab. The one thing you need to watch out for is complex variable references (ie object or variable variable references). In this case you can encase the variable reference within curly braces for it to resolve correctly.

code:
$obj = new Object('10');
$var = 20;
echo('$obj->value & $var\n');
echo("\n$obj->value & $var\n");
echo("{$obj->value} & var\n");



Outputs:

$obj->value & $var\n
ObjectReference#->value & 20
10 & 20

Actually I'm not sure what line 2 would be. At any rate, the poit is it would print whatever it prints when you specify to print an object, but it wouldn't reference the varaible it points to because it doesn't evaluate operators without the curly braces.

-jiblet

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 05-26-2001 23:57

Yes but it is SQL code he was refering to, not PHP code

kat
Paranoid (IV) Inmate

From: memphis TN
Insane since: Apr 2001

posted posted 05-27-2001 14:28

if you wanna redirect a page after the headers are sent you can easily echo the

code:
<script language="javascript">
document.location='';
</script>


bit dependent on an if() statement.

this is the method i use for my click counting script.

{ kat ; mmm.. a site! }

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 05-27-2001 15:46

Kat, if some of your visitors have JS turned off that method won't work...

« BackwardsOnwards »

Show Forum Drop Down Menu