Ok, I thought I had gotten all the answers I needed and marked my thread as solved. But now I am having problems trying to put the code into use. I tried to insert the data from my form into my data base and got an error message. I obviously screwed something up.

Warning: mysql_connect(): Access denied for user: 'xxxxxxxx@nsev7a.hostfor2bucks.com' (Using password: YES) in /var/www/html/food_journal_insert.php on line 36
Unable to select database

Here is the code that I used. Could someone maybe look it over and see where I messed up?


Here is the code for the form I am going to use:

<form action="food_journal_insert.php" method="post">

<table width="744" border="0" cellspacing="0" cellpadding="0" background="images/spiral_notebook_bg.jpg">
  <tr>
    <td>&nbsp;</td>
    <td width="694"><p align="center" class="style1">What I ate today:</p>
  </tr>  
  <tr>
    <td width="50">&nbsp;</td>
    <td width="694">
      <p>Date: <select name="date_month">
	<option value="01">January</option>
	<option value="02">Febuary</option>
	<option value="03">March</option>
	<option value="04">April</option>
	<option value="05">May</option>
	<option value="06">June</option>
	<option value="07">July</option>
	<option value="08">August</option>
	<option value="09">September</option>
	<option value="10">October</option>
	<option value="11">November</option>
	<option value="12">December</option>
	</select>
	<select name="date_day">
		<option value="01">1</option>
		<option value="02">2</option>
		<option value="03">3</option>
		<option value="04">4</option>
		<option value="05">5</option>
		<option value="06">6</option>
		<option value="07">7</option>
		<option value="08">8</option>
		<option value="09">9</option>
		<option value="10">10</option>
		<option value="11">11</option>
		<option value="12">12</option>
		<option value="13">13</option>
		<option value="14">14</option>
		<option value="15">15</option>
		<option value="16">16</option>
		<option value="17">17</option>
		<option value="18">18</option>
		<option value="19">19</option>
		<option value="20">20</option>
		<option value="21">21</option>
		<option value="22">22</option>
		<option value="23">23</option>
		<option value="24">24</option>
		<option value="25">25</option>
		<option value="26">26</option>
		<option value="27">27</option>
		<option value="28">28</option>
		<option value="29">29</option>
		<option value="30">30</option>
		<option value="31">31</option>
		
	</select>
	<select name="date_year">
		<option value="2007">2007</option>
		<option value="2008">2008</option>
		<option value="2009">2009</option>
	</select>		

        </p>
      <p><br />
        How many Glasses of water did I drink today? 
		<input type="radio" name="water" value="1"> 1
		<input type="radio" name="water" value="2"> 2
		<input type="radio" name="water" value="3"> 3
		<input type="radio" name="water" value="4"> 4
		<input type="radio" name="water" value="5"> 5
		<input type="radio" name="water" value="6"> 6
		<input type="radio" name="water" value="7"> 7
		<input type="radio" name="water" value="8"> 8+		
      <p><br />
        Breakfast  
          <textarea name="breakfast"></textarea>
    Calories
    <input type="text" name="breakfast_calories" />
    Time 
	<select name="breakfast_time_hr">
		  <option value="1">1</option>
		  <option value="2">2</option>
		  <option value="3">3</option>
		  <option value="4">4</option>
		  <option value="5">5</option>
		  <option value="6">6</option>
		  <option value="7">7</option>
		  <option value="8">8</option>
		  <option value="9">9</option>
		  <option value="10">10</option>
		  <option value="11">11</option>
		  <option value="12">12</option>
	  </select>
	:
	<select name="breakfast_time_min">
		  <option value="1">00</option>
		  <option value="2">15</option>
		  <option value="3">30</option>
		  <option value="4">45</option>
		  
	</select>
	<select name="breakfast_time_ampm">
		  <option value="am">am</option>
		  <option value="pm">pm</option>
	  </select>	 
      <br />
      <br />
      Snack 1 &nbsp;
      <textarea name="snack_1"></textarea>
      Calories
      <input type="text" name="snack_1_calories" />
      Time
	  <select name="snack_1_time_hr">
		  <option value="1">1</option>
		  <option value="2">2</option>
		  <option value="3">3</option>
		  <option value="4">4</option>
		  <option value="5">5</option>
		  <option value="6">6</option>
		  <option value="7">7</option>
		  <option value="8">8</option>
		  <option value="9">9</option>
		  <option value="10">10</option>
		  <option value="11">11</option>

		  <option value="12">12</option>
	    </select>
	:
	<select name="snack_1_time_min">
		  <option value="1">00</option>
		  <option value="2">15</option>
		  <option value="3">30</option>
		  <option value="4">45</option>
		  
	</select>
	<select name="snack_1_time_ampm">
		  <option value="am">am</option>
		  <option value="pm">pm</option>
	  </select>	
        </p>      <p><br />
          Lunch &nbsp; &nbsp; &nbsp;
          <textarea name="lunch"></textarea>
    Calories 
    <input type="text" name="lunch_calories" />
    Time  
	<select name="lunch_time_hr">
		<option value="1">1</option>
		<option value="2">2</option>
		<option value="3">3</option>
		<option value="4">4</option>
		<option value="5">5</option>
		<option value="6">6</option>
		<option value="7">7</option>
		<option value="8">8</option>
		<option value="9">9</option>
		<option value="10">10</option>
		<option value="11">11</option>
		<option value="12">12</option>
	</select>
	:
	<select name="lunch_time_min">
		<option value="1">00</option>
		<option value="2">15</option>
		<option value="3">30</option>
		<option value="4">45</option>
		
	</select>
	<select name="lunch_time_ampm">
		<option value="am">am</option>
		<option value="pm">pm</option>
	</select>	
    <br />
    <br />
    Snack 2&nbsp;  
    <textarea name="snack_2"></textarea>
    Calories 
    <input type="text" name="snack_2_calories" />
    Time  
	<select name="snack_2_time_hr">
		<option value="1">1</option>
		<option value="2">2</option>
		<option value="3">3</option>
		<option value="4">4</option>
		<option value="5">5</option>
		<option value="6">6</option>
		<option value="7">7</option>
		<option value="8">8</option>
		<option value="9">9</option>
		<option value="10">10</option>
		<option value="11">11</option>
		<option value="12">12</option>
	</select>
	:
	<select name="snack_2_time_min">
		<option value="1">00</option>
		<option value="2">15</option>
		<option value="3">30</option>
		<option value="4">45</option>
		
	</select>
	<select name="snack_2_time_ampm">
		<option value="am">am</option>
		<option value="pm">pm</option>
	</select>	
    <br />
    <br />
    Dinner &nbsp; &nbsp;  
    <textarea name="dinner"></textarea>
    Calories 
    <input type="text" name="dinner_calories" />
    Time     
	<select name="dinner_time_hr">
		<option value="1">1</option>
		<option value="2">2</option>
		<option value="3">3</option>
		<option value="4">4</option>
		<option value="5">5</option>
		<option value="6">6</option>
		<option value="7">7</option>
		<option value="8">8</option>
		<option value="9">9</option>
		<option value="10">10</option>
		<option value="11">11</option>
		<option value="12">12</option>
	</select>
	:
	<select name="dinner_time_min">
		<option value="1">00</option>
		<option value="2">15</option>
		<option value="3">30</option>
		<option value="4">45</option>
		
	</select>
	<select name="dinner_time_ampm">
		<option value="am">am</option>
		<option value="pm">pm</option>
	</select>	
    <br />
    <br />
    Snack 3&nbsp;  
    <textarea name="snack_3"></textarea>
    Calories 
    <input type="text" name="snack_3_calories" />
    Time 
	<select name="snack_3_time_hr">
		<option value="1">1</option>
		<option value="2">2</option>
		<option value="3">3</option>
		<option value="4">4</option>
		<option value="5">5</option>
		<option value="6">6</option>
		<option value="7">7</option>
		<option value="8">8</option>
		<option value="9">9</option>
		<option value="10">10</option>
		<option value="11">11</option>
		<option value="12">12</option>
	</select>
	:
	<select name="snack_3_time_min">
		<option value="1">00</option>
		<option value="2">15</option>
		<option value="3">30</option>
		<option value="4">45</option>
		
	</select>
	<select name="snack_3_time_ampm">
		<option value="am">am</option>
		<option value="pm">pm</option>
	</select>	
    </p>
        <p><br />
          <br />
          <input type="submit" value="Update My Journal"/>

        </p></td>
  </tr>

 
</table>
</form>

Here is the code for the insert.php. I did the time values in the same way as the date values, is that correct?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>food_journal_insert.php script</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
$username="username";
$password="password";
$database="your_database";

$client_username=$_POST['client_username'];
$TIMESTAMP=$_POST['date_year']."-".$_POST['date_month']."-".$_POST['date_day']; 
$water=$_POST['water'];
$breakfast=$_POST['breakfast'];
$breakfast_calories=$_POST['breakfast_calories'];
$breakfast_time=$_POST['breakfast_time_hr'].":".$_POST['breakfast_time_min']." ".$_POST['breakfast_time_ampm']; 
$snack_1=$_POST['snack_1'];
$snack_1_calories=$_POST['snack_1_calories'];
$snack_1_time=$_POST['snack_1_time_hr'].":".$_POST['snack_1_min']." ".$_POST['snack_1_ampm'];
$lunch=$_POST['lunch']; 
$lunch_calories=$_POST['lunch_calories'];
$lunch_time=$_POST['lunch_time_hr'].":".$_POST['lunch_time_min']." ".$_POST['blunch_time_ampm'];
$snack_2=$_POST['snack_2'];
$snack_2_calories=$_POST['snack_2_calories'];
$snack_2_time=$_POST['snack_2_time_hr'].":".$_POST['snack_2_time_min']." ".$_POST['snack_2_time_ampm'];
$dinner=$_POST['dinner'];
$dinner_calories=$_POST['dinner_calories'];
$dinner_time=$_POST['dinner_time_hr'].":".$_POST['dinner_time_min']." ".$_POST['dinner_time_ampm'];
$snack_3=$_POST['snack_3'];
$snack_3_calories=$_POST['snack_3_calories'];
$snack_3_time=$_POST['snack_3_time_hr'].":".$_POST['snack_3_time_min']." ".$_POST['snack_3_time_ampm'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 
$query="INSERT INTO food_journal VALUES('','$client_username','$date_month','$date_day','$date_year',
'$water','$breakfast','$breakfast_calories','$breakfast_time','$snack_1','$snack_1_calories',
'$snack_1_time','$lunch','$lunch_calories','$lunch_time','$snack_2','$snack_2_calories',
'$snack_2_time','$dinner','$dinner_calories','$dinner_time','$snack_3','$snack_3_calories',
'$snack_3_time')";
mysql_query($query);

mysql_close();
?> 

</body>
</html>

Recommended Answers

All 10 Replies

mysql_connect(localhost,$username,$password);

should be

mysql_connect('localhost',$username,$password);

Try this and see what happens.

Ok, yeah, I missed that, but I still get the same error when I fixed that. Dumb question, how do you write the localhost? would it be 'daniweb', 'daniweb.com', or 'http://daniweb.com'. When I tried it with the 'http://' it came back with unknown MySQL server Host 'http'. When I tried it with the dot com at the end, the dot disappeared and was made into a space, and came up with the same error message.

Is there anyway to change the database name once it has been created using phpMyAdmin? I am learning as I go, and when I created the database name, I think I might have done some things that weren't allowed. This is what it came out as 'befit4riding_com_-_journal'. Will that make any difference?

are

$username="username";
$password="password";
$database="your_database";

set to the correct things ? and is the database and food_journal created? is the query ssyntax valid?

In my actual code I changed 'username' and 'password' to the correct username and password for the database, I just didn't want to post those up on an open forum. I'm not sure if the query syntax is valid, not even sure what the query syntax is. I'm trying to learn this as I go using tutorials and the help I get from daniweb. Is this the query that you are refering to?
$query="INSERT INTO food_journal VALUES
If so, that is the syntax that I got from a tutorial.

Are you still getting access denied? If so, try this:

$dbhost = 'localhost';
$dbuser = 'jaysusername';
$dbpass = 'jayspassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'befit4riding_com_-_journal';
mysql_select_db($dbname);

Insert Syntax for MySQL:
If you were inserting the variables $email and $name into the table you would use:

INSERT INTO food_journal (email,name) VALUES ('$email','$name')";

Where the left side of VALUES is the actual fieldnames in the table and the right side of VALUES is the variables you want to insert.

Ok, I made the changes recommended and still get the following error message:

Warning: mysql_connect(): Access denied for user: 'jaysusername@nsev7a.hostfor2bucks.com' (Using password: YES) in /var/www/html/food_journal_insert.php on line 36
Unable to select database

Here is the revised code, and I had changed jaysusername and jayspassword to the same username and password that I use to log into phpMySQL.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>food_journal_insert.php script</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?

$dbhost = 'localhost';
$dbuser = 'jaysusername';
$dbpass = 'jayspassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'befit4riding_com_-_journal';
mysql_select_db($dbname) or die( "Unable to select database");

$client_username=$_POST['client_username'];
$TIMESTAMP=$_POST['date_year']."-".$_POST['date_month']."-".$_POST['date_day']; 
$water=$_POST['water'];
$breakfast=$_POST['breakfast'];
$breakfast_calories=$_POST['breakfast_calories'];
$breakfast_time=$_POST['breakfast_time_hr'].":".$_POST['breakfast_time_min']." ".$_POST['breakfast_time_ampm']; 
$snack_1=$_POST['snack_1'];
$snack_1_calories=$_POST['snack_1_calories'];
$snack_1_time=$_POST['snack_1_time_hr'].":".$_POST['snack_1_min']." ".$_POST['snack_1_ampm'];
$lunch=$_POST['lunch']; 
$lunch_calories=$_POST['lunch_calories'];
$lunch_time=$_POST['lunch_time_hr'].":".$_POST['lunch_time_min']." ".$_POST['blunch_time_ampm'];
$snack_2=$_POST['snack_2'];
$snack_2_calories=$_POST['snack_2_calories'];
$snack_2_time=$_POST['snack_2_time_hr'].":".$_POST['snack_2_time_min']." ".$_POST['snack_2_time_ampm'];
$dinner=$_POST['dinner'];
$dinner_calories=$_POST['dinner_calories'];
$dinner_time=$_POST['dinner_time_hr'].":".$_POST['dinner_time_min']." ".$_POST['dinner_time_ampm'];
$snack_3=$_POST['snack_3'];
$snack_3_calories=$_POST['snack_3_calories'];
$snack_3_time=$_POST['snack_3_time_hr'].":".$_POST['snack_3_time_min']." ".$_POST['snack_3_time_ampm'];



$query="INSERT INTO food_journal (client_username,date_month,date_day,date_year,
water,breakfast,breakfast_calories,breakfast_time,snack_1,snack_1_calories,
snack_1_time,lunch,lunch_calories,lunch_time,snack_2,snack_2_calories,
snack_2_time,dinner,dinner_calories,dinner_time,snack_3,snack_3_calories,
snack_3_time) VALUES ('','$client_username','$date_month','$date_day','$date_year',
'$water','$breakfast','$breakfast_calories','$breakfast_time','$snack_1','$snack_1_calories',
'$snack_1_time','$lunch','$lunch_calories','$lunch_time','$snack_2','$snack_2_calories',
'$snack_2_time','$dinner','$dinner_calories','$dinner_time','$snack_3','$snack_3_calories',
'$snack_3_time')";
mysql_query($query);

mysql_close();
?> 

</body>
</html>

I don't know if this makes any difference, but this is what it claims is running on the phpMySQL:

Welcome to phpMyAdmin 2.5.3
MySQL 3.23.58 running on localhost as jaysusername@localhost

Sounds like somewhere in your connection, either the username or password is wrong. If the host was wrong the error would say Unknown MySQL server host. It's strange because usually the error would be :
Access denied for user: '*****@localhost' .
instead of
Access denied for user: 'xxxxxxxx@nsev7a.hostfor2bucks.com'

Sounds like somewhere in your connection, either the username or password is wrong.

Yeah, I was thinking that it was kind of wrong, so I copied both from the input field while I was signing into phpMyAdmin and pasted them right into the script, and then I signed onto phpMyAdmin and it signed in good. But it seems to me that it is coming up as the wrong login info.

If the host was wrong the error would say Unknown MySQL server host. It's strange because usually the error would be :
Access denied for user: '*****@localhost' .
instead of
Access denied for user: 'xxxxxxxx@nsev7a.hostfor2bucks.com'

I'm starting to think that I need to contact the hosting company and see if there is any weird thing that needs to be done for their system.

Does this mean that I can't use php scripting to automatically insert data into the db?

Can I connect to my database using a TCP/IP connection? [top]

For the security reasons, this will not be permitted. You must use PhpMyAdmin to manage your DB.

Timestamp: 2003/04/09

But then it says this:

Can I run Web application with connectivity to my DB? [top]

Yes, you may install or develop PHP, CGI applications and publish them to your site with connectivity to your Database.

Timestamp: 2003/05/08

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.