clint Posted February 15, 2011 Share Posted February 15, 2011 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: <?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'])) $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"> </td> </tr> <tr> <td width="160" valign="top"><p> </p> <p> </p> <p> </p> <p> </p> <p> </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"> </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"> </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"> </p> </td> <td width="196" valign="top"> </td> </tr> <tr> <td colspan="3"> </td> </tr> </table> </body> </html> And here is my jobs table: 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 PS: please excuse my newb non programmer lingo! Link to comment https://forums.phpfreaks.com/topic/227715-php-form-issue/ Share on other sites More sharing options...
gristoi Posted February 15, 2011 Share Posted February 15, 2011 Your trying to insert the user_id into the id field $sql_insert = "INSERT into `jobs` (`id`,`job_title`,`description`,`type`,`date`,`remuneration`,`terms`,`start_date`) Needs to be $sql_insert = "INSERT into `jobs` (`user_id`,`job_title`,`description`,`type`,`date`,`remuneration`,`terms`,`start_date`) You do not need to insert the id field as it auto increments Link to comment https://forums.phpfreaks.com/topic/227715-php-form-issue/#findComment-1174439 Share on other sites More sharing options...
clint Posted February 15, 2011 Author Share Posted February 15, 2011 Wow! What a silly mistake! Thank you so much! That did the trick Link to comment https://forums.phpfreaks.com/topic/227715-php-form-issue/#findComment-1174444 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.