Jump to content

Checkbox Array to MySQL


CoasterMedia

Recommended Posts

Hello,

 

I have been spending over 6 hours trying to square this away and I have looked at some tutorials from PHPFreaks... Maybe I'm missing something.

 

Basically I rebuilding the form from bottom up and I thought some check boxes would be easier for some part of the form instead of drop-down menu.

 

Basically this form is submitting query to MySQL:

 

article_add.php (Basically has the form)

article_insert.php (Basically insert the data to MySQL)

 

Here is one part of the form where the check box is located:

 

Here is article_add.php

<form action="article_insert.php" method="post">
<-- 
OTHER INPUT BOXES
-->
<div class="inputfieldtb"><label for="news_cat">News Categories:</label>
   							<?
						echo "<table>";
						echo "<tr>";

						$counter = 1;
                            while ($nc = mysql_fetch_array($news_cats)) {
							$ncat_id = $nc['ncat_id'];
							$ncat_name = $nc['ncat_label'];

						echo "<td><div><input type=\"checkbox\" name=\"category[]\" value=\"$ncat_id\" ''/>$ncat_name</div></td>\n";

							if (($counter % 4) == 0) {
							echo "</tr><tr>";
							}
						$counter ++;
						}
						echo "</tr></table>";
                            echo "<br />";
						?>
    </div>
<-- 
MORE FORMS
-->
</form>

 

Here is article_insert.php

 


// CONNECT TO DATABASE
include("../mods/connect.php");

<--- 
OTHER DATA INSERT IN MYSQL
-->

$news_cat = mysql_real_escape_string(implode(',', $_POST['category'])); // <-- Checkbox Data

<--- 
MORE DATA INSERT IN MYSQL
-->

$query = "INSERT INTO news_articles VALUES ('<--- MORE CONTENT BEING INSERT INTO MYSQL -->,'$news_cat','$news_content',<--- MORE CONTENT BEING INSERT INTO MYSQL -->'')";
mysql_query($query) or die ('Error inserting new data into the data to the database: ' . mysql_error() . '');

 

The error I'm getting is "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '','','3','0','','','')' at line 1";

 

 

So what am I doing wrong? 

 

Once I get this resolved, I would like to learn how to display the checkbox / updated the checkbox as well.

 

Thank you in advance for your time and your help with this problem.

Link to comment
Share on other sites

I think, there is some problem with single-quotes in the mysql string, try this modified query string

 

$query = "INSERT INTO news_articles VALUES ('<--- MORE CONTENT BEING INSERT INTO MYSQL -->','$news_cat','$news_content','<--- MORE CONTENT BEING INSERT INTO MYSQL -->')";

Link to comment
Share on other sites

@avvllvva Thanks for responding.  I took a look at it.  I notice there was one extra values so I deleted it but the problem still remain with same error.  Maybe I'm missing something. 

 

Here's the full script of article_insert.php:

<?php 
// CONNECT TO DATABASE
include("../mods/connect.php");

$nr_day=$_POST['nr_day'];
$nr_month=$_POST['nr_month'];
$nr_year=$_POST['nr_year'];

$news_title=$_POST['news_title'];
$news_subtitle=$_POST['news_subtitle'];
$news_aid=$_POST['news_aid'];
$news_date="$nr_year-$nr_month-$nr_day";
$news_cat = mysql_real_escape_string(implode(',', $_POST['category'])); // <-- Checkbox Data
$news_content=$_POST['news_content'];
$news_thumb=$_POST['news_thumb'];
$news_image=$_POST['news_image'];
$news_video=$_POST['news_video'];
$news_gallery=$_POST['news_gallery'];
$news_rc=$_POST['news_rc'];
$news_rides=$_POST['news_rides'];
$news_resorts=$_POST['news_resorts'];
$news_industry=$_POST['news_industry'];
$news_corp=$_POST['news_corp'];
$news_club=$_POST['news_club'];
$news_wid=$_POST['news_wid'];
$news_key=$_POST['news_key'];
$news_sid=$_POST['news_sid'];
$news_features=$_POST['news_features'];
$news_fpix=$_POST['news_fpix'];
$news_note=$_POST['news_note'];

$query = "INSERT INTO news_articles VALUES ('','$news_title','$news_subtitle','$news_aid','$news_date','','$news_tid','$news_cat','$news_content','$news_thumb','$news_image','$news_video','$news_gallery','$news_vid','$news_rc','$news_rides','$news_resorts','$news_industry','$news_corp','$news_club',$news_wid','$news_key','$news_sid','$news_features','$news_fpix','$news_note','')";
mysql_query($query) or die ('Error inserting new data into the data to the database: ' . mysql_error() . '');

echo '
<script type="text/javascript">
<!--
history.go(-2)?alert=success
//-->
</script>
';

?>

 

NOTE: There's 27 columns for this table.  There are some just fill in the blank like the standard "ID" and timestamp and another field that auto generate when it is live like counting the hits.

Link to comment
Share on other sites

@BlueSkyIS

 

Thanks for the suggestion and I tried it.  I like that format better since it is not so time consuming.  It seems to do the trick.  It did feed to the Database.

 

The new problem that I'm facing is that once I submit the form, it took me to the white page with nothing on it so I looked at the page source:  This is what I saw.

<br /> 
<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/tpc342/public_html/_adm/news/article_insert.php</b> on line <b>18</b><br /> 

<script type="text/javascript"> 
<!--
history.go(-2)?alert=success
//-->
</script> 

 

How can I avoid this from happening since once this form is operational, there will be at least 10 people using this form. 

 

My command line for checkbox data is this:

$news_cat = mysql_real_escape_string(implode(',', $_POST['category'])); // <-- Checkbox Data

 

 

Many thanks to both of you for your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.