By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,817 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

Insert Select image from MySQL PHP -- what am I doing wrong

P: 63
OK! I've gone through a few tutorials and I cannot understand what I'm doing wrong

casting_registration.php

Expand|Select|Wrap|Line Numbers
  1. <table>
  2. <tr>
  3. <td>
  4.   <form enctype="multipart/form-data" action="thankyou.php" method="post" name="registrationform">
  5.       Choose a shows:
  6.  </td>
  7. <td>   <select name="shows"> 
  8.       <option selected value="no show selected">Select a show</option>
  9.       <option value="Bad Girls">Bad Girls</option>
  10.       <option value="Real World">Real World</option>
  11.       <option value="Road Rules">Road Rules</option>
  12.       <option value="Murder">Murder</option>
  13.       <option value="Steveo">Steveo</option>
  14.       <option value="The Simple Life">The Simple Life</option>
  15.     </select>
  16. </td>
  17. <td>
  18.     Gender:
  19. </td>
  20. <td>
  21.     <select name="gender">
  22.       <option selected value="no gender selected">Select your gender</option>
  23.       <option value="Female">Female</option>
  24.       <option value="Male">Male</option>
  25.     </select>   
  26. </td>
  27. <td>    
  28.     Age:
  29. </td>
  30. <td colspan="7">    
  31.     <select name="age">
  32.       <option selected value="no age selected">What is your age</option>
  33.       <option value="18">18</option>
  34.       <option value="19">19</option>
  35.       <option value="20">20</option>
  36.       <option value="21">21</option>
  37.       <option value="22">22</option>
  38.       <option value="23">23</option>
  39.       <option value="24">24</option>
  40.       <option value="25">25</option>
  41.       <option value="26">26</option>
  42.       <option value="27">27</option>
  43.       <option value="28">28</option>
  44.     </select>
  45. </td>
  46. </tr>
  47. <tr>
  48. <td>
  49.     First Name: 
  50. </td>
  51. <td>
  52.     <input type="text" name="first_name" />
  53. </td>
  54. <td>    
  55.     Last Name: 
  56. </td>
  57. <td>
  58.     <input type="text" name="last_name" />
  59. </td>
  60. <td>    
  61.     Email Address: 
  62. </td>
  63. <td>
  64.     <input type="text" name="email_address" />
  65. </td>
  66. </tr>
  67. <tr>
  68. <td>
  69.     Phone: 
  70. </td>
  71. <td>
  72.     <input type="text" name="phone_number" />
  73. </td>
  74. <td>
  75.     City: 
  76. </td>
  77. <td>
  78.     <input type="text" name="city" />
  79. </td>
  80. <td>
  81.     State: 
  82. </td>
  83. <td>
  84.     <input type="text" name="state" />
  85. </td>
  86. <td>
  87.     Zip: 
  88. </td>
  89. <td>
  90.     <input type="text" name="zip" />
  91. </td>
  92. </tr>
  93. <tr>
  94. <td colspan="7"> 
  95. Please choose an image to upload: <input type="file" name="imgdata" id="imgdata"><br>
  96. <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
  97.     <input type="submit" name="upload" id="upload" value="Add Record" />
  98.  
  99.   </form>
  100.   </td>
  101.   </tr>
  102.  </table>
  103.  
  104.  
Which takes you to thankyou.php

Expand|Select|Wrap|Line Numbers
  1.  
  2. if(isset($_POST['upload']) && $_FILES['imgdata']['size'] > 0)
  3. {
  4. $fileName = $_FILES['imgdata']['name'];
  5. $tmpName  = $_FILES['imgdata']['tmp_name'];
  6. $fileSize = $_FILES['imgdata']['size'];
  7. $fileType = $_FILES['imgdata']['type'];
  8.  
  9. $fp      = fopen($tmpName, 'r');
  10. $content = fread($fp, filesize($tmpName));
  11. $content = addslashes($content);
  12. fclose($fp);
  13. $encoded = chunk_split(base64_encode($content));
  14. $decoded = chunk_split(base64_decode($content));
  15.  
  16.     if(!get_magic_quotes_gpc())
  17.     {
  18.         $fileName = addslashes($fileName);
  19.     }
  20. }
  21.  
  22.             function my_insert_to_mysql_function($encoded)
  23.             {
  24.                 $con = mysql_connect("localhost","kronus","gohomE2k");
  25.                 if (!$con)
  26.                 {
  27.                     die('Could not connect: ' . mysql_error());
  28.                 }
  29.                 else
  30.                 {
  31.                     mysql_select_db("kronus", $con);
  32.                     $my_sql_string = "INSERT INTO casting_registration 
  33.                     (SHOWS, GENDER, AGE, FIRST_NAME, LAST_NAME, EMAIL_ADDRESS, PHONE_NUMBER, CITY, STATE, ZIP, PICTURE) 
  34.                     VALUES
  35.                     ('$_REQUEST[shows]', '$_REQUEST[gender]', '$_REQUEST[age]', '$_REQUEST[first_name]', '$_REQUEST[last_name]', '$_REQUEST[email_address]', '$_REQUEST[phone_number]', '$_REQUEST[city]', '$_REQUEST[state]', '$_REQUEST[zip]', '$encoded')";
  36.  
  37.                     if (!mysql_query($my_sql_string,$con))
  38.                       {
  39.                         // echo '<br /> Your SQL query is: ' .$my_sql_string;                
  40.                           die('Error: ' . mysql_error());
  41.                         // echo '<br /> Your SQL query is: ' .$my_sql_string;                
  42.                       }
  43.                     // echo 'Errors reported' .     mysql_error();
  44.                     // echo '<br /> Your SQL query is: ' .$my_sql_string;                
  45.                 }
  46.                 mysql_close($con);
  47.             }
  48. </script>
  49. <script language="php">
  50.  
  51.             function my_select_to_mysql_function($encoded)
  52.             {
  53.                 $con = mysql_connect("localhost","kronus","gohomE2k");
  54.                 if (!$con)
  55.                 {
  56.                     die('Could not connect: ' . mysql_error());
  57.                 }
  58.                 mysql_select_db("kronus", $con);
  59.                 $my_sql_string = "SELECT * FROM casting_registration 
  60.                 WHERE SHOWS='" . $_REQUEST[shows] . "' AND GENDER= '" . $_REQUEST[gender] . "' AND AGE= '" . $_REQUEST[age] . "' AND FIRST_NAME= '" . $_REQUEST[first_name] . "' AND LAST_NAME= '" . $_REQUEST[last_name] . "' AND EMAIL_ADDRESS= '" . $_REQUEST[email_address] . "' AND CITY= '" . $_REQUEST[city] . "' AND  STATE= '" . $_REQUEST[state] . "' AND  ZIP= '" . $_REQUEST[zip] . "' AND  PICTURE= '" . $encoded . "'
  61.                 LIMIT 0 , 30";
  62.                 if (!mysql_query($my_sql_string,$con))
  63.                   {
  64.                         die('Error: ' . mysql_error());
  65.                         // echo 'Errors reported' .     mysql_error();
  66.                   }
  67.                 else
  68.                   {
  69.                         // echo 'Your SQL query is: ' .$my_sql_string;
  70.                         echo "<form action='registered.php' method='post'>";
  71.                         $someVar = mysql_query($my_sql_string,$con);
  72.                         $i = '1';
  73.                         while($row = mysql_fetch_array($someVar))
  74.                         {
  75.                         echo "<input type='textfield' name='shows" .$i. "' value='" .$row['SHOWS'] . "'><input type='textfield' name='gender" .$i. "' value='" .$row['GENDER'] . "'><input type='textfield' name='age" .$i. "' value='" .$row['AGE'] . "'><input type='textfield' name='first_name" .$i. "' value='" . $row['FIRST_NAME']. "'><input type='textfield' name='last_name" .$i. "' value='" . $row['LAST_NAME']. "'><br /><input type='textfield' name='email_address" .$i. "' value='" . $row['EMAIL_ADDRESS']. "'><input type='textfield' name='phone_number" .$i. "' value='" . $row['PHONE_NUMBER']. "'><input type='textfield' name='city" .$i. "' value='" . $row['CITY']. "'><input type='textfield' name='state" .$i. "' value='" . $row['STATE']. "'><input type='textfield' name='zip" .$i. "' value='" . $row['ZIP']. "'><br /><img src='gateway.php?picture='" .$encoded . "'><br />";
  76.  
  77.                         echo "<input type='hidden' name='hidden_registered_id" .$i. "' value='" .$row['REGISTERED_ID'] . "'><input type='hidden' name='hidden_shows" .$i. "' value='" .$row['SHOWS'] . "' id='hidden_shows" .$i. "' value='" .$row['SHOWS'] . "'><input type='hidden' name='hidden_gender" .$i. "' value='" .$row['GENDER'] . "' id='hidden_gender" .$i. "' value='" .$row['GENDER'] . "'><input type='hidden' name='hidden_age" .$i. "' value='" .$row['AGE'] . "' id='hidden_age" .$i. "' value='" .$row['AGE'] . "'><input type='hidden' name='hidden_first_name" .$i. "' value='" . $row['FIRST_NAME']. "' id='hidden_first_name" .$i. "' value='" . $row['FIRST_NAME']. "'><input type='hidden' name='hidden_last_name" .$i. "' value='" . $row['LAST_NAME']. "' id='hidden_last_name" .$i. "' value='" . $row['LAST_NAME']. "'><input type='hidden' name='hidden_email_address" .$i. "' value='" . $row['EMAIL_ADDRESS']. "' id='hidden_email_address" .$i. "' value='" . $row['EMAIL_ADDRESS']. "'><input type='hidden' name='hidden_phone_number" .$i. "' value='" . $row['PHONE_NUMBER']. "' id='hidden_phone_number" .$i. "' value='" . $row['PHONE_NUMBER']. "'><input type='hidden' name='hidden_city" .$i. "' value='" . $row['CITY']. "' id='hidden_city" .$i. "' value='" . $row['CITY']. "'><input type='hidden' name='hidden_state" .$i. "' value='" . $row['STATE']. "' id='hidden_state" .$i. "' value='" . $row['STATE']. "'><input type='hidden' name='hidden_zip" .$i. "' value='" . $row['ZIP']. "' id='hidden_phone_zip" .$i. "' value='" . $row['ZIP']. "'><p>";
  78.                         $i++;
  79.                         }
  80.                         echo "<input type='hidden' name='my_i' value='" .$i. "'><input type='submit' name='update_button' value='Update Record'>";
  81.                         echo "</form>";                      
  82.                   }
  83.                 // echo 'Errors reported' .     mysql_error();
  84.                 mysql_close($con);
  85.             }
  86.  
  87.  
  88.           function welcome_writing_function()
  89.             {
  90.                 echo 'Thank you, ';
  91.                 echo $_REQUEST['first_name'];
  92.                 echo '&nbsp;';
  93.                 echo $_REQUEST['last_name'];
  94.                 echo ', for registering to the casting department of ';
  95.                 echo '&nbsp;';
  96.                 echo $_REQUEST['shows'];
  97.                 echo '&nbsp;';
  98.             }
  99.  
  100.           function comments_writing_function()
  101.             {
  102.                 echo $_REQUEST['picture'];
  103.                 echo '<p />';
  104.                 echo 'We will contact you at: ';
  105.                 echo $_REQUEST['email_address'];
  106.                 echo '<br />';
  107.                 echo $_REQUEST['phone_number'];
  108.                 echo '<br />';
  109.             }
  110.  
  111.           function first_half_writing_function()
  112.             {
  113.                 echo $_REQUEST['gender'];
  114.                 echo '&nbsp;';
  115.                 echo $_REQUEST['age'];
  116.                 echo '&nbsp;';
  117.                 echo $_REQUEST['first_name'];
  118.                 echo '&nbsp;';
  119.                 echo $_REQUEST['last_name'];
  120.                 echo '<br />';
  121.                 echo $_REQUEST['email_address'];
  122.                 echo '<br />';
  123.                 echo $_REQUEST['phone_number'];
  124.                 echo '<br />';
  125.                 echo $_REQUEST['city'];
  126.                 echo '<br />';
  127.                 echo $_REQUEST['state'];
  128.                 echo '<br />';
  129.                 echo $_REQUEST['zip'];
  130.                 echo '<br />';
  131.                 echo $_REQUEST['picture'];
  132.                 echo '&nbsp;';
  133.             }
  134.  
  135.           function second_half_writing_function()
  136.             {
  137.                 $words1 = ' on ';
  138.                 $words2 = date('m-d-y');
  139.                 $words3 = date('G') - 3;
  140.                 $words4 = date(':i:s');
  141.                 $time_right_now = $words1. ' ' .$words2. ' ' .$words3.$words4. ' PST';
  142.                 echo $time_right_now;
  143.             }
  144.  
  145.     </script>
  146. <div style="position:absolute; display:inline; top:500px; left:70px;">
  147.   <script language="php">
  148.         welcome_writing_function();
  149.         my_insert_to_mysql_function($encoded);
  150.         my_select_to_mysql_function($encoded);
  151.     </script>
  152. </div>
  153.  
  154.  
And here is the gateway.php which is supposed to be able to allow you to view the image via the thankyou.php

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.                 $con = mysql_connect("localhost","username","password");
  4.                 if (!$con)
  5.                 {
  6.                     die('Could not connect: ' . mysql_error());
  7.                 }
  8.                 mysql_select_db("username", $con);
  9.  
  10.                 $my_sql_string = "select picture from casting_registration where registered_id ='" . $_REQUEST['registered_id'] . "'";
  11.                 if (!mysql_query($my_sql_string,$con))
  12.                   {
  13.                         die('Error: ' . mysql_error());
  14.                   }
  15.                 else
  16.                   {
  17.                         // echo 'Your SQL query is: ' .$my_sql_string;
  18.                         $someVar = mysql_query($my_sql_string,$con);
  19.                         $i = '1';
  20.                         while($row = mysql_fetch_array($someVar))
  21.                         {
  22.                             $encodeddata = $row[picture];
  23.                         }
  24.                   }
  25.                 // echo 'Errors reported' .     mysql_error();
  26.                 mysql_close($con);
  27.                 header("Content-type: image/jpeg");
  28.                 echo base64_decode($encodeddata);
  29. ?>
  30.  
  31.  
The textfields are populated just fine on the select, but it returns a question mark icon and looking in the db itself, I can see by the sizes that it actually uploaded the file, but when I export the the table to Microsoft .xls file, it has a bunch of jiberish binary code instead of an image.

Why can't I get this? Please, someone help! I've been trying for a week now and I do not understand what else I can do.
May 29 '07 #1
Share this Question
Share on Google+
2 Replies


P: 63
OK, I believe that the issue is with either how I'm inputting the data into the db (which I believe is the true problem -- due to jiberish being in the xls output file via phpadmin) or my gateway.php script that is the issue, because I was able to remove the question mark icon by correcting some syntax in my thankyou.php

Expand|Select|Wrap|Line Numbers
  1.  
  2. <img src='gateway.php?registered_id=".$row['REGISTERED_ID']."'>
  3.  
  4.  
After doing a view source on the web page, which successfully returns the correct info

Expand|Select|Wrap|Line Numbers
  1.  
  2. gateway.php?registered_id=63
  3.  
  4.  
May 29 '07 #2

P: 63
OK, I figured it out

I'm entering only the name of the file into the db and copying the image to an images directory

Expand|Select|Wrap|Line Numbers
  1. <script language="php">
  2.  
  3. if(isset($_POST['upload']) && $_FILES['imgdata']['size'] > 0)
  4. {
  5. $fileName = $_FILES['imgdata']['name'];
  6. $tmpName  = $_FILES['imgdata']['tmp_name'];
  7. $fileSize = $_FILES['imgdata']['size'];
  8. $fileType = $_FILES['imgdata']['type'];
  9.  
  10. $fp      = fopen($tmpName, 'r');
  11. $content = fread($fp, filesize($tmpName));
  12. $content = addslashes($content);
  13. $dbfilename = "" .rand().$_REQUEST['first_name'].$_REQUEST['last_name'].$_FILES["imgdata"]["name"];
  14. move_uploaded_file($_FILES["imgdata"]["tmp_name"],
  15. "images/" .$dbfilename);
  16.  
  17. fclose($fp);
  18. }
  19.  
  20.             function my_insert_to_mysql_function($dbfilename)
  21.             {
  22.                 $con = mysql_connect("localhost","username","password");
  23.                 if (!$con)
  24.                 {
  25.                     die('Could not connect: ' . mysql_error());
  26.                 }
  27.                 else
  28.                 {
  29.                     mysql_select_db("dbname", $con);
  30.                     $my_sql_string = "INSERT INTO casting_registration 
  31.                     (SHOWS, GENDER, AGE, FIRST_NAME, LAST_NAME, EMAIL_ADDRESS, PHONE_NUMBER, CITY, STATE, ZIP, PICTURE) 
  32.                     VALUES
  33.                     ('$_REQUEST[shows]', '$_REQUEST[gender]', '$_REQUEST[age]', '$_REQUEST[first_name]', '$_REQUEST[last_name]', '$_REQUEST[email_address]', '$_REQUEST[phone_number]', '$_REQUEST[city]', '$_REQUEST[state]', '$_REQUEST[zip]', '".$dbfilename."')";
  34.  
  35.                     if (!mysql_query($my_sql_string,$con))
  36.                       {
  37.                         // echo '<br /> Your SQL query is: ' .$my_sql_string;                
  38.                           die('Error: ' . mysql_error());
  39.                         // echo '<br /> Your SQL query is: ' .$my_sql_string;                
  40.                       }
  41.                     // echo 'Errors reported' .     mysql_error();
  42.                     // echo '<br /> Your SQL query is: ' .$my_sql_string;                
  43.                 }
  44.                 $id= mysql_insert_id();
  45.                 echo '<input type=hidden name=id value="' . $id . '">';
  46.                 mysql_close($con);
  47.             }
  48. </script>
  49. <script language="php">
  50.  
  51.             function my_select_to_mysql_function($dbfilename)
  52.             {
  53.                 $con = mysql_connect("localhost","username","password");
  54.                 if (!$con)
  55.                 {
  56.                     die('Could not connect: ' . mysql_error());
  57.                 }
  58.                 mysql_select_db("dbname", $con);
  59.                 $my_sql_string = "SELECT * FROM casting_registration 
  60.                 WHERE SHOWS='" . $_REQUEST[shows] . "' AND GENDER= '" . $_REQUEST[gender] . "' AND AGE= '" . $_REQUEST[age] . "' AND FIRST_NAME= '" . $_REQUEST[first_name] . "' AND LAST_NAME= '" . $_REQUEST[last_name] . "' AND EMAIL_ADDRESS= '" . $_REQUEST[email_address] . "' AND CITY= '" . $_REQUEST[city] . "' AND  STATE= '" . $_REQUEST[state] . "' AND  ZIP= '" . $_REQUEST[zip] . "'
  61.                 LIMIT 0 , 30";
  62.                 if (!mysql_query($my_sql_string,$con))
  63.                   {
  64.                         die('Error: ' . mysql_error());
  65.                         // echo 'Errors reported' .     mysql_error();
  66.                   }
  67.                 else
  68.                   {
  69.                         // echo 'Your SQL query is: ' .$my_sql_string;
  70.                         echo "<form action='registered.php' method='post'>";
  71.                         $someVar = mysql_query($my_sql_string,$con);
  72.                         $i = '1';
  73.                         while($row = mysql_fetch_array($someVar))
  74.                         {
  75.                         echo "<input type='textfield' name='shows" .$i. "' value='" .$row['SHOWS'] . "'><input type='textfield' name='gender" .$i. "' value='" .$row['GENDER'] . "'><input type='textfield' name='age" .$i. "' value='" .$row['AGE'] . "'><input type='textfield' name='first_name" .$i. "' value='" . $row['FIRST_NAME']. "'><input type='textfield' name='last_name" .$i. "' value='" . $row['LAST_NAME']. "'><br /><input type='textfield' name='email_address" .$i. "' value='" . $row['EMAIL_ADDRESS']. "'><input type='textfield' name='phone_number" .$i. "' value='" . $row['PHONE_NUMBER']. "'><input type='textfield' name='city" .$i. "' value='" . $row['CITY']. "'><input type='textfield' name='state" .$i. "' value='" . $row['STATE']. "'><input type='textfield' name='zip" .$i. "' value='" . $row['ZIP']. "'><br /><img src='images/" .$dbfilename."' height='200px'><br />";
  76.  
  77.                         echo "<input type='hidden' name='hidden_registered_id" .$i. "' value='" .$row['REGISTERED_ID'] . "'><input type='hidden' name='hidden_shows" .$i. "' value='" .$row['SHOWS'] . "' id='hidden_shows" .$i. "' value='" .$row['SHOWS'] . "'><input type='hidden' name='hidden_gender" .$i. "' value='" .$row['GENDER'] . "' id='hidden_gender" .$i. "' value='" .$row['GENDER'] . "'><input type='hidden' name='hidden_age" .$i. "' value='" .$row['AGE'] . "' id='hidden_age" .$i. "' value='" .$row['AGE'] . "'><input type='hidden' name='hidden_first_name" .$i. "' value='" . $row['FIRST_NAME']. "' id='hidden_first_name" .$i. "' value='" . $row['FIRST_NAME']. "'><input type='hidden' name='hidden_last_name" .$i. "' value='" . $row['LAST_NAME']. "' id='hidden_last_name" .$i. "' value='" . $row['LAST_NAME']. "'><input type='hidden' name='hidden_email_address" .$i. "' value='" . $row['EMAIL_ADDRESS']. "' id='hidden_email_address" .$i. "' value='" . $row['EMAIL_ADDRESS']. "'><input type='hidden' name='hidden_phone_number" .$i. "' value='" . $row['PHONE_NUMBER']. "' id='hidden_phone_number" .$i. "' value='" . $row['PHONE_NUMBER']. "'><input type='hidden' name='hidden_city" .$i. "' value='" . $row['CITY']. "' id='hidden_city" .$i. "' value='" . $row['CITY']. "'><input type='hidden' name='hidden_state" .$i. "' value='" . $row['STATE']. "' id='hidden_state" .$i. "' value='" . $row['STATE']. "'><input type='hidden' name='hidden_zip" .$i. "' value='" . $row['ZIP']. "' id='hidden_phone_zip" .$i. "' value='" . $row['ZIP']. "'><p>";
  78.                         $i++;
  79.                         }
  80.                         echo "<input type='hidden' name='my_i' value='" .$i. "'><input type='submit' name='update_button' value='Update Record'>";
  81.                         echo "</form>";                      
  82.                   }
  83.                 // echo 'Errors reported' .     mysql_error();
  84.                 mysql_close($con);
  85.             }
  86. </script>
  87.   <script language="php">
  88.         welcome_writing_function();
  89.         my_insert_to_mysql_function($dbfilename);
  90.         my_select_to_mysql_function($dbfilename);
  91.     </script>
  92.  
  93.  
Hope this helps someone out :-)
May 30 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.