julzk Posted November 16, 2009 Share Posted November 16, 2009 This is driving me crazy. I have been trying to work this out for weeks and cannot get my head around it. any help would be greatly appreciated!! I have two tables "tbl_jobs" and "tbl_jobs_done". I have an SQL query which displays and formats my daily job list from tbl_jobs SQL database, and has a checkbox at the end of each row. I can't get the stupid checkboxs to work :*( I want to be able to select 1, 2, 3 or more checkboxs and then click my submit button which will then insert the data into my second MySQL database named "tbl_jobs_done". i have spent hours and hours looking at tutorials and other peoples forum posts but can't seem to find something that hits home and works for me I only need the jobs_id field data from the tbl_jobs to be inserted into tbl_jobs_done table and into jobs_id field. I have other data on the form that will also be submitted. But in general, that's all I need :*( Here is my SQL Query -> if(!isset($cmd)) { echo "<form method='post' action=''><table width='98%' cellspacing='1' cellpadding='1' border='0'>\n"; echo "<tr>\n"; echo "<td class='sectiontitle' valign='top' align='left' width='100%'>Today's Jobs!</td>"; echo "<td class='txt' colspan='2' valign='top' align='right'><input name='submit' type='submit' value='Complete'></td>"; echo "</tr>\n"; $everyday=EVERYDAY; $dayname=date('l'); if($wtype=date('l')) { if ($wtype=="Saturday" or $wtype=="Sunday") { $wtype=WEEKEND; } elseif ($wtype=="Monday" or $wtype=="Tuesday" or $wtype=="Wednesday" or $wtype=="Thursday" or $wtype=="Friday") { $wtype=WEEKDAY; } } $result = mysql_query("SELECT * FROM tbl_jobs WHERE jobs_datetype IN('$everyday', '$dayname', '$wtype') ORDER BY jobs_id ASC"); $count=mysql_num_rows($result); $i = 0; while($r=mysql_fetch_array($result)) { $jobs_id=$r["jobs_id"]; $jobs_description=str_replace("\r\n","<br>",$r["jobs_description"]); $jobs_datestart=$r["jobs_datestart"]; $jobs_dateend=$r["jobs_dateend"]; $jobs_datetype=$r["jobs_datetype"]; $jobs_user=$r["jobs_user"]; $jobs_updateuser=$r["jobs_updateuser"]; if($i%2 == 0){ echo "<tr class='rowresult1'>\n"; echo "<td class='txt' align='left' width='100%'> $jobs_description</td>"; echo "<td class='txt' align='right'><input name='jobs_comment' style='width: 50px;'></td>"; echo "<td class='txt' valign='top' align='center'><input name='checkbox[]' type='checkbox' id='checkbox[]' value='$jobs_id'></td>"; echo "</tr>\n"; $i++; }else{ echo "<tr class='rowresult2'>\n"; echo "<td class='txt' align='left' width='100%'> $jobs_description</td>"; echo "<td class='txt' align='right'><input name='jobs_comment' style='width: 50px;'></td>"; echo "<td class='txt' valign='top' align='center'><input name='checkbox[]' type='checkbox' id='checkbox[]' value='$jobs_id'></td>"; echo "</tr>\n"; $i++; } } echo "</table></form>\n"; } And here is my Insert code: Well... i have no real code here at the moment as it's chopped up in 100 different things in attempts to get this to work :*( Link to comment https://forums.phpfreaks.com/topic/181684-solved-checkbox-form-inserting-row-data-from-one-sql-table-to-another-sql-table/ Share on other sites More sharing options...
joel24 Posted November 16, 2009 Share Posted November 16, 2009 for the insert have $jobs = $_POST['checkbox']; $jobIDs = array(); foreach ($job in $jobs) { $jobIDs[] = "('$job')"; } //implode with "," for sql $jobIDS = implode(", ", $jobIDs); $sql = "INSERT INTO tbl_jobs_done(jobs_id) VALUES $jobIDS;"; Link to comment https://forums.phpfreaks.com/topic/181684-solved-checkbox-form-inserting-row-data-from-one-sql-table-to-another-sql-table/#findComment-958244 Share on other sites More sharing options...
julzk Posted November 16, 2009 Author Share Posted November 16, 2009 I tried your method but it returns "Parse error: syntax error, unexpected T_STRING in /var/www/html/tsdportal/dailyjobs_index.php on line 121".. Line 121 is the foreach ($job in $jobs) line My Insert Code here: // ##### Complete Job and Insert into DB ##### Start -> if ($_POST[submit] == "Complete") { $jobs = $_POST['checkbox']; $jobIDs = array(); foreach ($job in $jobs) { $jobIDs[] = "('$job')"; } //implode with "," for sql $jobIDS = implode(", ", $jobIDs); $sql = "INSERT INTO tbl_jobs_done(jobs_id) VALUES $jobIDS;"; $result = mysql_query($sql); } // ##### Complete Job and Insert into DB ##### End <- Link to comment https://forums.phpfreaks.com/topic/181684-solved-checkbox-form-inserting-row-data-from-one-sql-table-to-another-sql-table/#findComment-958260 Share on other sites More sharing options...
joel24 Posted November 16, 2009 Share Posted November 16, 2009 haha sorry! i wrote that foreach loop as if it were in C#, // ##### Complete Job and Insert into DB ##### Start -> if ($_POST[submit] == "Complete") { $jobs = $_POST['checkbox']; $jobIDs = array(); foreach ($jobs as $job) { $jobIDs[] = "('$job')"; } //implode with "," for sql $jobIDS = implode(", ", $jobIDs); $sql = "INSERT INTO tbl_jobs_done(jobs_id) VALUES $jobIDS;"; $result = mysql_query($sql); } // ##### Complete Job and Insert into DB ##### End <- Link to comment https://forums.phpfreaks.com/topic/181684-solved-checkbox-form-inserting-row-data-from-one-sql-table-to-another-sql-table/#findComment-958319 Share on other sites More sharing options...
julzk Posted November 16, 2009 Author Share Posted November 16, 2009 Awesome, worked a charm! Link to comment https://forums.phpfreaks.com/topic/181684-solved-checkbox-form-inserting-row-data-from-one-sql-table-to-another-sql-table/#findComment-958337 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.