[SOLVED] Data From Form To Database Input Problems

This is a discussion on "[SOLVED] Data From Form To Database Input Problems" within the PHP Forum section. This forum, and the thread "[SOLVED] Data From Form To Database Input Problems are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Oct 10th, 2007, 13:25
Reputable Member
Join Date: Oct 2007
Location: Liverpool UK
Age: 29
Posts: 227
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Data From Form To Database Input Problems

I recieved help yesterday with this problem, and thought i had solved the issue but i quickly realised i was wrong.

I have a php webpage & mysql database, and a fully working install of Php\Appache\Mysql set up via localhost.

In my Php file a have a form with text fileds and list menus, that i need to recieve data & transfer the data into my sql database.

Problem 1) The data is not transferring through no matter how i create the records!

Problem 2) A Few of my list menus are not showing up in browser view!

Problem 3) When i insert a submit button it dows not show up in browser view!

My form is split inbetween to columns of a dreamweave table, could this be causing the button problem?


Below is my code:

PHP: Select all

<?php
require_once('Connections/conndb.php'); 
mysql_select_db($database_conndb$conndb) or die("oh no oh no, the database could't be selected! It's all going wrong");
// Okay, your submit button is named "button" so we check that this has been clicked, if it has we can start the form processing 
if (!empty($_POST['button'])) // !empty is a double check, checking for existence AND a value other than false, NULL, 0 or "" 

    
// I am unsure about your magic quotes setting so I'll use this to strip slashes and escape correctly 
     
    
$needStrip get_magic_quotes_gpc(); 
     
    
// Okay, let's assume that all form values are going into the same mySQL table 
    // Create an array of the form fields -- using the form code you have below, it looks like this 
     
    
$fields = array(); 
     
    
// The array key is the name of the form element 
     
    
$fields['name'] = array('type'        => 's',       // The type of value - we'll use s for string or n for number (hard for a form to be anything else) 
                               
'required'    => true,    // Is the field *required*, we will stop the script if it's not set if this is true 
                               
'column'        => 'signame'    // The mysql data column where the value will go 
                               
); 
    
$fields['sex'] = array('type'        => 's'
                               
'required'    => true
                               
'column'        => 'sigsexmalefemale' 
                               
); 
    
$fields['username'] = array('type'        => 's'
                               
'required'    => true
                               
'column'        => 'sigusername' 
                               
);
 
$fields['password'] = array('type'        => 's'
                               
'required'    => true
                               
'column'        => 'sigpassword' 
                               
);
 
$fields['hobbies'] = array('type'        => 'n'
                               
'required'    => true
                               
'column'        => 'd' 
                               
); 
 
$fields['email'] = array('type'        => 's'
                               
'required'    => true
                               
'column'        => 'sigemail' 
                               
);
 
$fields['turnon'] = array('type'        => 's'
                               
'required'    => true
                               
'column'        => 'sigwhatturnsmeon' 
                               
);
 
$fields['looklike'] = array('type'        => 's'
                               
'required'    => true
                               
'column'        => 'sigwhatlooklike' 
                               
);
 
$fields['iam'] = array('type'        => 'n'
                               
'required'    => true
                               
'column'        => 'sigiam' 
                               
);
 
$fields['interestedin'] = array('type'        => 'n'
                               
'required'    => true
                               
'column'        => 'siginterested' 
                               
);
 
$fields['sexsubject'] = array('type'        => 'n'
                               
'required'    => true
                               
'column'        => 'sigfor' 
                               
); 
          
   
$fields['area'] = array('type'        => 's'
                               
'required'    => true
                               
'column'        => 'sigarea' 
                               
);                                                                
    
// Etc.. and so on with the field names (excluding the submit of course)  
     
    //Now, we will loop through each of the fields as listed above ^ and create the database query 
     
    // To keep track of errors, we will have an error array 
     
    
$errors = array(); 
     
    
// The query bits which will be built on below 
    
$cols ''
    
$vals ''
     
    foreach (
$fields as $field => $dets// $field will hold the field name and $dets an array of the details 
    

        
// remove preceding and trailing white space 
        
$field trim($field); 
         
        
// first check, is the value required AND empty? 
        
if ($dets['required'] && empty($_POST[$field])) { 
            
$errors[] = $field ' is a required field!'// Add to the errors 
            
continue; // stop this loop and start with the next 
        

         
        
// next check, data type (only check numbers as all post variables are strings 
        
if ($dets['type'] == 'n' && !is_numeric($_POST[$field])) { 
            
$errors[] = $field ' should be a number!'
            continue; 
        } 
         
        
// Don't bother adding this to the query if we have errors already of if there is no value 
        
if (empty($_POST['field']) || count($errors)) 
            continue; 
             
        
// Now add the SQL for the insert 
         
        // Remove slashes if they exist 
        
$_POST[$field] = $needStrip stripslashes($_POST[$field]) : $_POST[$field]; 
         
        
// Escape the data for safe SQL insertion 
        
$_POST[$field] = mysql_real_escape_string($_POST[$field]); 
         
        
// Add a comma to separate the values if there is one there already 
        
if (!empty($cols)) { 
        
$cols .= ', '
        
$vals .= ', '
        } 
         
        
$cols .= $dets['column']; // the columns as in INSERT into Table (col1, col2) 
        
$vals .= $dets['type'] == 'n' && is_numeric($_POST[$field]) ? $_POST[$field] : "'{$_POST[$field]}'"// the values as in VALUES ('this', 'that', 1) 
     
    


if (isset(
$_POST['select4'])) {
 
$month $_POST['select4'];
}
if (isset(
$_POST['select5'])) {
 
$day $_POST['select5'];
}
if (isset(
$_POST['select6'])) {
 
$year $_POST['select6'];
}
$dob $day."/".$month."/".$year;
$cols .= ', sigage';
$vals .= ", '$dob'";
    
// Now we have one of two things 1) A list of columns and values 2) errors 
     
    
if (!count($errors) && !empty($cols) && !empty($vals)) {// No errors? and some values and columns? Continue to add the query         
     
        // contruct the query 
        
$query "INSERT INTO details($cols) VALUES($vals)"
         
        
// run the query 
         
mysql_select_db($database_conndb$conndb); 
        
$result mysql_query($query) or die(mysql_error()); 
         
        
// success? 
         
        
$message "Yay, you have been inserted as " mysql_insert_id(); // The insert ID 
  
   
$insertGoTo "profile.php";
  if (isset(
$_SERVER['QUERY_STRING'])) {
    
$insertGoTo .= (strpos($insertGoTo'?')) ? "&" "?";
    
$insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  
header(sprintf("Location: %s"$insertGoTo));
         
    }  else { 
// Oh oh! errors 
     
        // create an error string 
        
$message '<strong>The following errors are ruining my feng shui man!</strong> 
                    <ol>'

         
        foreach (
$errors as $err// $err will have the error message from above 
            
$message .= '<li>' $err '</li>'
             
        
$message .= '</ol>'
    } 
}
// Now get the info to fill out the form
 
 
        
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtmlxml:lang="en">
<head>
<title>Create Your profile</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/1.css" type="text/css" media="screen,projection" />
<style type="text/css">
<!--
.style1 {color: #FF00FF}
.style2 {color: #FF3399}
.style3 {color: #0474F1; }
.style4 {font-family: Tahoma}
.style5 {font-family: Verdana, Arial, Helvetica, sans-serif}
-->
</style>
<script src="file:///C|/webserver/Apache2/htdocs/Scripts/AC_RunActiveContent.js" type="text/javascript"></script> 
</head>
 
<body bgcolor="white">
  <div id="header">
 
   <h1 class="right">&nbsp;</h1>
   <h1>&nbsp;</h1>
 
</div>
 
 <ul id="nav">
   
   <li class="right"><input value="Search..." type="text" /></li>
   
   <li><a href="MainPage.php">MainPage</a></li>
   
   <li><a href="Login.php">LogIn</a></li>
   
   <li><a href="admin/Sign Up.php">SignUp</a></li>
   
   <li><a href="Chat.php">Chat</a></li>
 
</ul>
 
 <div class="clear" />
 
 <div id="content">
   <p>
        <?php
// Connect to database
$errmsg "";
if (! @
mysql_connect("localhost","trainee","abc123")) {
        
$errmsg "Cannot connect to database";
        }
@
mysql_select_db("test");
// First run ONLY - need to create table by uncommenting this
// Or with silent @ we can let it fail every sunsequent time ;-)
$q = <<<CREATE
create table pix (
    pid int primary key not null auto_increment,
    title text,
    imgdata longblob)
CREATE;
@
mysql_query($q);
// Insert any new image into database
if ($_REQUEST[completed] == 1) {
        
// Need to add - check for large upload. Otherwise the code
        // will just duplicate old file ;-)
        // ALSO - note that latest.img must be public write and in a
        // live appliaction should be in another (safe!) directory.
        
move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
        
$instr fopen("latest.img","rb");
        
$image addslashes(fread($instr,filesize("latest.img")));
        if (
strlen($instr) < 149000) {
                
mysql_query ("insert into pix (title, imgdata) values (\"".
                
$_REQUEST[whatsit].
                
"\", \"".
                
$image.
                
"\")");
        } else {
                
$errmsg "Too large!";
        }
}
// Find out about latest image
$gotten = @mysql_query("select * from pix order by pid desc limit 1");
if (
$row = @mysql_fetch_assoc($gotten)) {
        
$title htmlspecialchars($row[title]);
        
$bytes $row[imgdata];
} else {
        
$errmsg "There is no image in the database yet";
        
$title "no database image available";
        
// Put up a picture of our training centre
        
$instr fopen("../wellimg/ctco.jpg","rb");
        
$bytes fread($instr,filesize("../wellimg/ctco.jpg"));
}
// If this is the image request, send out the image
if ($_REQUEST[gim] == 1) {
        
header("Content-type: image/jpeg");
        print 
$bytes;
        exit ();
        }
?>
   </p>
       
      <h2>Welcome To Your Profile!</h2>
      <font color="red">
      <?= $errmsg ?>
      </font>
      <center>
        <img src="?gim=1" width="144" /><br />
        <?= $title ?>
      </center>
      <hr />
      <h2>Upload A Photo Of Yourself &amp; Enhance Your Responce From Other Members</h2>
      <form enctype="multipart/form-data" method="post">
        <p>
          <input type="hidden" name="MAX_FILE_SIZE" value="150000" />
          <input type="hidden" name="completed" value="1" />
          <span class="style2"><font face="Tahoma">Please choose an image to upload:</font></span>
          <input type="file" name="imagefile" />
        </p>
        <p> <br />
            <span class="style2"><font face="Tahoma">Please enter the title of that picture:</font></span>
            <input name="whatsit" />
        </p>
        <p> <br />
            <span class="style2"><font face="Tahoma">Click Submit To Upload Your Image:</font></span>
            <input type="submit" name="imageSubmit" value="Submit Image" />
        </p>
      </form>
    <form name="form1" id="form1" method="POST">
        <table width="828" height="557" border="0" align="center">
          <tr>
            <td height="45">
   <h2>To Contact &amp; Be Contacted By Other Members Please Create Your Profile Id</h2>
   </td>
          </tr><tr>
            <td height="504" valign="top">
   <p>&nbsp;</p>
             
    <table width="736" height="484" border="0">
                  <tr>
                    <td valign="top" class="style4">
    
                        <p class="style2">Please Input Your First Name (no capitals)</p>
                     
                          <span class="style2">
                          <label>
                          <input type="text" name="name" id="name" maxlength="20"/>
                          </label>
                          </span>
                
                      <p class="style2">Please Input Your Age (digits only)</p>
                      
                          <span class="style2">
                          <label>
                          <input type="text" name="age" id="age" maxlength="20" value="" />
                          </label>
                          </span>
                        <p class="style2">Please Input Your Sex</p>
                        <p>
                            <span class="style2">
                            <label>
       <select name="sex">
       <option value="male">Male</option>
       <option value="female">Female</option>
       </select>
                           
                            </label>
                          </span></p>
                        <p class="style2">Please Input Your Phone number (optional)</p>
                        <p class="style2">Please Input Your User Name </p>
                        <p class="style2">(user name must be same as your login name)</p>
                        <p>
                            <span class="style2">
                            <label>
                            <input type="text" name="username" id="username" value="" maxlength="45" />
                            </label>
                          </span></p>
                        <p class="style2">Please Input Your Password</p>
                        <p class="style2">(password must be the same as your login password)</p>
                        <p>
                            <span class="style2">
                            <label>
                            <input type="password" name="password" id="password" value="" maxlength="12" />
                            </label>
                          </span></p>
                        <p>
                            <label></label>
                            <span class="style2">Please Describe Your Hobbies &amp; Interests</span></p>
                        <p>
                          <textarea name="hobbies" id="hobbies" cols="45" rows="5"></textarea>
                        </p>
                  
                      <p class="style2">Please Input Your Email Address</p>
                    
                        <p>
                          <label>
                            <input type="text" name="email" id="email" value="" maxlength="45" />
                          </label>
                        </p>
                   
                      <p class="style2">
                        <input type="submit" name="button" id="button" value="Submit" />
                      </p>
     </td><td valign="top" class="style4">
                        <p class="style2"><span class="style2">Describe What Turns You On &amp; Why </span></p>
                  
                          <span class="style2">
                          <label></label>
                        </span>
                     
                          <span class="style2">
                          <label>
                          <textarea name="turnon" id="turnon" cols="45" rows="5"></textarea>
                          </label>
                          </span>
                   
                     
                          <span class="style2">
                          <label>Please Tell Us What You Look Like<br />
                          <br />
                          <textarea name="looklike" id="looklike" cols="45" rows="5"></textarea>
                          <br />
                          <br />
                          </label>
                          </span>
                        <p class="style2">I Am</p>
                        <p>
                            <span class="style2">
                            <label>
       
                            <select name="iam" id="iam">
       
       <?php
        $query 
"SELECT * FROM tbl_iam ORDER BY imid ASC";
        
$result mysql_query($query) or die(mysql_error());
        
        while (
$row mysql_fetch_assoc($result))
         echo 
'<option value="'$row['imid'], '">'$row['imname'], '</option>';
    
         
?>
                            </select>
       
       
                            </label>
                          </span></p>
                        <p>Interested In</p>
                        <p>
                          <label>
        
                          <select name="interestedin" id="interestedin">
        <?php
                             $query 
"SELECT * FROM tbl_interested ORDER BY intid ASC";
        
$result mysql_query($query) or die(mysql_error());
        
        while (
$row mysql_fetch_assoc($result))
         echo 
'<option value="'$row['intid'], '">'$row['intname'], '</option>';
       
?>
                          </select>
                                  </label>
                        </p>
                        <p class="style2">Looking For (Sexual Interests)</p>
                        <p>
                            <span class="style2">
                            <label>
       
                            <select name="sexsubject" id="sexsubject">
       <?php
                             $query 
"SELECT * FROM tbl_for ORDER BY forid ASC";
        
$result mysql_query($query) or die(mysql_error());
        
        while (
$row mysql_fetch_assoc($result))
         echo 
'<option value="'$row['forid'], '">'$row['forname'], '</option>';
         
         
?>
                            </select>
                            </label>
                          </span></p>
                    
                      <p class="style2">Please Input Your Area</p>
                     
                          <span class="style2">
                          <label>
                          <select name="area" id="area">
        <?php
         $query 
"SELECT * FROM tbl_area ORDER BY areaname ASC";
        
$result mysql_query($query) or die(mysql_error());
        
        while (
$row mysql_fetch_assoc($result))
         echo 
'<option value="'$row['areaname'], '">'$row['areaname'], '</option>';
       
?>
                          </select>
                          </label>
                          </span>
                 
                      <p class="style2">Please Input Your Postcode</p>
                      <p class="style2">(no capital letters) </p>
                    
                          <span class="style2">
                          <label>
                          <input type="text" name="postcode" id="postcode" />
                          </label>
                          </span>
                  
                     
                        <p>
                          <label></label>
                          <label></label>
                        </p>
                        <p align="center"> 
                          <label></label>
                        </p>
                        <p class="style3">&nbsp;</p>
                      <p>&nbsp;</p>
                      <p>&nbsp;</p>
            
     </td></tr></table>
              <h2 align="center" class="style5">Remember this is your space so be accurate with your data to ensure results!</h2>
              <p>&nbsp;</p>
     </td>
          </tr>
        </table>   
  
        
    </form>
      <hr />
<p>&nbsp;</p>
   <p>&nbsp;</p>
       
<p>&nbsp;</p>
    
          
   </div>
<div id="footer">
     
       <p>Site Design by <a href="http://www.sixshootermedia.com">AJL Online Services Business Developement Specialists</a>.<br />
                              <!-- you can delete below here -->
© All  copyrights reserved.</p>
     
</div>
</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Oct 12th, 2007, 10:12
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Data From Form To Database Input Problems

What do you mean browser view?

Are you viewing your page using localhost?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Oct 12th, 2007, 13:02
Reputable Member
Join Date: Oct 2007
Location: Liverpool UK
Age: 29
Posts: 227
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Data From Form To Database Input Problems

I have solved my problem now but thanks anyway!!!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!