PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me



Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Old January 30th, 2013, 10:16 PM
sigridish sigridish is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 sigridish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 50 sec
Reputation Power: 0
Cant connect php to database

Hi all! I would like to ask for help. i have a code that i got from youtube. my problem is i cant connect to my database and i dont know why. i dont think that i have a problem in my code. i have 2 php file. reg.php for the connection and demo-form.php for adding the record.

demo-form.php



<form action="reg.php" method="post" />
<p>First Name: <input type="text" name="fname" /> </p>
<p>Middle Name: <input type="text" name="mname" /> </p>
<p>Last Name: <input type="text" name="lname" /> </p>
<p>Home address: <input type="text" name="homeadd" /> </p>
<p>Email address: <input type="text" name="emailadd" /> </p>
<p>Office/project number: <input type="text" name="ofcnum" /> </p>
<p>Mobile number: <input type="text" name="mobilenum" /> </p>
<p>Home number: <input type="text" name="homenum" /> </p>
<p>Position:
<select name="position">
<option value ="">Select</option>
<option value ="Project Manager">Project Manager</option>
<option value ="Practice Lead">Project Manager</option>
<option value ="Team Leader">Team Leader</option>
<option value ="Team Member">Team Member</option>
</select>
<p>Practice:
<select name="position">
<option value ="">Select</option>
<option value ="Solutions">Solutions</option>
<option value ="Outsourcing">Outsourcing</option>
</select>
<p>Project Name: <input type="text" name="projname" /> </p>
<p>Project Manager: <input type="text" name="projmgr" /> </p>
<p>Team Leader: <input type="text" name="teamlead" /> </p>


<input type="submit" value="Submit" />
<body bgcolor="cccccc">
</form>



reg.php


<?php

define ('DB_NAME', 'emp');
define ('DB_USER', 'root');
define ('DB_PASSWORD', '');
define ('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

$value1 = $_POST["fname"];
$value2 = $_POST["mname"];
$value3 = $_POST["lname"];
$value4 = $_POST["homeadd"];
$value5 = $_POST["emailadd"];
$value6 = $_POST["ofcnum"];
$value7 = $_POST["mobilenum"];
$value8 = $_POST["homenum"];
$value9 = $_POST["position"];
$value10 = $_POST["practice"];
$value11 = $_POST["projname"];
$value12 = $_POST["projmgr"];
$value13 = $_POST["teamlead"];

$sql = "INSERT INTO employee (fname, mname, lname, homeadd, emailadd, ofcnum, mobilenum, homenum, position, projname, projmgr, teamlead)
VALUES ('$value1', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12')";

if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}

mysql_close();

?>

Reply With Quote
Old January 30th, 2013, 11:22 PM
rbrown rbrown is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 139 rbrown User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 12 h 8 m 5 sec
Reputation Power: 8
What is|are the error|s you are getting when you run this?
Plus you should be using mysqli or PDO instead of mysql.
__________________
Bob

Last edited by rbrown : January 30th, 2013 at 11:25 PM.

Reply With Quote
Old January 30th, 2013, 11:25 PM
sigridish sigridish is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 sigridish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 50 sec
Reputation Power: 0
hi rbrown!

i dont get any errors. i just cant connect it to my database

Reply With Quote
Old January 31st, 2013, 12:23 AM
rbrown rbrown is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 139 rbrown User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 12 h 8 m 5 sec
Reputation Power: 8
you should be with this line in the code.
PHP Code:
if (!$link) {
die(
'Could not connect: ' mysql_error());



so if you aren't getting anything,
1) it's not getting to that section of the code
OR
2) you do not have errors turned on in the server

add this to the top of the code and run it.

PHP Code:
 error_reporting(E_ALL);
ini_set('display_errors''1'); 

Reply With Quote
Old January 31st, 2013, 01:03 AM
sigridish sigridish is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 sigridish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 50 sec
Reputation Power: 0
it's still the same

Reply With Quote
Old January 31st, 2013, 01:23 AM
rbrown rbrown is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 139 rbrown User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 12 h 8 m 5 sec
Reputation Power: 8
Try this below in a separate file, replace "your_whatever" with your settings and tell me what you have.
And what are you testing on and with?
local server, hosted server
windows, unix
Wamp, uniform server, etc...

PHP Code:
<?php
mysql_connect
("localhost""your_username""your_password") or die(mysql_error());
echo 
"Connected to MySQL<br />"
mysql_select_db("your_db") or die(mysql_error()); 
echo 
"Connected to Database"?>

Last edited by rbrown : January 31st, 2013 at 01:31 AM.

Reply With Quote
Old January 31st, 2013, 01:26 AM
sigridish sigridish is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 sigridish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 50 sec
Reputation Power: 0
i replaced you code with this

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
echo “Connected to MySQL<br />”;
mysql_select_db("emp") or die(mysql_error());
echo “Connected to Database”;

?>

and what i got was

"”; mysql_select_db("emp") or die(mysql_error()); echo “Connected to Database”; ?> "

im using wamp server

Reply With Quote
Old January 31st, 2013, 01:35 AM
rbrown rbrown is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 139 rbrown User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 12 h 8 m 5 sec
Reputation Power: 8
Sorry... I forgot to fix the double quotes...
try it again with this...


PHP Code:
<?php 
mysql_connect
("localhost""root""") or die(mysql_error());
echo 
"Connected to MySQL<br />";
mysql_select_db("emp") or die(mysql_error()); 
echo 
"Connected to Database";
?>

Reply With Quote
Old January 31st, 2013, 01:40 AM
sigridish sigridish is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 sigridish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 50 sec
Reputation Power: 0
i tried it and got this

"; mysql_select_db("emp") or die(mysql_error()); echo "Connected to Database"; ?>


Reply With Quote
Old January 31st, 2013, 01:42 AM
sigridish sigridish is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 sigridish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 50 sec
Reputation Power: 0
Hi rbrown!

i assessed the codes. there was a br tag. i removed it then i got a blank page

Reply With Quote
Old January 31st, 2013, 01:47 AM
rbrown rbrown is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 139 rbrown User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 12 h 8 m 5 sec
Reputation Power: 8
your files has to have a .php not a .html
Unless...
you have it setup in your .htaccess file to process .html files as .php files

Or

if you are using Apache force type directive

Reply With Quote
Old January 31st, 2013, 02:04 AM
rbrown rbrown is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 139 rbrown User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 12 h 8 m 5 sec
Reputation Power: 8
the test code should work without removing the <br/> tag the reason why you are seeing:

"; mysql_select_db("emp") or die(mysql_error()); echo "Connected to Database"; ?>

is because your test page does not have a php extension.

save it as db_test.php
and try it again.

you should see:

Connected to MySQL
Connected to Database


I'm trying to make sure you can connect to the db first them we can address the rest of your code. There are a few issues.

I have to leave so we can pick this back up in about 4 hours if you still haven't got it working....

Last edited by rbrown : January 31st, 2013 at 02:07 AM.

Reply With Quote
Old January 31st, 2013, 01:50 PM
rbrown rbrown is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 139 rbrown User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 12 h 8 m 5 sec
Reputation Power: 8
Well where are we at now?
Did you get the db test working?

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Cant connect php to database


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap