Another n00b messing it up

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Sleeve
Forum Newbie
Posts: 17
Joined: Wed Jan 22, 2003 12:39 am

Another n00b messing it up

Post by Sleeve »

Hi guys,
First I want to say that from browsing through some of your posts I can tell it takes brains to PHP. Most of You guys have it! I'm excited to learn!
I am literally 3 hours into MySQL and PHP and have hit a bit of a snag. Maybe my post is premature, but I'm sure there is someone in this forum that can offer great help with a simple and elemenrty script such as this:

Code: Select all

<?php

 mysql_connect ("", "", "");

$database = "incomming";

@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO requests VALUES ('$FirstName', '$LastName', '$Title', 

'$SalonName','$Address1', '$Address2', '$City', '$State', '$Zip', $Phone', '$Email', 

'$YesNo', '$Date', '$Comments')";

$result = mysql_query($query);

  mysql_close ();

?>
As you can see I'm trying to log information posted on a website to a MySQL database. I am almost positive I have created the table correctly but will post it anyway:

Code: Select all

+---------------+--------------+------+-----+---------+-------+
| Field         | Type         | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| first         | varchar(15)  | YES  |     | NULL    |       |
| last          | varchar(25)  | YES  |     | NULL    |       |
| title         | varchar(15)  | YES  |     | NULL    |       |
| salon_name    | varchar(20)  | YES  |     | NULL    |       |
| address1      | varchar(20)  | YES  |     | NULL    |       |
| address2      | varchar(20)  | YES  |     | NULL    |       |
| city          | varchar(15)  | YES  |     | NULL    |       |
| state         | varchar(15)  | YES  |     | NULL    |       |
| zip           | varchar(12)  | YES  |     | NULL    |       |
| phone         | varchar(25)  | YES  |     | NULL    |       |
| email         | varchar(25)  | YES  |     | NULL    |       |
| requested_cat | varchar(20)  | YES  |     | NULL    |       |
| comments      | varchar(255) | YES  |     | NULL    |       |
| date          | varchar(10)  | YES  |     | NULL    |       |
+---------------+--------------+------+-----+---------+-------+
I sure hope this is okay...Sorry for being a n00b :(

The problem is that there are no errors displayed when the script is run with the variables assigned but when I select * from database; nothing seems to have been entered.
Once I find a solution, I'll regain my confidence...Thanks for your help.

Kind Regards,

Sleeve
User avatar
gyardleydn
Forum Commoner
Posts: 27
Joined: Tue Dec 03, 2002 8:27 am

Post by gyardleydn »

Just some general tips here

1. Check that error reporting is on. You can do this maually [Listing] before the error could of occurred, or in the php.ini file to apply to all scipts.

2. Echo values that may be incorrect to see if they are what you expect. So in this case echo $query. If it is what you expect, try running it as the same user as you connect to here in the mysql shell.

3. Also see mysql_affected_rows() and mysql_info()

Code: Select all

<?php
ini_set('display_errors', TRUE); 
ini_set('error_reporting', E_ALL); 
?>
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

If your are entering the information from a form be sure to define the form inputs as variables:

eg:

$FirstName = $_POST['firstname'];

/* Where ['firstname'] is the name of the form field. */

Do that for them all, before your start your mysql_query's

also, try not closing your mysql_query. No-one i know closes it, and its not really relevant.
Sleeve
Forum Newbie
Posts: 17
Joined: Wed Jan 22, 2003 12:39 am

Post by Sleeve »

Thanks for your help.
Great tips gyardleydn. Thanks for the details evilcoder but Nothing seems to work.
I added:

Code: Select all

$rSQL = mysql_query($query) ;
echo (mysql_affected_rows()?"success":"failure");

It returns 'success' but still no data written to the table.
Maybe I'm jumping ahead, but all the tutorials I've read on this simple operation are the same. I have followed them, but to no avail.
Strangest thing... I'm able to use PHP Admin to input data into MySQL but my script absolutly refuses to. I have tried sending the variables using POST and GET, and I have tried Hard Coding them directly into PHP. Still nothing...
Any other ideas? Thank you in advance. Helping a n00b is fun, Right?
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

This may seem simple, but is your database connection correct?
Sleeve
Forum Newbie
Posts: 17
Joined: Wed Jan 22, 2003 12:39 am

Post by Sleeve »

If it were't whould I still be able to use phpMyAdmin to write to the DB and not my script?
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

are you logging into them with the same details?
Sleeve
Forum Newbie
Posts: 17
Joined: Wed Jan 22, 2003 12:39 am

Post by Sleeve »

Yes, It's on a test server with null passwords. I know so little about MySQL at this point but I am using the default login which I thought was ODBC@localhost with no pass.
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

can i see your include that handles the database connection?
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

sorry was thinking about another post.

Umm is your database name all correct?
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

ok after

$result = mysql_query( $query );

put this:

if ( !$result )
{
echo "Something went wrong";
}
else
{
echo " Everything went OK, apparently";
}
Sleeve
Forum Newbie
Posts: 17
Joined: Wed Jan 22, 2003 12:39 am

Post by Sleeve »

I thought mysql_connect( "","","" ) assumed localhost, root with no pass? This is what I am using ...is it okay?
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

No you have to define localhost
Sleeve
Forum Newbie
Posts: 17
Joined: Wed Jan 22, 2003 12:39 am

Post by Sleeve »

Okay... I added mysql_connect ("127.0.0.1", "root", ""); and the code your last thread. this is the most progress I have made since I started. It returns 'Something went wrong' Here is my entire revised and apparently incorrect code:

Code: Select all

<?php

 mysql_connect ("127.0.0.1", "root", "");

$database = "incomming";

@mysql_select_db($database) or die("Could not connect: " . mysql_error());

$query = "INSERT INTO 'requests' 'INSERT INTO `requests` ( `first` , `last` , `title` , 

`salon_name` , `address1` , `address2` , `city` , `state` , `Zip` , `phone` , `email` , 

`requested_cat` , `comments` , `date` ) VALUES ('$FirstName', '$LastName', '$Title', 

'$SalonName','$Address1', '$Address2', '$City', '$State', '$Zip', '$Phone', 

'$Email','$YesNo', '$Date', '$Comments')";

$rSQL = mysql_query($query) ;
echo (mysql_affected_rows()?"success":"failure");


$result = mysql_query($query);

if ( !$result ) 
&#123; 
echo "Something went wrong"; 
&#125; 
else 
&#123; 
echo " Everything went OK, apparently"; 
&#125;





  mysql_close ();

?>
Thank you for sticking with me here. I owe you :)
elipollak
Forum Newbie
Posts: 22
Joined: Sun Jan 19, 2003 10:23 pm

Post by elipollak »

dun know if it's of any help ...but I always use :

$db_con = mysql_connect("localhost") OR die ("DB CONNECT Error");
Post Reply