Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
matthiasone
Forum Contributor
Posts: 117 Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:
Post
by matthiasone » Mon Jul 22, 2002 12:14 pm
I am wanting to insert data from users to a mySQL database. When I run the code I don't get any errors messages.....but when the result is tested it is false. I can run the query that is generated in phpMyAdmin 2.2.5 and it works perfectly. Any ideas?
Code: Select all
$conn = db_connect();
if(!$conn)
die("Error: could not connect to database. Please try again later.");
mysql_select_db("lufkinorg");
$query = "insert into contributors values ("02G9100261", "Matt", "Phillips".
"", "1026 Hyde Rd", "Huntington", "TX", "75949", "936.422.4241", "mphillips@lufkin.org")";
$result = mysql_query($query, $conn);
if ($results)
echo mysql_affected_rows()."entered";
else
echo "no result";
fatalcure
Forum Contributor
Posts: 141 Joined: Thu Jul 04, 2002 12:57 pm
Contact:
Post
by fatalcure » Mon Jul 22, 2002 12:18 pm
uhh, try not using the escape characters in your query:
$query = "INSERT INTO contributors VALUES ('02G9100261', 'Matt', 'Phillips', ' ', '1026 Hyde Rd', 'Huntington', 'TX', '75949', '936.422.4241', '
mphillips@lufkin.org ')";
and your if statement sould be if ($result) not $results
matthiasone
Forum Contributor
Posts: 117 Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:
Post
by matthiasone » Mon Jul 22, 2002 12:24 pm
already tried that........but thanks for the suggestion.
Here is the connection function I wrote.......this the is same function I use to pull data out of the database which works..........
Code: Select all
function db_connect()
{
$result = mysql_pconnect("localhost", "webbot", "sm1l3");
if (!$result)
return false;
if (!mysql_select_db("lufkinorg"))
return false;
return $result;
}
fatalcure
Forum Contributor
Posts: 141 Joined: Thu Jul 04, 2002 12:57 pm
Contact:
Post
by fatalcure » Mon Jul 22, 2002 1:07 pm
use this for ur db connection:
Code: Select all
function query_db($query) {
$db = "db";
$dbusername = "username";
$dbpassword = "password";
$err = "";
$connection = mysql_connect("localhost",$dbusername,$dbpassword) or $err .= mysql_error();
$database = mysql_select_db($db, $connection) or $err .= mysql_error();
$result = mysql_query($query) or $err .= mysql_error();
echo $err;
return $result;
mysql_close($connection);
}
then just use:
Code: Select all
$query = "INSERT INTO contributors VALUES ('02G9100261', 'Matt', 'Phillips', ' ', '1026 Hyde Rd', 'Huntington', 'TX', '75949', '936.422.4241', 'mphillips@lufkin.org')";
$result = query_db($query);
if ($result) {
//code
} else {
//code
}
matthiasone
Forum Contributor
Posts: 117 Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:
Post
by matthiasone » Mon Jul 22, 2002 2:16 pm
That worked...........thanks Fatalcure