Jump to content


Photo

newbie = cannot connect to DB on localhost


  • Please log in to reply
10 replies to this topic

#1 greggustin

greggustin
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 12 July 2006 - 01:35 AM

I am brand new at this
ok with HTML // learning PHP
trying the most SIMPLE tests for my self
made at DB using phpMyAdmin installed on my PC (WinXP)
c:\wamp\mysql
c:\wamp\php
c:\wamp\phpMyAdmin
and
c:\wamp\www = localhost
(ie i put my index.html in that folder and 'surf' to localhost and all is fine :) )
I put this file in same directory (eg my2.html)
and surf to  [http://localhost/my2.html ]
and it looks ok
here is html file
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<form action="process.php" method="post">
Your Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br>
Location: <input type="text" name = "location"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

and here is the code for process.php
<head>
<title>Untitled</title>
</head>
<body>
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$location=$_POST['location'];
MySQL_connect("localhost:/MySQL/data/test1", "root", "")or die ('I cannot connect to the database because: ' . MySQL_error());
MySQL_select_db("test1");
MySQL_query("INSERT INTO `test1table` VALUES ('$name', '$email', '$location')");
Print "Your information has been successfully added to the database.";
?>
</body>
</html>

it also appears ok in browser
I get this as echo on screen (ie NOT the error message)
Your information has been successfully added to the database.

I am guessing the problem is in the HOST field above in the 'msql_connect' line
and yes - there is a DB called "test1" and a table called "test1table.frm"
which is a folder called test1 which is in the data folder (as above)

I have been reading tutorials for hours
they say DB "locations" (and something about 'socks' :P )
so = what is syntax for my location???

2nd question - as this is not working = why does it show the 'success' echo when I submit the data?
(I open the DB using phpMyAdmin and there are no new data rows)



#2 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 12 July 2006 - 01:51 AM

Change:
MySQL_connect("localhost:/MySQL/data/test1", "root", "")or die ('I cannot connect to the database because: ' . MySQL_error());
To
MySQL_connect("localhost", "root", "")or die ('I cannot connect to the database because: ' . MySQL_error());


Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#3 robos99

robos99
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 12 July 2006 - 02:04 AM

put "or die" after all your sql functions. my guess is either your select_db is failing or the query itself is failing.

you can also test to see if MySQL successfully inserted your data by doing this:
if(mysql_affected_rows($connection)==0){
 echo "Insertion failed.";
 }

Also put this in at the top of your script when debugging:
error_reporting(E_ALL);

This should give you some info on what is going wrong.

To answer your other question, the reason it's saying that it added the info is because you don't have an "or die" statement after your query or select_db. So even if it did fail, PHP just continues and echos whatever you tell it to. That's why you want to put "or die" after everything...actually it'd be even better to write an error handling function but that may be over your head.

#4 greggustin

greggustin
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 12 July 2006 - 02:47 AM

Change:

MySQL_connect("localhost:/MySQL/data/test1", "root", "")or die ('I cannot connect to the database because: ' . MySQL_error());
To
MySQL_connect("localhost", "root", "")or die ('I cannot connect to the database because: ' . MySQL_error());


well - this was my 1st try - and it did not work
thanks anyway

hmm
as local host =
c:\wamp\www
and MySql is up a level then down
eg
c:\wamp\MySql
how does the connect command know where to find the DB?
should I copy it 'under' the localhost folder somewhere?


#5 greggustin

greggustin
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 12 July 2006 - 03:06 AM

I added the 'or die' after each line as suggested and got this after I hit submit

I cannot connect to the database because: Column count doesn't match value count at row 1

my table has 4 fields, (id, name, email and location)
'id' is auto increment = so I did not put that field name in my php file
should I?
and if so, how should I edit my insert values code?



#6 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 12 July 2006 - 03:07 AM

Last solution, try to use wamp -> http://www.wampserver.com/en
You just put "localhost" and it will be working.

mysql_connect("localhost", "root","")or die(mysql_error());
mysql_select_db("database-name-here") or die(mysql_error());


Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#7 greggustin

greggustin
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 12 July 2006 - 03:31 AM

Last solution, try to use wamp -> http://www.wampserver.com/en
You just put "localhost" and it will be working.

MySQL_connect("localhost", "root","")or die(MySQL_error());
MySQL_select_db("database-name-here") or die(MySQL_error());


hehe
I am already using wamp server :)
anyway got it just about working
had to add another field called ID
but I had to hard code the value to make it work and have to increment it by hand
$id=7;
I am sure there is some code to make it do that automatically

the real purpose of this exercise is to allow web site visitors the option of checkig off check boxes
and therfore = every field will not be 'filled'
will that be a problem?




#8 greggustin

greggustin
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 12 July 2006 - 03:48 AM

generic question = when you do the 'query insert'
do you have to have a variable for EVERY one of the columns in the table?
if so. seems odd ?


#9 criticalsystems

criticalsystems
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 12 July 2006 - 04:01 AM

you do not have to have a variable for ever column you can just specify the cols that you are going to give values for in the SQL statement.

eg INSERT into table (col1,col2,col3) values (col1val,col2val,col3val);

#10 saikiran

saikiran
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 12 July 2006 - 05:59 AM

hi,

this is how i use to connect with my database .

if it works for you, then just go ahead and try..i too got lots of help from this forum.

just create a file

config.php

<?php

$server = 'localhost';
$user_name ='admin' //my user name
$pw = 'ijk' //password to connect with MySQL
$db = 'test' //the database

$link = mysql_connect($server,$user,$pw)

if(!$link)
{
  echo 'No connection is found ';
}else
{
  mysql_select_db($db,$link) or die('Connection found.No database found');
}

?>


now in your php file, just add this line (process.php)

<?php include_once('config.php');?>

<?php
 
$name=$_POST['name'];
$email=$_POST['email'];
$location=$_POST['location'];

sql = "insert into table (name, email,location) values ('$name', '$email', '$location') ";

$result=mysql_query($sql,$link) or die('Error in inserting values');
if(!$result)
{
          echo 'mysql_errno().mysql_error()';
}

?>


This way, you can find the errors easily.

anyother method if anyone knows that is better, pls tell to me.



i hope this will help u

cheers
sai





#11 greggustin

greggustin
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 12 July 2006 - 11:44 AM

you do not have to have a variable for ever column you can just specify the cols that you are going to give values for in the SQL statement.

eg INSERT into table (col1,col2,col3) values (col1val,col2val,col3val);


this (your suggestion) worked Great
too bad the original code I practiced with did not have the "columns id's" in them as did your example
now each new submit has its 'ID' field auto increment in my table :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users