Forum Moderators: coopster
This is absoluptly driving me up the wall.
I am sure it is the smallest of things i have missed..
SCENARIO:
Form >> PHP Variables >> DB
PROBLEM:
Data is successfully sent to PHP file (shows on output), but fails to INSERT INTO the database.
MY INSERT CODE:
// hostname or ip of server
$servername='localhost';
// username and password to log onto db server
$dbusername='por_por';
$dbpassword='********';
// name of database
$dbname='por_petition';
$dbtable = $petition;
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
//set query
$query = "insert INTO $dbtable (ID, firstname, lastname, email, area, comments, date, time, ip) VALUES ('','$firstname','$lastname','$email','$area','$comments','$date','$time','$ip')";
//insert data into DB
mysql_query($query);
--------------------------
TABLE STRUCTURE
--------------------------
use por_petition;
SET FOREIGN_KEY_CHECKS=0;
#----------------------------
# Table structure for 001
#----------------------------
drop table if exists `001`;
CREATE TABLE `001` (
`ID` tinyint(11) unsigned NOT NULL auto_increment,
`firstname` varchar(255) NOT NULL default '',
`lastname` varchar(255) NOT NULL default '',
`email` varchar(255) NOT NULL default '',
`area` varchar(255) NOT NULL default '',
`comments` varchar(255) NOT NULL default '',
`date` varchar(255) NOT NULL default '',
`time` varchar(255) NOT NULL default '',
`ip` varchar(255) NOT NULL default '',
PRIMARY KEY (`ID`)
) TYPE=MyISAM;
#----------------------------
# Records for table 001
#----------------------------
Any and all help is appreciatted.
---------------------------------
I notice earlier you have this:
$dbtable = $petition;
So, now does $petition hold any value to pass to the new variable? And my third questions is, why assign something to $dbtable? Why not just the $petition variable in your query?
And welcome to WebmasterWorld GlobalFusion :)
dc
Not a valid MySQL link resource.
You are not sending the query to the database, as the connection is not valid.
Try adding the connection handle in the query:
mysql_query($query, $link);
You could also var_dump($link) to check if it is a MySQL resource.
Hope this can help you
------------------------------------------
FORM PAGE GOES TO 'SUBMIT_PETITION.PHP'
------------------------------------------
-------------------------------------
Heres The Code (SUBMIT_PETITION.PHP)
-------------------------------------
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
//set query
$query = "insert INTO $dbtable (ID, firstname, lastname, email, area, comments, date, time, ip) VALUES ('','$firstname','$lastname','$email','$area','$comments','$date','$time','$ip')";
//insert data into DB
$result = mysql_query($query);
echo "the db error is: ".mysql_error($query);
echo '<br />--------------<br /> '.$query.'<br />--------------------<br /><br />';
mysql_close();
//redirect to confirmation page
include('submit_confirm.php');
------------------------------------------
OUTPUT SHOWN ON 'SUBMIT_CONFIRM.PHP'
------------------------------------------
OutPut Shows as:
Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in /home/por/public_html/petition/submit_petition.php on line 41
the db error is:
--------------
insert INTO 001 (ID, firstname, lastname, email, area, comments, date, time, ip) VALUES ('','John','Doe','SomeWhere','johndoe@nonames.com','In the end.. this will work..','Sunday 20th of March 2005','02:57:25 PM','202.#*$!.xxx.xxx')
--------------------
$petition = 001
$date = Sunday 20th of March 2005
$time = 02:57:25 PM
$ip = 202.172.123.199
$firstname = John
$lastname = Doe
$email = SomeWhere
$area = johndoe@nonames.com
$comments = In the end.. this will work..
---------------------
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
return $link;
---------------------
And i tried in the closing bracket too. but still nothing.
The (out of bracket attempt) prevented my 'output' page from showing the data.
*currently running with the 'return $link' inside the brackets.
[edited by: coopster at 6:39 pm (utc) on Mar. 20, 2005]
[edit reason] removed url per TOS [webmasterworld.com] [/edit]
if you are enclosing a variable name in a single quote, '$var' then try this instead: (enclose in double quotes)
$query = "INSERT INTO.... VALUES ('". $var1 ."', '". $var2 ."');";
Your code should work without any problems, but I find that sometimes this method is better (even if it's more difficult to look at).
No harm in trying... I've never had problems doing it this way.
Pete
Tried this:
-----------
//set query
$query = "INSERT INTO '$dbtable' ('ID', 'firstname', 'lastname', 'email', 'area', 'comments', 'date', 'time', 'ip') VALUES ('', '".$firstname."', '".$lastname."', '".$email."', '".$area."', '".$comments."', '".$date."', '".$time."', '".$ip."')";
//insert data into DB
mysql_query($query);
To No Avail either.. :(
------------------------
And This:
---------
//set query
$query = "INSERT INTO '$dbtable' ('firstname', 'lastname', 'email', 'area', 'comments', 'date', 'time', 'ip') VALUES ('".$firstname."', '".$lastname."', '".$email."', '".$area."', '".$comments."', '".$date."', '".$time."', '".$ip."')";
//insert data into DB
mysql_query($query);
Neither Worked..
----------------
:(
The only time it succeded.. was when i made an insert via PhpMyAdmin.
This is the code it produced:
PHPMYADMIN
__________________________________________________
SQL-query:
INSERT INTO `001` ( `ID` , `firstname` , `lastname` , `email` , `area` , `comments` , `date` , `time` , `ip` )
VALUES (
'', 'test', 'user', 'test@user.com', 'this area', 'some comments', 'some date', 'some time', '#*$!.#*$!.xxx.xxx'
);
__________________________________________________
But if i use that EXACT same code in my php file.. it doesnt INSERT. :( could it be something else?
Does anyone have a better connection string?
$query = "INSERT INTO `$dbtable` (`firstname`, `lastname`, `email`, `area`, `comments`, `date`, `time`, `ip`) VALUES ('".$firstname."', '".$lastname."', '".$email."', '".$area."', '".$comments."', '".$date."', '".$time."', '".$ip."')";
//insert data into DB
mysql_query($query);
Also, with regards to an earlier post, the return $link; should have been INSIDE the function.
dc
I have only one thing to add (ask), is the code you posted EXACTLY as it is in your program? If it is then this is probably your problem:
Look at the END of your insert:
.$ip."')";
Do you see whats missing? At the end of every MySQL query you MUST include a ';' semi-colon... your code doesn't. You have one only for the PHP script, you need one inside the quotes:
.$ip."');";
See? Am I crazy or is this the fix?
The query string should not end with a semicolon.
mysql_query() [php.net]