Jump to content

newbie = cannot connect to DB on localhost


greggustin

Recommended Posts

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
[code]<!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>
[/code]

and here is the code for process.php
[code]<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>[/code]

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

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)

Link to comment
Share on other sites

Change:
[code]MySQL_connect("localhost:/MySQL/data/test1", "root", "")or die ('I cannot connect to the database because: ' . MySQL_error());[/code]
To
[code]MySQL_connect("localhost", "root", "")or die ('I cannot connect to the database because: ' . MySQL_error());[/code]
Link to comment
Share on other sites

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:
[code]if(mysql_affected_rows($connection)==0){
echo "Insertion failed.";
}
[/code]

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

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.
Link to comment
Share on other sites

[quote author=hackerkts link=topic=100261.msg395533#msg395533 date=1152669104]
Change:
[code]MySQL_connect("localhost:/MySQL/data/test1", "root", "")or die ('I cannot connect to the database because: ' . MySQL_error());[/code]
To
[code]MySQL_connect("localhost", "root", "")or die ('I cannot connect to the database because: ' . MySQL_error());[/code]
[/quote]

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?
Link to comment
Share on other sites

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

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

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?

Link to comment
Share on other sites

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

[code]mysql_connect("localhost", "root","")or die(mysql_error());
mysql_select_db("database-name-here") or die(mysql_error());[/code]
Link to comment
Share on other sites

[quote author=hackers link=topic=100261.msg395547#msg395547 date=1152673627]
Last solution, try to use wamp -> http://www.wampserver.com/en
You just put "localhost" and it will be working.

[code]MySQL_connect("localhost", "root","")or die(MySQL_error());
MySQL_select_db("database-name-here") or die(MySQL_error());[/code]
[/quote]

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
[code]$id=7;[/code]
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?


Link to comment
Share on other sites

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

[color=red]<?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');
}

?>
[/color]

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

[color=red]<?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()';
}

?>[/color]

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



Link to comment
Share on other sites

[quote author=criticalsystems link=topic=100261.msg395561#msg395561 date=1152676914]
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);
[/quote]

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.