Ok, I am trying to insert the data from a form into a table on my MySQL database. I have been going through some tutorials on how to do this, and have found slightly different answers. I'm assuming that they both kind of do the same thing, but what would be the differences betwen them and why would someone use one over the other.
First tutorial:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("my_db", $con);$sql="INSERT INTO person (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";mysql_close($con)
?>

Second tutorial:

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

$first=$_POST['first'];
$last=$_POST['last'];
$phone=$_POST['phone'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$email=$_POST['email'];
$web=$_POST['web'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')";
mysql_query($query);

mysql_close();
?>

there basically the same

the second way is more appropriate if you want to check the values to make sure they are valid before inserting them

jbennet, thanks, that is what I was thiking, but I wanted to make sure and not just blindly copy and paste things. Thanks for the clarification.

Could someone double check my code and make sure I am understanding this correctly?

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>

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

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?

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.