Solved

Populate list menu with mysql data

Posted on 2006-10-20
12
188 Views
Last Modified: 2013-12-12
Hi all i have an insert form which has numerous list menus. One of which is a username list menu.

I want to have the membername listmenu populated from a column/table in my databse (mySQL)

I found various posts on this but they seem to conflict with the way i have my code

Can you help?

heres my code:

<?php

include ( 'functions.php' );

if ( isset($_FILES['photo']) && ( strlen($_FILES['photo']['name']) > 0 ) ) {

   $photo = $_FILES['photo']['tmp_name'];
   $photo_name = $_FILES['photo']['name'];
   $photo_size = $_FILES['photo']['size'];
   $photo_type = $_FILES['photo']['type'];
   $photo_error = $_FILES['photo']['error'];
   $imgName = basename($photo).'.jpg';
   $dest = 'images/resized/'.$imgName;
   $image = resize_image('jpg',$photo,$dest,300,75);

if ( $image === false ) {

  echo 'Could not upload image<br>';
  echo $messages;

} else {

  $image = $dest;

  $db = @mysql_pconnect('localhost', 'username', 'password');
  @mysql_select_db('database');

  $query = 'INSERT INTO fish_catches(membername, date, timehr, timemin, weightlb, weightoz, bait, typeoffish, notes, photo) VALUES( \''.$_POST['membername'].'\', \''.$_POST['date'].'\', \''.$_POST['timehr'].'\', \''.$_POST['timemin'].'\', \''.$_POST['weightlb'].'\', \''.$_POST['weightoz'].'\', \''.$_POST['bait'].'\', \''.$_POST['typeoffish'].'\', \''.$_POST['notes'].'\', \'/'.$image.'\')';

  if ( @mysql_query($query) !== false ) {
   echo 'Your fish has been successfully added to the database<br>';
   echo $messages;
  } else {
   echo 'Error while executing query.<br>'.mysql_error();
  }
}
}

echo '<form enctype="multipart/form-data" action="add.php" method="POST">
Member Name: <input type="text" name="membername"><br>
Date: <input type="text" id="date" name="date" />
  <button id="trigger">...</button><br>
  <script type="text/javascript">
  Calendar.setup(
    {
      inputField  : "date",         // ID of the input field
      ifFormat    : "%Y-%m-%d",    // the date format
      button      : "trigger"       // ID of the button
    }
  );
</script>
Time(hr): <select name="timehr" id="timehr">
            <option value="00">00</option>
            <option value="01">01</option>
            <option value="02">02</option>
            <option value="03">03</option>
            <option value="04">04</option>
            <option value="05">05</option>
            <option value="06">06</option>
            <option value="07">07</option>
            <option value="08">08</option>
            <option value="09">09</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>
            </select><br>
Time(min): <select name="timemin" id="timemin">
            <option value="5">5</option>
            <option value="10">10</option>
            <option value="15">15</option>
            <option value="20">20</option>
            <option value="25">25</option>
            <option value="30">30</option>
            <option value="35">35</option>
            <option value="40">40</option>
            <option value="45">45</option>
            <option value="50">50</option>
            <option value="55">55</option>
            <option value="00" selected="selected">00</option>
          </select><br>
Weight(lb): <select name="weightlb" id="weightlb">
            <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>
            <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>
            <option value="32">32</option>
            <option value="33">33</option>
            <option value="34">34</option>
            <option value="35">35</option>
            <option value="36">36</option>
            <option value="37">37</option>
            <option value="38">38</option>
            <option value="39">39</option>
            <option value="40">40</option>
            <option value="41">41</option>
            <option value="42">42</option>
            <option value="43">43</option>
            <option value="44">44</option>
            <option value="45">45</option>
            <option value="46">46</option>
            <option value="47">47</option>
            <option value="48">48</option>
            <option value="49">49</option>
            <option value="50">50</option>
            <option value="51">51</option>
            <option value="52">52</option>
            <option value="53">53</option>
            <option value="54">54</option>
            <option value="55">55</option>
            <option value="56">56</option>
            <option value="57">57</option>
            <option value="58">58</option>
            <option value="59">59</option>
            <option value="60">60</option>
            <option value="61">61</option>
            <option value="62">63</option>
            <option value="63">63</option>
            <option value="64">64</option>
            <option value="65">65</option>
            <option value="66">66</option>
            <option value="67">67</option>
            <option value="68">68</option>
            <option value="69">69</option>
            <option value="70">70</option>

          </select><br>
Weight(oz): <select name="weightoz" id="weightoz">
                    <option value="0">0</option>
              <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>
              <option value="13">13</option>
              <option value="14">14</option>
              <option value="15">15</option>
              </select><br>
Bait: <input type="text" name="bait"><br>
Type Of Fish: <select name="typeoffish" id="typeoffish">
          <option value="Bream">Bream</option>
          <option value="Catfish">Catfish</option>
          <option value="Chubb">Chubb</option>
          <option value="Common Carp">Common Carp</option>
          <option value="Crucian Carp">Crucian Carp</option>
          <option value="Eel">Eel</option>
          <option value="Fully Scaled Carp">Fully Scaled Carp</option>
          <option value="Ghost Carp">Ghost Carp</option>
          <option value="Mirror Carp">Mirror Carp</option>
          <option value="Perch">Perch</option>
          <option value="Pike" selected="selected">Pike</option>
          <option value="Roach">Roach</option>
          <option value="Rudd">Rudd</option>
          <option value="Tench">Tench</option>
          <option value="Zander">Zander</option>
          </select><br>
Notes: <input type="text" name="notes"><br>
Photo: <input type="file" name="photo"><br>
<input type="submit" value="Add">
</form> ';

?>


Thanks guys!
0
Comment
Question by:satmanuk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 6

Assisted Solution

by:Kosta
Kosta earned 250 total points
ID: 17778680
Assuming you have a table called 'members' with a field 'membername'

<?php

include ( 'functions.php' );

if ( isset($_FILES['photo']) && ( strlen($_FILES['photo']['name']) > 0 ) ) {

   $photo = $_FILES['photo']['tmp_name'];
   $photo_name = $_FILES['photo']['name'];
   $photo_size = $_FILES['photo']['size'];
   $photo_type = $_FILES['photo']['type'];
   $photo_error = $_FILES['photo']['error'];
   $imgName = basename($photo).'.jpg';
   $dest = 'images/resized/'.$imgName;
   $image = resize_image('jpg',$photo,$dest,300,75);

if ( $image === false ) {

  echo 'Could not upload image<br>';
  echo $messages;

} else {

  $image = $dest;

  $db = @mysql_pconnect('localhost', 'username', 'password');
  @mysql_select_db('database');

  $query = 'INSERT INTO fish_catches(membername, date, timehr, timemin, weightlb, weightoz, bait, typeoffish, notes, photo) VALUES( \''.$_POST['membername'].'\', \''.$_POST['date'].'\', \''.$_POST['timehr'].'\', \''.$_POST['timemin'].'\', \''.$_POST['weightlb'].'\', \''.$_POST['weightoz'].'\', \''.$_POST['bait'].'\', \''.$_POST['typeoffish'].'\', \''.$_POST['notes'].'\', \'/'.$image.'\')';

  if ( @mysql_query($query) !== false ) {
   echo 'Your fish has been successfully added to the database<br>';
   echo $messages;
  } else {
   echo 'Error while executing query.<br>'.mysql_error();
  }
}
}

echo '<form enctype="multipart/form-data" action="add.php" method="POST">
Member Name: <select name="membername">';
$query='SELECT membername FROM members';
$q=mysql_query($query) or die('Error while executing query.<br>'.mysql_error());
while($r=mysql_fetch_array($q)
    echo "\n<option>$r[membername]</option>";

echo '
</select>
<br>
Date: <input type="text" id="date" name="date" />
  <button id="trigger">...</button><br>
  <script type="text/javascript">
  Calendar.setup(
    {
      inputField  : "date",         // ID of the input field
      ifFormat    : "%Y-%m-%d",    // the date format
      button      : "trigger"       // ID of the button
    }
  );
</script>
Time(hr): <select name="timehr" id="timehr">
            <option value="00">00</option>
            <option value="01">01</option>
            <option value="02">02</option>
            <option value="03">03</option>
            <option value="04">04</option>
            <option value="05">05</option>
            <option value="06">06</option>
            <option value="07">07</option>
            <option value="08">08</option>
            <option value="09">09</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>
            </select><br>
Time(min): <select name="timemin" id="timemin">
            <option value="5">5</option>
            <option value="10">10</option>
            <option value="15">15</option>
            <option value="20">20</option>
            <option value="25">25</option>
            <option value="30">30</option>
            <option value="35">35</option>
            <option value="40">40</option>
            <option value="45">45</option>
            <option value="50">50</option>
            <option value="55">55</option>
            <option value="00" selected="selected">00</option>
          </select><br>
Weight(lb): <select name="weightlb" id="weightlb">
            <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>
            <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>
            <option value="32">32</option>
            <option value="33">33</option>
            <option value="34">34</option>
            <option value="35">35</option>
            <option value="36">36</option>
            <option value="37">37</option>
            <option value="38">38</option>
            <option value="39">39</option>
            <option value="40">40</option>
            <option value="41">41</option>
            <option value="42">42</option>
            <option value="43">43</option>
            <option value="44">44</option>
            <option value="45">45</option>
            <option value="46">46</option>
            <option value="47">47</option>
            <option value="48">48</option>
            <option value="49">49</option>
            <option value="50">50</option>
            <option value="51">51</option>
            <option value="52">52</option>
            <option value="53">53</option>
            <option value="54">54</option>
            <option value="55">55</option>
            <option value="56">56</option>
            <option value="57">57</option>
            <option value="58">58</option>
            <option value="59">59</option>
            <option value="60">60</option>
            <option value="61">61</option>
            <option value="62">63</option>
            <option value="63">63</option>
            <option value="64">64</option>
            <option value="65">65</option>
            <option value="66">66</option>
            <option value="67">67</option>
            <option value="68">68</option>
            <option value="69">69</option>
            <option value="70">70</option>

          </select><br>
Weight(oz): <select name="weightoz" id="weightoz">
                 <option value="0">0</option>
              <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>
              <option value="13">13</option>
              <option value="14">14</option>
              <option value="15">15</option>
              </select><br>
Bait: <input type="text" name="bait"><br>
Type Of Fish: <select name="typeoffish" id="typeoffish">
          <option value="Bream">Bream</option>
          <option value="Catfish">Catfish</option>
          <option value="Chubb">Chubb</option>
          <option value="Common Carp">Common Carp</option>
          <option value="Crucian Carp">Crucian Carp</option>
          <option value="Eel">Eel</option>
          <option value="Fully Scaled Carp">Fully Scaled Carp</option>
          <option value="Ghost Carp">Ghost Carp</option>
          <option value="Mirror Carp">Mirror Carp</option>
          <option value="Perch">Perch</option>
          <option value="Pike" selected="selected">Pike</option>
          <option value="Roach">Roach</option>
          <option value="Rudd">Rudd</option>
          <option value="Tench">Tench</option>
          <option value="Zander">Zander</option>
          </select><br>
Notes: <input type="text" name="notes"><br>
Photo: <input type="file" name="photo"><br>
<input type="submit" value="Add">
</form> ';

?>
0
 
LVL 2

Expert Comment

by:davidateuropol
ID: 17779108
I would like to make one correction to the previous post. Assuming that you fill a list in order to be able to retreive the chosen values again and put it into another table, I would make sure of the following.
If you are talking about members, I also assume that you want to be able to keep track of the data per member. In that case you should make sure that the ID's of the option values which are being used in the selectstatement are unique for each username and correspond with their unique identifier in the database (primary key).

Assuming the following : you have a table tblmember with at least the following fields member_id (primary key --> unique identifier for each record) and member_name.

echo '<form enctype="multipart/form-data" action="add.php" method="POST">
Member Name: <select name="membername">';
$query='SELECT member_id,member_name FROM tblmember';
$q=mysql_query($query) or die('Error while executing query.<br>'.mysql_error());
while($r=mysql_fetch_array($q)
    echo "\n<option value=$r[member_id]>$r[member_name]</option>";

echo '
</select>

Whatever name is selected now, will pass the member_id as a POST-variable, so you will be able to put this id into any field of any table and have a unique identification of one person/member.

In case of any questions just ask,
0
 
LVL 6

Expert Comment

by:Kosta
ID: 17780726
davidateuropol is right. And making it even more perfect, you have to sort names, to make selection easyer for users:

echo '<form enctype="multipart/form-data" action="add.php" method="POST">
Member Name: <select name="membername">';
$query='SELECT member_id,member_name FROM tblmember ORDER BY member_name';
$q=mysql_query($query) or die('Error while executing query.<br>'.mysql_error());
while($r=mysql_fetch_array($q)
    echo "\n<option value=$r[member_id]>$r[member_name]</option>";

echo '
</select>
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 1

Author Comment

by:satmanuk
ID: 17781101
I get this
Parse error: syntax error, unexpected T_ECHO in /home/melvin/public_html/atrb/add.php on line 145

Which referes to this line:


echo "\n<option value=$r[memberid]>$r[username]</option>";


Heres my code
echo '<form enctype="multipart/form-data" action="add.php" method="POST">

Member Name: <select name="username">';
$query='SELECT memberid,username FROM tbl_users ORDER BY username';
$q=mysql_query($query) or die('Error while executing query.<br>'.mysql_error());
while($r=mysql_fetch_array($q)
echo "\n<option value=$r[memberid]>$r[username]</option>";

echo '
</select>
0
 
LVL 2

Accepted Solution

by:
davidateuropol earned 250 total points
ID: 17781143
Okay... try changing

while($r=mysql_fetch_array($q)
echo "\n<option value=$r[memberid]>$r[username]</option>";

into

while($r=mysql_fetch_array($q) )
echo "\n<option value=$r[memberid]>$r[username]</option>";

or

while($r=mysql_fetch_array($q) ){
echo "\n<option value=$r[memberid]>$r[username]</option>";
}

I prefer the last one as I think it gives a better view on the while-loop.

Indeed Kosta, I didn't think of sorting it, but in case of names it would be better to do so.
0
 
LVL 1

Author Comment

by:satmanuk
ID: 17781231
ok i just tried both.

now the page loads with only that list menu nothing else of my form shows up at all, and the list menu doesnt show anything its blank

Ideas?


My code
<?php

include ( 'functions.php' );

if ( isset($_FILES['photo']) && ( strlen($_FILES['photo']['name']) > 0 ) ) {

   $photo = $_FILES['photo']['tmp_name'];
   $photo_name = $_FILES['photo']['name'];
   $photo_size = $_FILES['photo']['size'];
   $photo_type = $_FILES['photo']['type'];
   $photo_error = $_FILES['photo']['error'];
   $imgName = basename($photo).'.jpg';
   $dest = 'images/resized/'.$imgName;
   $image = resize_image('jpg',$photo,$dest,300,75);

if ( $image === false ) {

  echo 'Could not upload image<br>';
  echo $messages;

} else {

  $image = $dest;

  $db = @mysql_pconnect('localhost', 'username', 'password');
  @mysql_select_db('database');

  $query = 'INSERT INTO fish_catches(membername, date, timehr, timemin, weightlb, weightoz, bait, typeoffish, notes, photo) VALUES( \''.$_POST['membername'].'\', \''.$_POST['date'].'\', \''.$_POST['timehr'].'\', \''.$_POST['timemin'].'\', \''.$_POST['weightlb'].'\', \''.$_POST['weightoz'].'\', \''.$_POST['bait'].'\', \''.$_POST['typeoffish'].'\', \''.$_POST['notes'].'\', \'/'.$image.'\')';

  if ( @mysql_query($query) !== false ) {
   echo 'Your fish has been successfully added to the database<br>';
   echo $messages;
  } else {
   echo 'Error while executing query.<br>'.mysql_error();
  }
}
}

echo '<form enctype="multipart/form-data" action="add.php" method="POST">
Member Name: <select name="username">';
$query='SELECT memberid,username FROM tbl_users ORDER BY username';
$q=mysql_query($query) or die('Error while executing query.<br>'.mysql_error());

while($r=mysql_fetch_array($q) )
echo "\n<option value=$r[memberid]>$r[username]</option>";

echo '
</select>
<br>
Date: <input type="text" id="date" name="date" />
  <button id="trigger">...</button><br>
  <script type="text/javascript">
  Calendar.setup(
    {
      inputField  : "date",         // ID of the input field
      ifFormat    : "%Y-%m-%d",    // the date format
      button      : "trigger"       // ID of the button
    }
  );
</script>
Time(hr): <select name="timehr" id="timehr">
            <option value="00">00</option>
            <option value="01">01</option>
            <option value="02">02</option>
            <option value="03">03</option>
            <option value="04">04</option>
            <option value="05">05</option>
            <option value="06">06</option>
            <option value="07">07</option>
            <option value="08">08</option>
            <option value="09">09</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>
            </select><br>
Time(min): <select name="timemin" id="timemin">
            <option value="5">5</option>
            <option value="10">10</option>
            <option value="15">15</option>
            <option value="20">20</option>
            <option value="25">25</option>
            <option value="30">30</option>
            <option value="35">35</option>
            <option value="40">40</option>
            <option value="45">45</option>
            <option value="50">50</option>
            <option value="55">55</option>
            <option value="00" selected="selected">00</option>
          </select><br>
Weight(lb): <select name="weightlb" id="weightlb">
            <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>
            <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>
            <option value="32">32</option>
            <option value="33">33</option>
            <option value="34">34</option>
            <option value="35">35</option>
            <option value="36">36</option>
            <option value="37">37</option>
            <option value="38">38</option>
            <option value="39">39</option>
            <option value="40">40</option>
            <option value="41">41</option>
            <option value="42">42</option>
            <option value="43">43</option>
            <option value="44">44</option>
            <option value="45">45</option>
            <option value="46">46</option>
            <option value="47">47</option>
            <option value="48">48</option>
            <option value="49">49</option>
            <option value="50">50</option>
            <option value="51">51</option>
            <option value="52">52</option>
            <option value="53">53</option>
            <option value="54">54</option>
            <option value="55">55</option>
            <option value="56">56</option>
            <option value="57">57</option>
            <option value="58">58</option>
            <option value="59">59</option>
            <option value="60">60</option>
            <option value="61">61</option>
            <option value="62">63</option>
            <option value="63">63</option>
            <option value="64">64</option>
            <option value="65">65</option>
            <option value="66">66</option>
            <option value="67">67</option>
            <option value="68">68</option>
            <option value="69">69</option>
            <option value="70">70</option>

          </select><br>
Weight(oz): <select name="weightoz" id="weightoz">
                 <option value="0">0</option>
              <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>
              <option value="13">13</option>
              <option value="14">14</option>
              <option value="15">15</option>
              </select><br>
Bait: <input type="text" name="bait"><br>
Type Of Fish: <select name="typeoffish" id="typeoffish">
          <option value="Bream">Bream</option>
          <option value="Catfish">Catfish</option>
          <option value="Chubb">Chubb</option>
          <option value="Common Carp">Common Carp</option>
          <option value="Crucian Carp">Crucian Carp</option>
          <option value="Eel">Eel</option>
          <option value="Fully Scaled Carp">Fully Scaled Carp</option>
          <option value="Ghost Carp">Ghost Carp</option>
          <option value="Mirror Carp">Mirror Carp</option>
          <option value="Perch">Perch</option>
          <option value="Pike" selected="selected">Pike</option>
          <option value="Roach">Roach</option>
          <option value="Rudd">Rudd</option>
          <option value="Tench">Tench</option>
          <option value="Zander">Zander</option>
          </select><br>
Notes: <input type="text" name="notes"><br>
Photo: <input type="file" name="photo"><br>
<input type="submit" value="Add">
</form> ';

?>
0
 
LVL 6

Expert Comment

by:Kosta
ID: 17781565
Please paste here the HTML source of the resulting page (from the browser's "View Source").
Also, it may be good to test this statement: "SELECT memberid,username FROM tbl_users ORDER BY username" in a phpMyAdmin or any other tool that you youse for database tasks, in order to check that all table names and field names are correct.
0
 
LVL 1

Author Comment

by:satmanuk
ID: 17781812
Excellent!

Good tip, i found that it had an error saying it couldnt connect to the database,

I have added another connection string and it works!

Do i have to have the 2 connections called or is there a way to have just one?


Thanks for your help.
0
 
LVL 1

Author Comment

by:satmanuk
ID: 17781818
Sorry here is what i mean

<?php

include ( 'functions.php' );

if ( isset($_FILES['photo']) && ( strlen($_FILES['photo']['name']) > 0 ) ) {

   $photo = $_FILES['photo']['tmp_name'];
   $photo_name = $_FILES['photo']['name'];
   $photo_size = $_FILES['photo']['size'];
   $photo_type = $_FILES['photo']['type'];
   $photo_error = $_FILES['photo']['error'];
   $imgName = basename($photo).'.jpg';
   $dest = 'images/resized/'.$imgName;
   $image = resize_image('jpg',$photo,$dest,300,75);

if ( $image === false ) {

  echo 'Could not upload image<br>';
  echo $messages;

} else {

  $image = $dest;

  $db = @mysql_pconnect('localhost', 'username, 'password');
  @mysql_select_db('database');

  $query = 'INSERT INTO table(username, date, timehr, timemin, weightlb, weightoz, bait, typeoffish, notes, photo) VALUES( \''.$_POST['username'].'\', \''.$_POST['date'].'\', \''.$_POST['timehr'].'\', \''.$_POST['timemin'].'\', \''.$_POST['weightlb'].'\', \''.$_POST['weightoz'].'\', \''.$_POST['bait'].'\', \''.$_POST['typeoffish'].'\', \''.$_POST['notes'].'\', \'/'.$image.'\')';

  if ( @mysql_query($query) !== false ) {
   echo 'Your fish has been successfully added to the database<br>';
   echo $messages;
  } else {
   echo 'Error while executing query.<br>'.mysql_error();
  }
}
}

$db = @mysql_pconnect('localhost', 'username', 'password');
  @mysql_select_db('database');
echo '<form enctype="multipart/form-data" action="add.php" method="POST">
Member Name: <select name="username">';
$query='SELECT memberid,username FROM tbl_users ORDER BY username';
$q=mysql_query($query) or die('Error while executing query.<br>'.mysql_error());

while($r=mysql_fetch_array($q) )
echo "\n<option value=$r[memberid]>$r[username]</option>";


 echo '
</select>

<br>

Date: <input type="text" id="date" name="date" />
  <button id="trigger">...</button><br>
  <script type="text/javascript">
  Calendar.setup(
    {
      inputField  : "date",         // ID of the input field
      ifFormat    : "%Y-%m-%d",    // the date format
      button      : "trigger"       // ID of the button
    }
  );
</script>
Time(hr): <select name="timehr" id="timehr">
            <option value="00">00</option>
            <option value="01">01</option>
            <option value="02">02</option>
            <option value="03">03</option>
            <option value="04">04</option>
            <option value="05">05</option>
            <option value="06">06</option>
            <option value="07">07</option>
            <option value="08">08</option>
            <option value="09">09</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>
            </select><br>
Time(min): <select name="timemin" id="timemin">
            <option value="5">5</option>
            <option value="10">10</option>
            <option value="15">15</option>
            <option value="20">20</option>
            <option value="25">25</option>
            <option value="30">30</option>
            <option value="35">35</option>
            <option value="40">40</option>
            <option value="45">45</option>
            <option value="50">50</option>
            <option value="55">55</option>
            <option value="00" selected="selected">00</option>
          </select><br>
Weight(lb): <select name="weightlb" id="weightlb">
            <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>
            <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>
            <option value="32">32</option>
            <option value="33">33</option>
            <option value="34">34</option>
            <option value="35">35</option>
            <option value="36">36</option>
            <option value="37">37</option>
            <option value="38">38</option>
            <option value="39">39</option>
            <option value="40">40</option>
            <option value="41">41</option>
            <option value="42">42</option>
            <option value="43">43</option>
            <option value="44">44</option>
            <option value="45">45</option>
            <option value="46">46</option>
            <option value="47">47</option>
            <option value="48">48</option>
            <option value="49">49</option>
            <option value="50">50</option>
            <option value="51">51</option>
            <option value="52">52</option>
            <option value="53">53</option>
            <option value="54">54</option>
            <option value="55">55</option>
            <option value="56">56</option>
            <option value="57">57</option>
            <option value="58">58</option>
            <option value="59">59</option>
            <option value="60">60</option>
            <option value="61">61</option>
            <option value="62">63</option>
            <option value="63">63</option>
            <option value="64">64</option>
            <option value="65">65</option>
            <option value="66">66</option>
            <option value="67">67</option>
            <option value="68">68</option>
            <option value="69">69</option>
            <option value="70">70</option>

          </select><br>
Weight(oz): <select name="weightoz" id="weightoz">
                    <option value="0">0</option>
              <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>
              <option value="13">13</option>
              <option value="14">14</option>
              <option value="15">15</option>
              </select><br>
Bait: <input type="text" name="bait"><br>
Type Of Fish: <select name="typeoffish" id="typeoffish">
          <option value="Bream">Bream</option>
          <option value="Catfish">Catfish</option>
          <option value="Chubb">Chubb</option>
          <option value="Common Carp">Common Carp</option>
          <option value="Crucian Carp">Crucian Carp</option>
          <option value="Eel">Eel</option>
          <option value="Fully Scaled Carp">Fully Scaled Carp</option>
          <option value="Ghost Carp">Ghost Carp</option>
          <option value="Mirror Carp">Mirror Carp</option>
          <option value="Perch">Perch</option>
          <option value="Pike" selected="selected">Pike</option>
          <option value="Roach">Roach</option>
          <option value="Rudd">Rudd</option>
          <option value="Tench">Tench</option>
          <option value="Zander">Zander</option>
          </select><br>
Notes: <input type="text" name="notes"><br>
Photo: <input type="file" name="photo"><br>
<input type="submit" value="Add">
</form> ';

?>
0
 
LVL 1

Author Comment

by:satmanuk
ID: 17781841
When i inser the form the username is entered as the primary key identifier instead of the username?

I need the username to be entered into the database as this is being echo'd out elsewhere on my site.
0
 
LVL 1

Author Comment

by:satmanuk
ID: 17781880
sorted it cheers
0
 
LVL 6

Expert Comment

by:Kosta
ID: 17782103
>Do i have to have the 2 connections called or is there a way to have just one?

No, you don't. Your connection is closed because it is opened inside a function, so it is not exist outside this function. You have to move the connection out of the function, so you will not need to open it twice:

<?php

include ( 'functions.php' );
$db = @mysql_pconnect('localhost', 'username, 'password');
@mysql_select_db('database');


if ( isset($_FILES['photo']) && ( strlen($_FILES['photo']['name']) > 0 ) ) {

   $photo = $_FILES['photo']['tmp_name'];
   $photo_name = $_FILES['photo']['name'];
   $photo_size = $_FILES['photo']['size'];
   $photo_type = $_FILES['photo']['type'];
   $photo_error = $_FILES['photo']['error'];
   $imgName = basename($photo).'.jpg';
   $dest = 'images/resized/'.$imgName;
   $image = resize_image('jpg',$photo,$dest,300,75);

if ( $image === false ) {

  echo 'Could not upload image<br>';
  echo $messages;

} else {

  $image = $dest;

  $query = 'INSERT INTO table(username, date, timehr, timemin, weightlb, weightoz, bait, typeoffish, notes, photo) VALUES( \''.$_POST['username'].'\', \''.$_POST['date'].'\', \''.$_POST['timehr'].'\', \''.$_POST['timemin'].'\', \''.$_POST['weightlb'].'\', \''.$_POST['weightoz'].'\', \''.$_POST['bait'].'\', \''.$_POST['typeoffish'].'\', \''.$_POST['notes'].'\', \'/'.$image.'\')';

  if ( @mysql_query($query) !== false ) {
   echo 'Your fish has been successfully added to the database<br>';
   echo $messages;
  } else {
   echo 'Error while executing query.<br>'.mysql_error();
  }
}
}

echo '<form enctype="multipart/form-data" action="add.php" method="POST">
Member Name: <select name="username">';
$query='SELECT username FROM tbl_users ORDER BY username';
$q=mysql_query($query) or die('Error while executing query.<br>'.mysql_error());

while($r=mysql_fetch_array($q) )
echo "\n<option>$r[username]</option>";


 echo '
</select>

<br>

Date: <input type="text" id="date" name="date" />
  <button id="trigger">...</button><br>
  <script type="text/javascript">
  Calendar.setup(
    {
      inputField  : "date",         // ID of the input field
      ifFormat    : "%Y-%m-%d",    // the date format
      button      : "trigger"       // ID of the button
    }
  );
</script>
Time(hr): <select name="timehr" id="timehr">
            <option value="00">00</option>
            <option value="01">01</option>
            <option value="02">02</option>
            <option value="03">03</option>
            <option value="04">04</option>
            <option value="05">05</option>
            <option value="06">06</option>
            <option value="07">07</option>
            <option value="08">08</option>
            <option value="09">09</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>
            </select><br>
Time(min): <select name="timemin" id="timemin">
            <option value="5">5</option>
            <option value="10">10</option>
            <option value="15">15</option>
            <option value="20">20</option>
            <option value="25">25</option>
            <option value="30">30</option>
            <option value="35">35</option>
            <option value="40">40</option>
            <option value="45">45</option>
            <option value="50">50</option>
            <option value="55">55</option>
            <option value="00" selected="selected">00</option>
          </select><br>
Weight(lb): <select name="weightlb" id="weightlb">
            <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>
            <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>
            <option value="32">32</option>
            <option value="33">33</option>
            <option value="34">34</option>
            <option value="35">35</option>
            <option value="36">36</option>
            <option value="37">37</option>
            <option value="38">38</option>
            <option value="39">39</option>
            <option value="40">40</option>
            <option value="41">41</option>
            <option value="42">42</option>
            <option value="43">43</option>
            <option value="44">44</option>
            <option value="45">45</option>
            <option value="46">46</option>
            <option value="47">47</option>
            <option value="48">48</option>
            <option value="49">49</option>
            <option value="50">50</option>
            <option value="51">51</option>
            <option value="52">52</option>
            <option value="53">53</option>
            <option value="54">54</option>
            <option value="55">55</option>
            <option value="56">56</option>
            <option value="57">57</option>
            <option value="58">58</option>
            <option value="59">59</option>
            <option value="60">60</option>
            <option value="61">61</option>
            <option value="62">63</option>
            <option value="63">63</option>
            <option value="64">64</option>
            <option value="65">65</option>
            <option value="66">66</option>
            <option value="67">67</option>
            <option value="68">68</option>
            <option value="69">69</option>
            <option value="70">70</option>

          </select><br>
Weight(oz): <select name="weightoz" id="weightoz">
                 <option value="0">0</option>
              <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>
              <option value="13">13</option>
              <option value="14">14</option>
              <option value="15">15</option>
              </select><br>
Bait: <input type="text" name="bait"><br>
Type Of Fish: <select name="typeoffish" id="typeoffish">
          <option value="Bream">Bream</option>
          <option value="Catfish">Catfish</option>
          <option value="Chubb">Chubb</option>
          <option value="Common Carp">Common Carp</option>
          <option value="Crucian Carp">Crucian Carp</option>
          <option value="Eel">Eel</option>
          <option value="Fully Scaled Carp">Fully Scaled Carp</option>
          <option value="Ghost Carp">Ghost Carp</option>
          <option value="Mirror Carp">Mirror Carp</option>
          <option value="Perch">Perch</option>
          <option value="Pike" selected="selected">Pike</option>
          <option value="Roach">Roach</option>
          <option value="Rudd">Rudd</option>
          <option value="Tench">Tench</option>
          <option value="Zander">Zander</option>
          </select><br>
Notes: <input type="text" name="notes"><br>
Photo: <input type="file" name="photo"><br>
<input type="submit" value="Add">
</form> ';

?>
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question