Simple PHP script to load data to mysql database.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

offsite
Forum Newbie
Posts: 8
Joined: Tue Jun 25, 2002 12:10 pm
Location: Colchester, CT

Simple PHP script to load data to mysql database.

Post by offsite »

THAT WON"T WORK WHY?
PHP Script (add.php)
<?php

$DBhost = "localhost";
$DBuser = "me";
$DBpass = "mypass";
$DBName = "mydatabase";

$table = "table";

mysql_connect($DBhost, $DBuser, $DBpass) or die("News=Uh Oh, Could not connect");
mysql_select_db($DBName, $Connect);

$result = mysql_query("INSERT INTO $table (CC, Contact, Phone, Email) VALUES ('$CC', '$Contact', '$Phone', '$Email')", $Connect);

Print "Status=Record has been added";
?>

Form information
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<h1 align="center">Install / Upgrade Client Information - Input Form </h1>
<form name="Clients" method="post" action="add.php">
<table width="75%" border="1" align="center">
<tr>
<td height="33">Company Code: </td>
<td height="33">
<input type="text" name="CC" maxlength="3">
</td>
<td height="33">Contact: </td>
<td height="33">
<input type="text" name="Contact">
</td>
</tr>
<tr>
<td>Phone Number:</td>
<td>
<input type="text" name="Phone">
<td>E-Mail</td>
<td>
<input type="text" name="Email">
</td>
</tr>
</table>
<p align="center">
<input type="submit" name="Submit" value="Submit">
</p>
<p>&nbsp; </p>
<p>&nbsp;</p>
</form>
<h1 align="center">&nbsp;</h1>
</body>
</html>


Am I missing something ?? :oops:
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post by enygma »

well, you're missing telling us what the error is....
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

and it is worth putting some print statements in to see the value of your variables to make sure they contain what you think they do.

$DBhost = "localhost";
$DBuser = "me";
$DBpass = "mypass";
$DBName = "mydatabase";

$table = "table";

mysql_connect($DBhost, $DBuser, $DBpass) or die("News=Uh Oh, Could not connect");
mysql_select_db($DBName, $Connect);


print "CC: $CC<br>";
print "Contact: $Contact"; etc...


$result = mysql_query("INSERT INTO $table (CC, Contact, Phone, Email) VALUES ('$CC', '$Contact', '$Phone', '$Email')", $Connect);
offsite
Forum Newbie
Posts: 8
Joined: Tue Jun 25, 2002 12:10 pm
Location: Colchester, CT

Been there, done that

Post by offsite »

I added print $CC etc for the input fields used in the from andl they all came back with the data entered on the form.
I get no errors when running the script. It prints as specified ie: "record added to database"
When i open the database using myphpadmin, no data or rows get added to the table.
I can insert a record using mysql>insert into table etc. with no problem.

does the stntax look proper?
Data
Forum Newbie
Posts: 15
Joined: Tue Jun 25, 2002 4:14 am
Location: Aberdeen, Scotland

Post by Data »

Try removing the '$result =' from the mysql_query.

You'll also want an 'if ( $submit )' around the mysql_query and the text that gets printed.
offsite
Forum Newbie
Posts: 8
Joined: Tue Jun 25, 2002 12:10 pm
Location: Colchester, CT

Working OK now

Post by offsite »

Thanks for all your help.

I removed the $result= and iit worked fine.
Now I have a new issue.
How do I see the data.

I created a lookup.php file ..

<?php

$DBhost = "localhost";
$DBuser = "me";
$DBpass = "pw";
$DBName = "db";

$table = "recaps";

$Connect = mysql_connect($DBhost, $DBuser, $DBpass) or die("News=Uh Oh, Could not connect");
mysql_select_db($DBName, $Connect);

mysql_query("SELECT * FROM $table WHERE CompanyCode = $CC)", $Connect);

This seems to work, with no errors, but I don't get any results from the query. Did I leave something out?

This is my 1st try at this, it's interesting, but a little frustrating.

Thanks again for all your help.

ps: This is the from I'm using to extract the data from the database.

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<p>Query the database</p>
<form name="form1" method="post" action="lookup.php">
<p>Enter the Company code:
<input type="text" name="CC">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
<p>&nbsp; </p>
</body>
</html>
honkyinc
Forum Newbie
Posts: 19
Joined: Tue Jun 04, 2002 10:30 am
Location: Maryland, USA

Post by honkyinc »

Code: Select all

$query = "SELECT * FROM table WHERE condition"
$result = mysql_query($query, $connect)
    or die("query was not successful);

while($row = mysql_fetch_array($result)) &#123;
   $CC = $row&#1111;'CC'];
   $Company = $row&#1111;'Company'];

   echo "Company Code $CC belongs to the Company $company<br>\n";
&#125;
offsite
Forum Newbie
Posts: 8
Joined: Tue Jun 25, 2002 12:10 pm
Location: Colchester, CT

Parse error: parse error in lookup.php on line 17

Post by offsite »

I used honkyinc's code and got this error:

Here is the code I used:

<?php

$DBhost = "host";
$DBuser = "user";
$DBpass = "PW";
$DBName = "Name";

$table = "recaps";

$Connect = mysql_connect($DBhost, $DBuser, $DBpass) or die("News=Uh Oh, Could not connect");
mysql_select_db($DBName, $Connect);
$query = "SELECT * FROM $table WHERE ComanyCode = $CC"
$result = mysql_query($query, $connect)
or die("query was not successful);

while($row = mysql_fetch_array($result)) {
$CC = $row['CompanyCode'];
This is line 17
$Company = $row['CompanyName'];

echo "Company Code $CC belongs to the Company $companyName run by $$PR_Contact<br>\n";
}
?>
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post by enygma »

$query = "SELECT * FROM $table WHERE ComanyCode = $CC"

missing a ;
offsite
Forum Newbie
Posts: 8
Joined: Tue Jun 25, 2002 12:10 pm
Location: Colchester, CT

$query = "SELECT * FROM $table WHERE ComanyCode = $CC&q

Post by offsite »

Thanks again for the quick reply,

Added the semi-colon..
$query = "SELECT * FROM $table WHERE ComanyCode = $CC";

Same error.

Even tried to substutute the row number instead of the field name, (on line 17)

same results.

:oops:
Mahmoud
Forum Newbie
Posts: 13
Joined: Tue Jun 25, 2002 5:21 pm

Post by Mahmoud »

Try $_POST[CC]
Last edited by Mahmoud on Wed Jun 26, 2002 6:14 pm, edited 1 time in total.
Mahmoud
Forum Newbie
Posts: 13
Joined: Tue Jun 25, 2002 5:21 pm

Post by Mahmoud »

is the CC a string or an Integer ?

Also try something like this:
$query = "SELECT * FROM ".$table." where CC = ".$_POST[CC];
Last edited by Mahmoud on Wed Jun 26, 2002 6:14 pm, edited 1 time in total.
Mahmoud
Forum Newbie
Posts: 13
Joined: Tue Jun 25, 2002 5:21 pm

Post by Mahmoud »

something like :
$result = mysql_query("INSERT INTO $table (CC, Contact, Phone, Email) VALUES ('$CC', '$Contact', '$Phone', '$Email')", $Connect);

should be like this:

Code: Select all

$sql = 'INSERT INTO '.$table.' (CC, Contact, Phone, Email) VALUES ("'.
$_POST&#1111;CC].'", "'.$_POST&#1111;Contact].'", "'.$_POST&#1111;Phone].'", "'.
$_POST&#1111;Email].'")';
$result = mysql_query($sql);
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Remember, after mysql_query() do this:

Code: Select all

echo mysql_error();
That will help.
offsite
Forum Newbie
Posts: 8
Joined: Tue Jun 25, 2002 12:10 pm
Location: Colchester, CT

parse error

Post by offsite »

Jason,

That helped..heres what I received back:

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /www/dougmillard.com/web/lanteam/lookup.php on line 18

line 18 = $CC = $row['CC'];

I changed 'CC' to 'CompanyCode', which is the field name for row 1
to see if that worked. received this error, but not expecting 'T_STRING' or `T_VARIABLE' or `T_NUM_STRING'

Parse error: parse error in /www/dougmillard.com/web/lanteam/lookup.php on line 17

line 17= while($row = mysql_fetch_array{
Complete script
<?php

$DBhost = "localhost";
$DBuser = "dougmillard2";
$DBpass = "hookloft15";
$DBName = "dougmillard2";

$table = "recaps";

$Connect = mysql_connect($DBhost, $DBuser, $DBpass) or die("News=Uh Oh, Could not connect");
mysql_select_db($DBName, $Connect);

mysql_query("SELECT * FROM recaps WHERE CompanyCode = $CC)", $Connect);
//$query = "SELECT * FROM recaps WHERE CompanyCode = $CC"
//$result = mysql_query($query, $connect)
// or die("query was not successful);
while($row = mysql_fetch_array{
$CC = $row['CompanyCode'];
$Company = $row['CompanyName'];
$Payroll_Contact = $row['PR_Contact'];
echo "Company Code $CC belongs to the Company $company run by $$Payroll_Contact<br>\n";
}
?>
Post Reply