Forum Moderators: coopster

Message Too Old, No Replies

Php/mysql Insert Help!

         

angst

4:28 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



hello,
I'm trying to do a simple INSERT with php/mysql,

here is my code:

<?php
include "inc/dbcfg.inc";

$FirstName = $_REQUEST['FirstName'] ;
$LastName = $_REQUEST['LastName'] ;

$link = mysql_connect($SqlHost, $SqlUser, $SqlPass) or die('Login details incorrect');

if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($SqlDb , $link)
or die("Couldn't open $SqlDb: ".mysql_error());
mysql_query ("INSERT INTO pre-signups (FirstName, LastName) VALUES ('$FirstName','$LastName')");
mysql_close($link);

?>

I'm not getting any errors, it seems to connect to the db fine, and i can pull data ok. but something is wrong with my code for inserting data,
can anyone please tell me what i've done wrong here?

thanks in advance for your time!
-Ken

sned

4:39 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



Hi Ken, everything looks ok on first glance ... one thing to check would be permissions on the database .. does the username your using have permissions to insert?

-sned

angst

4:50 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



hmm, good question.

I'm using mysql and apache on windows just for testing on my local work station.

I'll take a look into that, and post back.

thanks!

-Ken

angst

4:54 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



ok, i do have the right permissions,

I just put everything into one file to make it simple, and made the values static to avoid another other problems like passing form data..

my code:

<?
$DBhost = "domain.ca";
$DBuser = "sql";
$DBpass = "sql";
$DBName = "work";
$table = "pre-signups";

mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

@mysql_select_db("$DBName") or die("Unable to select database $DBName");

$sqlquery = "INSERT INTO $table (FirstName, LastName) VALUES(test,test)";

$results = mysql_query($sqlquery);

mysql_close();
?>

and still nothing at all, no errors, and no data when i open the table in mysql?

any other ideas?

-Ken

sned

5:09 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



Hmm, in your test case ...

$sqlquery = "INSERT INTO $table (FirstName, LastName) VALUES(test,test)";

I think the (test,test) needs to have quotes around it : ('test','test') ... I think that should work.

Are any errors being reported anywhere?

-sned

angst

5:13 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



no i tried that too,
and i'm not getting any errors at all.

like i said, pulling data from the db is no problem, but inserting is just not working. i'm very confused.

-Ken

angst

6:07 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



YES!
i figured it out!

mysql or php or whatever doesn't like "-" in a table name!

as soon as i removed that it all started working!

I feel so dumb!

thanks again for your time!

I'm still learning this php stuff, but getting better everyday!

:-)

-Ken