Page 1 of 2

Another n00b messing it up

Posted: Wed Jan 22, 2003 12:39 am
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

Posted: Wed Jan 22, 2003 2:32 am
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); 
?>

Posted: Wed Jan 22, 2003 3:30 am
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.

Posted: Wed Jan 22, 2003 3:48 pm
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?

Posted: Wed Jan 22, 2003 3:58 pm
by evilcoder
This may seem simple, but is your database connection correct?

Posted: Wed Jan 22, 2003 4:11 pm
by Sleeve
If it were't whould I still be able to use phpMyAdmin to write to the DB and not my script?

Posted: Wed Jan 22, 2003 4:13 pm
by evilcoder
are you logging into them with the same details?

Posted: Wed Jan 22, 2003 4:18 pm
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.

Posted: Wed Jan 22, 2003 4:20 pm
by evilcoder
can i see your include that handles the database connection?

Posted: Wed Jan 22, 2003 4:21 pm
by evilcoder
sorry was thinking about another post.

Umm is your database name all correct?

Posted: Wed Jan 22, 2003 4:25 pm
by evilcoder
ok after

$result = mysql_query( $query );

put this:

if ( !$result )
{
echo "Something went wrong";
}
else
{
echo " Everything went OK, apparently";
}

Posted: Wed Jan 22, 2003 4:37 pm
by Sleeve
I thought mysql_connect( "","","" ) assumed localhost, root with no pass? This is what I am using ...is it okay?

Posted: Wed Jan 22, 2003 4:39 pm
by evilcoder
No you have to define localhost

Posted: Wed Jan 22, 2003 4:54 pm
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 :)

Posted: Wed Jan 22, 2003 5:58 pm
by elipollak
dun know if it's of any help ...but I always use :

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