PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.comp.lang.php > having problem getting php to connect
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
having problem getting php to connect

Réponse
 
LinkBack Outils de la discussion
Vieux 24/06/2007, 21h33   #1
gagal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut having problem getting php to connect

Warning: mysql_query(): supplied argument is not a valid MySQL-Link
resource in /home/smith/public_html/personal/ab_functions.php on line
30

line 30 is [mysql_query ($sql, $connect) or die(mysql_error());]

function addPerson ()
{
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
if( $_POST{'dobMonth'} and $_POST{'dobDay'})
$dob = ($_POST{'dobMonth'}.'/'.$_POST{'dobDay'}.'/'.
$_POST{'dobYear'});
else
$dob = ' ';
$category = $_POST['category'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$telephone = $_POST['telephone'];
$cellphone = $_POST['cellphone'];
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$aimid = $_POST['aimid'];
$url = $_POST['url'];
$notes = $_POST['notes'];

$sql = "INSERT INTO $db_table4
(firstName, lastName, dob, category, address1, address2, city,
state, zipcode, telephone, cellphone, email1, email2, aimid, url,
notes)
VALUES
('$firstName', '$lastName', '$dob', '$category', '$address1',
'$address2', '$city', '$state', '$zipcode', '$telephone',
'$cellphone', '$email1', '$email2', '$aimid', '$url', '$notes')";

mysql_query ($sql, $connect) or die(mysql_error());

echo "added";
}

function is called on this page

HTML>
<HEAD>
<?PHP include("includes/headsectionp.txt")?>
</HEAD>

<BODY>
<?PHP include("includes/connect.txt");?>
<?PHP include("ab_functions.php");?>
<?PHP
if(isset($_POST['do_addperson']))
{
addPerson ();
}
?>

  Réponse avec citation
Vieux 24/06/2007, 21h34   #2
gagal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: having problem getting php to connect

my connect page is

<?PHP
$db_server = "localhost";
$db_user = " ";
$db_pass = " ";
$db_name = "smith_website";
$db_table = "blog_category";
$db_table1 = "blog_entry";
$db_table2 = "quotes";
$db_table3 = "ab_category";
$db_table4 = "ab_person";

$connect = mysql_connect($db_server, $db_user, $db_pass) OR DIE ('I
can not connect to server because: ' . mysql_error());

mysql_select_db($db_name, $connect) OR DIE ('I can not connect to
database because: ' . mysql_error());
?>

  Réponse avec citation
Vieux 24/06/2007, 22h09   #3
peter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: having problem getting php to connect

> function addPerson ()
> {
> $firstName = $_POST['firstName'];
> $lastName = $_POST['lastName'];
> if( $_POST{'dobMonth'} and $_POST{'dobDay'})
> $dob = ($_POST{'dobMonth'}.'/'.$_POST{'dobDay'}.'/'.
> $_POST{'dobYear'});
> else
> $dob = ' ';
> $category = $_POST['category'];
> $address1 = $_POST['address1'];
> $address2 = $_POST['address2'];
> $city = $_POST['city'];
> $state = $_POST['state'];
> $zipcode = $_POST['zipcode'];
> $telephone = $_POST['telephone'];
> $cellphone = $_POST['cellphone'];
> $email1 = $_POST['email1'];
> $email2 = $_POST['email2'];
> $aimid = $_POST['aimid'];
> $url = $_POST['url'];
> $notes = $_POST['notes'];
>
> $sql = "INSERT INTO $db_table4
> (firstName, lastName, dob, category, address1, address2, city,
> state, zipcode, telephone, cellphone, email1, email2, aimid, url,
> notes)
> VALUES
> ('$firstName', '$lastName', '$dob', '$category', '$address1',
> '$address2', '$city', '$state', '$zipcode', '$telephone',
> '$cellphone', '$email1', '$email2', '$aimid', '$url', '$notes')";
>
> mysql_query ($sql, $connect) or die(mysql_error());
>
> echo "added";
> }
>
> function is called on this page
>
> HTML>
> <HEAD>
> <?PHP include("includes/headsectionp.txt")?>
> </HEAD>
>
> <BODY>
> <?PHP include("includes/connect.txt");?>
> <?PHP include("ab_functions.php");?>
> <?PHP
> if(isset($_POST['do_addperson']))
> {
> addPerson ();
> }
> ?>


In your function you are using the variable $connect. This variable is not
in the scope of the function.

You have 2 options you can do this:-

function addPerson ()
{
global $connect

I you change the start of your function to this then you will be able to use
the $connect variable within your function.

The better option however is to do this:-

function addPerson ($connect)
{

and when calling the function do addPerson($connect), this makes the
$connect variable a parameter of the function. The reason the $_POST array
works without doing this is because they are already global.
http://www.php.net/global


  Réponse avec citation
Vieux 24/06/2007, 22h22   #4
gagal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: having problem getting php to connect

New problem now
I made change and got:

Warning: Missing argument 1 for addperson() in /home/smith/public_html/
personal/ab_functions.php on line 20
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '(firstName, lastName, dob, category, address1, address2, city,
state, zipcode, t' at line 2

I'm sure insert code works because if I just run it from page with
form it is ok.

This is the new add function

function addPerson ($connect)
{
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
if( $_POST{'dobMonth'} and $_POST{'dobDay'})
$dob = ($_POST{'dobMonth'}.'/'.$_POST{'dobDay'}.'/'.
$_POST{'dobYear'});
else
$dob = ' ';
$category = $_POST['category'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$telephone = $_POST['telephone'];
$cellphone = $_POST['cellphone'];
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$aimid = $_POST['aimid'];
$url = $_POST['url'];
$notes = $_POST['notes'];


$sql = "INSERT INTO $db_table4
(firstName, lastName, dob, category, address1, address2, city,
state, zipcode, telephone, cellphone, email1, email2, aimid, url,
notes)
VALUES
('$firstName', '$lastName', '$dob', '$category', '$address1',
'$address2', '$city', '$state', '$zipcode', '$telephone',
'$cellphone', '$email1', '$email2', '$aimid', '$url', '$notes')";

mysql_query ($sql) or die(mysql_error());

echo "$db_name";
echo "added";
}


  Réponse avec citation
Vieux 24/06/2007, 22h41   #5
peter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: having problem getting php to connect

> New problem now
> I made change and got:
>
> Warning: Missing argument 1 for addperson() in /home/smith/public_html/
> personal/ab_functions.php on line 20
> You have an error in your SQL syntax; check the manual that
> corresponds to your MySQL server version for the right syntax to use
> near '(firstName, lastName, dob, category, address1, address2, city,
> state, zipcode, t' at line 2
>
> I'm sure insert code works because if I just run it from page with
> form it is ok.


Did you change the following code as instructed:-

if(isset($_POST['do_addperson']))
{
addPerson ();
}

to the following:-

if(isset($_POST['do_addperson']))
{
addPerson ($connect);
}

The error message you posted suggests you did not.


  Réponse avec citation
Vieux 24/06/2007, 22h42   #6
peter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: having problem getting php to connect


> $sql = "INSERT INTO $db_table4
> (firstName, lastName, dob, category, address1, address2, city,
> state, zipcode, telephone, cellphone, email1, email2, aimid, url,
> notes)
> VALUES
> ('$firstName', '$lastName', '$dob', '$category', '$address1',
> '$address2', '$city', '$state', '$zipcode', '$telephone',
> '$cellphone', '$email1', '$email2', '$aimid', '$url', '$notes')";


Also just noticed I presume that line is meant to actually read:-

$sql = "INSERT INTO $db_table

you have an eroneous 4 at the end of the table name


  Réponse avec citation
Vieux 24/06/2007, 22h46   #7
gagal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: having problem getting php to connect

Sorry, I didn't add it in original file. After adding it, i got:

You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '(firstName, lastName, dob, category, address1, address2, city,
state, zipcode, t' at line 2



function addPerson ($connect)
{
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
if( $_POST{'dobMonth'} and $_POST{'dobDay'})
$dob = ($_POST{'dobMonth'}.'/'.$_POST{'dobDay'}.'/'.
$_POST{'dobYear'});
else
$dob = ' ';
$category = $_POST['category'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$telephone = $_POST['telephone'];
$cellphone = $_POST['cellphone'];
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$aimid = $_POST['aimid'];
$url = $_POST['url'];
$notes = $_POST['notes'];


$sql = "INSERT INTO $db_table4
(firstName, lastName, dob, category, address1, address2, city,
state, zipcode, telephone, cellphone, email1, email2, aimid, url,
notes)
VALUES
('$firstName', '$lastName', '$dob', '$category', '$address1',
'$address2', '$city', '$state', '$zipcode', '$telephone',
'$cellphone', '$email1', '$email2', '$aimid', '$url', '$notes')";

mysql_query ($sql) or die(mysql_error());

echo "$db_name";
echo "added";
}


<?PHP include("includes/connect.txt");?>
<?PHP include("ab_functions.php");?>
<?PHP
if(isset($_POST['do_addperson']))
{
addPerson ($connect);
}
?>

  Réponse avec citation
Vieux 24/06/2007, 22h48   #8
gagal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: having problem getting php to connect

On Jun 24, 5:42 pm, "peter" <sub...@flexiwebhost.com> wrote:
> > $sql = "INSERT INTO $db_table4
> > (firstName, lastName, dob, category, address1, address2, city,
> > state, zipcode, telephone, cellphone, email1, email2, aimid, url,
> > notes)
> > VALUES
> > ('$firstName', '$lastName', '$dob', '$category', '$address1',
> > '$address2', '$city', '$state', '$zipcode', '$telephone',
> > '$cellphone', '$email1', '$email2', '$aimid', '$url', '$notes')";

>
> Also just noticed I presume that line is meant to actually read:-
>
> $sql = "INSERT INTO $db_table
>
> you have an eroneous 4 at the end of the table name


$db_server = "localhost";
$db_user = "smith_admin";
$db_pass = "password";
$db_name = "smith_website";
$db_table = "blog_category";
$db_table1 = "blog_entry";
$db_table2 = "quotes";
$db_table3 = "ab_category";
$db_table4 = "ab_person";

$connect = mysql_connect($db_server, $db_user, $db_pass) OR DIE ('I
can not connect to server because: ' .mysql_errno(). " -
" .mysql_error());
mysql_select_db($db_name, $connect) OR DIE ('I can not connect to
server because: ' .mysql_errno(). " - " .mysql_error());

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h30.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15028 seconds with 16 queries