Page 1 of 2
Simple PHP script to load data to mysql database.
Posted: Tue Jun 25, 2002 12:10 pm
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> </p>
<p> </p>
</form>
<h1 align="center"> </h1>
</body>
</html>
Am I missing something ??

Posted: Tue Jun 25, 2002 12:28 pm
by enygma
well, you're missing telling us what the error is....
Posted: Tue Jun 25, 2002 12:37 pm
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);
Been there, done that
Posted: Tue Jun 25, 2002 1:35 pm
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?
Posted: Tue Jun 25, 2002 2:31 pm
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.
Working OK now
Posted: Tue Jun 25, 2002 9:22 pm
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> </p>
</body>
</html>
Posted: Wed Jun 26, 2002 1:34 pm
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)) {
$CC = $rowї'CC'];
$Company = $rowї'Company'];
echo "Company Code $CC belongs to the Company $company<br>\n";
}
Parse error: parse error in lookup.php on line 17
Posted: Wed Jun 26, 2002 4:39 pm
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";
}
?>
Posted: Wed Jun 26, 2002 4:54 pm
by enygma
$query = "SELECT * FROM $table WHERE ComanyCode = $CC"
missing a ;
$query = "SELECT * FROM $table WHERE ComanyCode = $CC&q
Posted: Wed Jun 26, 2002 5:02 pm
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.

Posted: Wed Jun 26, 2002 5:58 pm
by Mahmoud
Try $_POST[CC]
Posted: Wed Jun 26, 2002 6:00 pm
by Mahmoud
is the CC a string or an Integer ?
Also try something like this:
$query = "SELECT * FROM ".$table." where CC = ".$_POST[CC];
Posted: Wed Jun 26, 2002 6:13 pm
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їCC].'", "'.$_POSTїContact].'", "'.$_POSTїPhone].'", "'.
$_POSTїEmail].'")';
$result = mysql_query($sql);
Posted: Wed Jun 26, 2002 6:58 pm
by jason
Remember, after mysql_query() do this:
That will help.
parse error
Posted: Wed Jun 26, 2002 7:17 pm
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";
}
?>