Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 14th, 2011, 09:49 AM
Authorized User
 
Join Date: Aug 2009
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default PHP Insert form problem

Hello,
I was hoping I could get a little help on the following:
I have a form to insert a job into the job table which is accessed by users on the users table. The form works and adds data to the job table but is not inserting the user_id from the user so I can use it in a join.

Here is my form:

Code:
<?php
include 'dbc.php';
page_protect();

if($_POST['submit'] == 'Submit Job') 
 
{ 
/******************* Filtering/Sanitizing Input *****************************
This code filters harmful script code and escapes data of all POST data
from the user submitted form.
*****************************************************************/
foreach($_POST as $key => $value) {
	$data[$key] = filter($value);
}

/********************* RECAPTCHA CHECK *******************************
This code checks and validates recaptcha
****************************************************************/
 require_once('recaptchalib.php');
     
      $resp = recaptcha_check_answer ($privatekey,
                                      $_SERVER["REMOTE_ADDR"],
                                      $_POST["recaptcha_challenge_field"],
                                      $_POST["recaptcha_response_field"]);

      if (!$resp->is_valid) {
        die ("<h3>Image Verification failed!. Go back and try again.</h3>" .
             "(reCAPTCHA said: " . $resp->error . ")");			
      }
/***************************************************************************/

if (isset($_SESSION['user_id']))
 
        //because you already sanitized the data using the build in function inside of dbc.php we can change to this
	/*
	 * $job_title = $post['job_title'];
         * $description = $post['description'];
         * $type = $post['type'];
         * $remuneration = $post['remuneration'];
         * $terms = $post['terms'];
         * $start_date = $post['start_date'];
	*/
	    $user_id = ($_POST['user_id']);
        $job_title = ($_POST['job_title']);
        $description = ($_POST['description']);
        $type = ($_POST['type']);
        $remuneration = ($_POST['remuneration']);
        $terms = ($_POST['terms']);
        $start_date = ($_POST['start_date']);

	 $sql_insert = "INSERT into `jobs` (`id`,`job_title`,`description`,`type`,`date`,`remuneration`,`terms`,`start_date`)
	  	  	VALUES ('".$_SESSION['user_id']."','$job_title','$description','$type','$remuneration','$terms','$start_date',now())";

mysql_query($sql_insert,$link) or die("Insertion Failed:" . mysql_error());
$user_id = mysql_insert_id($link); 
?>

<html>
<head>
<title>Market Affinity - Post a job</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script language="JavaScript" type="text/javascript" src="js/jquery.validate.js"></script>

  <script>
  $(document).ready(function(){
    $.validator.addMethod("username", function(value, element) {
        return this.optional(element) || /^[a-z0-9\_]+$/i.test(value);
    }, "Username must contain only letters, numbers, or underscore.");

    $("#regForm").validate();
  });
  </script>

<link href="styles.css" rel="stylesheet" type="text/css">
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">
  <tr> 
    <td colspan="3">&nbsp;</td>
  </tr>
  <tr> 
    <td width="160" valign="top"><p>&nbsp;</p>
      <p>&nbsp; </p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p></td>
    <td width="732" valign="top"><p>
	  <h2>Thank you</h2> Your job posting is now complete and you can <a href="mysettings.php">return to your menu here</a>";
	 <?php exit();
	  }
	?>
      <h3 class="titlehdr">Add a job to the directory</h3>
      <p>Please fill out the form below to add a job to the directory.<br />
	  The more details you fill out the more chance of your post being read.</p>
	 
	  <br>
	<!-- this file is called insertphpfile, you are posting to inserttest.phpÉ is this correct? -->
      <form action="inserttest.php" method="post" name="regForm" id="regForm" >
        <table width="95%" border="0" cellpadding="3" cellspacing="3" class="forms">
          <tr> 
            <td colspan="2">job_title<span class="required"><font color="#CC0000">*</font></span><br>
              <input name="job_title" type="text" id="job_title" size="40" class="required">
            </td>
            <td width="2"></td>
          </tr>
          <tr> 
            <td colspan="2">&nbsp;</td>
            <td></td>
          </tr>
          <tr> 
            <td colspan="2">Job Description (max 300 words)<span class="required"><font color="#CC0000">*</font></span><br>
		<!-- you have $post['description'] but your INPUT name is job_description -->
              <textarea name="description" cols="80" rows="8" id="description" class="required"></textarea>
            </td>
            <td></td>
          </tr>
          <tr> 
            <td width="168">Job Type<font color="#CC0000">*</font></td>
            <td width="625"> 
		<!-- you have $post['type'] but your SELECT name is job_type -->
              <select name="type" class="required" id="type">
                <option value="" selected></option>
                <option value="Permanent">Permanent</option>
                <option value="Part Time">Part Time</option>
                <option value="Contract">Contract</option>
              </select>
            </td>
            <td></td>
          </tr>
          <tr> 
            <td height="25" valign="top">Remuneration:</td>
            <td valign="top"> 
              <select name="remuneration" class="required" id="remuneration">
                <option value="" selected></option>
                <option value="0-5000">0-5000</option>
                <option value="5000-10000">5000-10000</option>
                <option value="10000-20000">10000-20000</option>
                <option value="20000-50000">20000-50000</option>
                <option value="50000-100000">50000-100000</option>
                <option value="100000 and above">100000 and above</option>
              </select>
            </td>
            <td></td>
          </tr>
          <tr> 
            <td height="25" valign="top">Salary/Wage terms:<span class="required"><font color="#CC0000"></font></span></td>
            <td valign="top"> <span style="color:red; font: bold 12px verdana; " id="terms" > 
              <select name="terms" class="required" id="terms">
                <option value="" selected></option>
                <option value="hourly">hourly</option>
                <option value="weekly">weekly</option>
                <option value="monthly">monthly</option>
                <option value="other">other</option>
              </select>
              </span> </td>
            <td></td>
          </tr>
          <tr> 
            <td height="34" valign="top">Start Date<span class="required"><font color="#CC0000">*</font></span> 
            </td>
            <td valign="top"> 
              <input name="start_date"  id="start_date">
            </td>
            <td></td>
          </tr>
          <tr> 
            <td height="21" colspan="2" valign="top">&nbsp;</td>
            <td></td>
          </tr>
          <tr> 
            <td height="24" valign="top"><strong>Image Verification </strong></td>
            <td valign="top"> 
              
			  <?php 
			require_once('recaptchalib.php');
			
				echo recaptcha_get_html($publickey);
			?>
           
		    </td>
            <td></td>
          </tr>
          <tr> 
            <td height="7"></td>
            <td></td>
            <td></td>
          </tr>
        </table>
        <p align="center">
          <input name="submit" type="submit" id="submit" value="Submit Job">
        </p>
      </form>
      <p align="right">&nbsp;</p>
	   
      </td>
    <td width="196" valign="top">&nbsp;</td>
  </tr>
  <tr> 
    <td colspan="3">&nbsp;</td>
  </tr>
</table>
</body>
</html>
And here is my jobs table:

Quote:
CREATE TABLE `jobs` (
`id` bigint(20) NOT NULL auto_increment,
`user_id` varchar(200) collate latin1_general_ci NOT NULL,
`job_title` varchar(200) collate latin1_general_ci NOT NULL default '',
`description` text collate latin1_general_ci NOT NULL,
`type` varchar(200) collate latin1_general_ci NOT NULL default '',
`remuneration` varchar(200) collate latin1_general_ci NOT NULL default '',
`terms` varchar(200) collate latin1_general_ci NOT NULL default '',
`start_date` text collate latin1_general_ci NOT NULL,
`date` date NOT NULL default '0000-00-00',
`ckey` varchar(220) collate latin1_general_ci NOT NULL default '',
`ctime` varchar(220) collate latin1_general_ci NOT NULL default '',
PRIMARY KEY (`id`),
FULLTEXT KEY `idx_search` (`job_title`,`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=55 ;
It is also giving me a duplicate entry error when I use the form again but the primary key (id) is set to auto increment?

Any help with this would really be highly appreciated.
Thank you
 
Old February 15th, 2011, 05:04 AM
Authorized User
 
Join Date: Aug 2009
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Never mind, I had 'id' instead of 'user_id' in my insert query.





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to insert values in tables using php?? asalaam Pro PHP 3 April 4th, 2007 01:02 PM
how to insert values in tables using php?? asalaam SQL Server 2000 1 March 27th, 2007 11:23 AM
PHP-MySQL Insert Data problem forexen PHP Databases 1 March 13th, 2007 12:58 AM
Database Insert/Update problem with VB.NET form tino mclaren VB Databases Basics 2 February 4th, 2005 05:12 AM
INSERT Problem with a web form acorbo ASP.NET 1.0 and 1.1 Professional 1 October 23rd, 2004 09:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.