Hi i m creating a small web application that allows a user to select multiple options from a list box.The problem is i cannot write that information to the database.
I have two forms: one to get information from the user and another to write that info to the database.The code that i use to extract info from the user and write to the database is below:

$app = $_POST ['app']; 
$appname = clean_it($_POST ['appname']);
$appdesc = $_POST ['appdesc']; 
$year = $_POST ['year']; 
$Ecomments = $_POST ['Ecomments']; 
$comments = $_POST ['comments']; 
$db = $_POST ['db']; 
$AppOwner = $_POST ['AppOwner']; 

$devlanguage=$_POST['devlanguage'];
        if ($devlanguage)
        {
          foreach ($devlanguage as $lang)
            {
              $d[] = $lang ;
            }
        }   

// build query to insert data into the job table
$qry = "INSERT INTO Application VALUES (".
       "'$app','$appname','$appdesc','$year','$d',".
       "'$Ecomments','$comments','$AppOwner','$db')";




// execute query
$added = execute_query($qry);

Please tel me wat i have done wrong...Please help me some1
Thanks in advance

welcome to daniweb.


please use code tags around your code from now on.


to answer your question, i can only make assumptions on which field, how you want to insert it, etc.

I'm assuming you are referring to $d and it's probably being saved as "array"


so to fix this

$qry = "INSERT INTO Application VALUES (".
"'$app','$appname','$appdesc','$year','".print_r($d,true),"',".
"'$Ecomments','$comments','$AppOwner','$db')";

Once solved, please mark the thread as solved

Yes i m referring to $d which is suppose to extract the values in an aray and store it in one column of the database.Problem is that it does not store anything in that column.I tried ur solution but it still does not work.
Any other suggestions please....

This is the exact code from the first form that i m trying to insert into the database table

<select name="devlanguage[]" size="4" multiple="multiple">
	<option value="Cobolt">Cobolt</option>
	<option value="C#">C#</option>
	<option value=".NET1.1">.NET1 </option>
	<option value=".NET2.0">.NET2.0</option>
	<option value="VB">VB</option>
	<option value="Oracle forms">Oracle forms</option>
</select>

2nd form

<?php
$devlanguage=$_POST['devlanguage'];
if ($devlanguage)
 {
    foreach ($devlanguage as $lang)
     {
      $d[] = $lang ;
    }
 }	
	
// build query to insert data into the Application table
$qry = "INSERT INTO Application VALUES (".
       "'$app','$appname','$appdesc','$year','".print_r($d,true),"',".
      "'$Ecomments','$comments','$AppOwner','$db')";
		
// execute query
$added = execute_query($qry);
<?

based off of your code, what I posted is correct. but without more information, it's hard to guess what else could be wrong.


if you want some tips. repost your php with code tags. post your html file/form

and if it were me, i'd do something like the following to see my values and it would probably make things clear.

echo "<pre>";print_r($_POST);echo "</pre>";

you could also use implode. which works as well.


again, since you didnt leave many requirements, it depends on what you want to save.


I've tested both ways, they work perfectly.

$qry = "INSERT INTO Application VALUES (".
       "'$app','$appname','$appdesc','$year','".implode($d,',')."',".
      "'$Ecomments','$comments','$AppOwner','$db')";

PS your above code with my print_r had a , instead of a . at the end

I have attached the two forms.Please take a look at it and help me if possible.

This is the MySQL code that was used to create the table

CREATE TABLE Application(
App VARCHAR(30) NOT NULL,
AppName VARCHAR(60) NOT NULL,
AppDescription VARCHAR(180),
YearDeveloped YEAR(4),
Development_Language VARCHAR(30),
EnhancementComments VARCHAR(90),
Comments VARCHAR(120),
SectionID INT NOT NULL,
DBID INT NOT NULL,
CONSTRAINT Application_pk_App PRIMARY KEY(App),
CONSTRAINT Application_fk_SectionID FOREIGN KEY(SectionID) REFERENCES Section(SectionID),
CONSTRAINT Application_fk_DBID FOREIGN KEY(DBID) REFERENCES DB(DBID));

read my above post. The code you posted wont run. it should be a . after the print_r, or if you just want the values, use the implode version.

a bad database design. If you have a field that stores multi values, you should not store them in 1 field comma separated. You should make another table for it. A one-to-many relationship and link them via foreign key.

Thanks very much Kireol. The implode version worked.

I have similar problem like this .. would someone correct my code..thanks a lot : )

<?php include("C:/xampp/htdocs/mySalon/connect_database.php"); ?>

<?php
session_start();

if (isset ($_POST['submit'],$_POST['completeName'],$_POST ['mobileNumber'],$_POST ['emailAddress'],$_POST ['date'],$_POST ['time'],$_POST['servicesHairID'],$_POST['termsCondition'])) {

$submit = $_POST['submit'];

//form data

$completeName = mysql_real_escape_string ($_POST ['completeName']);
$mobileNumber = mysql_real_escape_string ($_POST ['mobileNumber']);
$emailAddress = mysql_real_escape_string ($_POST ['emailAddress']);
$date = mysql_real_escape_string($_POST ['date']);
$time = mysql_real_escape_string($_POST ['time']);
$servicesHairID = mysql_real_escape_string ($_POST ['servicesHairID']);

     if ($servicesHairID)
     {
     foreach ($servicesHairID as $servicesHair_reserve)
     {
     $servicesHairID[] = $servicesHair_reserve;
     }
}
$termsCondition = mysql_real_escape_string ($_POST ['termsCondition']);




if ($submit)
{
        if (!empty($_POST['completeName']) && !empty($_POST['mobileNumber']) && !empty($_POST['emailAddress']) && !empty($_POST['date']) && !empty($_POST['time']) && !empty($_POST['servicesHairID']) && !empty($_POST['termsCondition']) ) {
            $query="SELECT * FROM services_reserve WHERE completeName = '".$completeName."' 
            AND mobileNumber = '".$mobileNumber."' 
            AND emailAddress = '".$emailAddress."' 
            AND date_reserve = '".$date."'
            AND time_reserve = '".$time."'
            AND hairServices ='".$servicesHairID."'
            AND termsCondition ='".$termsCondition."'";




            $result = mysql_query($query);
            @$num_rows = mysql_num_rows($result);



            if ($num_rows == 0) {
                $query1="INSERT INTO services_reserve (completeName,mobileNumber,emailAddress,date_reserve,time_reserve,hairServices,termsCondition)
            VALUES ('$_POST[completeName]','$_POST[mobileNumber]','$_POST[emailAddress]','$_POST[date]','$_POST[time]','".implode($servicesHair_reserve,',')."','$_POST[termsCondition]')";

        $result1 = mysql_query($query1);
                if(@result1 == 0) {?> 
                <script language="javascript">
                alert("Reservation detail has been send.Please check your email address for the confirmation.");
                </script> 
                <?
                }
                else { ?> <script language="javascript">
                alert("No record added.");
                </script><? }
        }
        else
            echo "";
    }
    else 
    echo "Please fill in <b>all</b> fields!";
}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Reservation</title>
<link href="stylesheet/default.css" rel="stylesheet" type="text/css" />

<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>



<link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style7 {font-size: x-small}
.style9 {color: #FFFFFF}

.style17 {
    font-size: 12%;
    color: #006600;
}
.style28 {
    font-size: 13px;
    color: #666666;
}
.style29 {
    color: #006600;
    font-weight: bold;
}
.style30 {
    color: #FF0000;
    font-weight: bold;
}
-->
</style>
<script type="text/javascript">
<!--
function SelectDate()
{
    D = document.getElementById('Date').value;
    if(D){
        D = D.split('/');
    }else{
        Dat = new Date();
        D = new Array(Dat.getDay(), Dat.getMonth(), Dat.getFullYear());
    }
    win = window.open("date-picker.html","win","status=no,scrollbars=no,toolbar=no,menubar=no,height=150,width=150");
    if (parseInt(navigator.appVersion) == 2 && navigator.appName == "Netscape")
        win = window.open("date-picker.html","win","status=yes,height=325,width=250");
        //win'MakeDate',D[2], D[1],D[0], 'SetDate');
        win.MakeDate(D[2], D[1], D[0]);
}
function SetDate(Day, Month, Year)
{
    document.getElementById('Date').value = Day + '/' + Month + '/' + Year;
}
//-->
</script>
</head>

<body>
<div id="header">
  <div id="logo">
        <h1>&nbsp;</h1>
  </div>

    <ul id="MenuBar1" class="MenuBarHorizontal">
      <li><a href="index.php">Home</a> </li>
      <li><a href="#" class="MenuBarItemSubmenu MenuBarItemSubmenu">Services</a>
        <ul>
          <li><a href="hair_services.php">Hair Services</a></li>
          <li><a href="nail_services.php">Nail Services</a></li>
          <li><a href="massage_services.php">Massage Services</a></li>
          <li><a href="others_services.php">Others Services</a> </li>
        </ul>
      </li>
      <li><a href="#">Reservation</a> </li>
      <li><a href="#" class="MenuBarItemSubmenu">Gallery</a>
        <ul>
          <li><a href="hairStyle.html">Hair Style</a></li>
          <li><a href="nailArt.html">Nail Art</a></li>
        </ul>
      </li>
      <li><a href="contact.php">Contact Us</a></li>
  </ul>
    <script type="text/javascript">
AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','519','height','171','title','header','src','images/header','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','images/header' ); //end AC code
    </script>
    <noscript>
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="519" height="171" title="header">
      <param name="movie" value="images/header.swf" />
      <param name="quality" value="high" />
      <embed src="images/header.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="519" height="171"></embed>
    </object>
    </noscript>
</div>
</div>
<div id="page">
  <div id="content">
<div id="welcome" class="boxed2">
            <h1 class="title style17">Making An Online Reservation</h1>
            <p align="left">&nbsp;</p>
            <p align="left">Please complete the following form and                                           a confirmation email will be sent to you otherwise a representative will contact you within                                           24 hours to confirm your reservation details. </p>
            <p align="left">&nbsp;</p>
            <p align="left">To make reservations requests                                           with less than 24 hours advance notice,                                           to cancel a reservation or if you                                           have any questions regarding your reservation,                                           please <a href="http://www.hinamana.com/contact.php"><strong>contact                                           us</strong></a> directly at<strong> (02) 542-6478</strong></p>
            <p align="left">&nbsp;</p>
            <p align="left" class="style30">Note: Keith Louise Salon and Spa accomodate only 10 reservations a day.</p>
      <p class="style28">&nbsp;</p>
<div class="content">
            <p><h4 class="style29">Please Fill Out This Form To Request A Reservation</h4>
            <p class="style29">&nbsp;</p>
            </p>

            <form action="reservation.php" method="post">
                <table width="400" border="0" style="font-size:12px">

                  <tr>
                    <td><span class="style13">Complete Name:</span></td>
                    <td><input name="completeName" type="text" size="25"/></td>
                </tr>

                  <tr>
                    <td><span class="style13">Mobile Number:</span></td>
                    <td><input type="text" name="mobileNumber" size="25"/></td>
                  </tr>
                  <tr>
                  <td><span class="style13">Email Address:</span></td>
                <td><input type="text" name="emailAddress" size="25"/></td>
              </tr>
              </table>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                 <fieldset>
              <legend class="style29">Preffered Date and Time</legend>
              <p>&nbsp;</p>
                <p align="center">
                  <input type="text" name="date" id="Date"/>
                  <a href="javascript:SelectDate()">
                  <img src="images/calendar_icon.gif"/></a>
                  <select name="time" id="time">

                    <option value=""></option>
                    <option value="8:00 am">8:00 am</option>
                    <option value="8:30 am">8:30 am</option>
                    <option value="9:00 am">9:00 am</option>
                    <option value="9:30 am">9:30 am</option>
                    <option value="10:00 am">10:00 am</option>
                    <option value="10:30 am">10:30 am</option>
                    <option value="11:00 am">11:00 am</option>
                    <option value="11:30 am">11:30 am</option>
                    <option value="12:00 pm">12:00 pm</option>
                    <option value="12:00 pm">12:30 pm</option>
                    <option value="1:00 pm">1:00 pm</option>
                    <option value="1:30 pm">1:30 pm</option>
                    <option value="2:00 pm">2:00 pm</option>
                    <option value="2:30 am">2:30 am</option>
                    <option value="3:00 pm">3:00 pm</option>
                    <option value="3:30 pm">3:30 pm</option>
                    <option value="4:00 pm">4:00 pm</option>
                    <option value="4:30 pm">4:30 pm</option>
                    <option value="5:00 pm">5:00 pm</option>
                    <option value="5:30 pm">5:30 pm</option>
                    <option value="6:00 pm">6:00 pm</option>
                    <option value="6:30 pm">6:30 pm</option>
                    <option value="7:00 pm">7:00 pm</option>
                    <option value="7:30 pm">7:30 pm</option>
                    <option value="8:00 pm">8:00 pm</option>
                    <option value="8:30 pm">8:30 pm</option>
                    <option value="9:00 pm">9:00 pm</option>
                  </select>

                </p>
                <p>&nbsp;</p>
                </fieldset>
                <fieldset>
                <legend class="style29">Choose Type of Services</legend>
              <p>&nbsp;</p>

               <table width="400" border="0" style="font-size:12px">

                  <tr>
                    <td><span class="style13">Hair Services:</span></td>
                    <td><p>
                      <select name="servicesHairID[]" id="servicesHairID" multiple="multiple">
                        <option value="Amino Mint Treatment">Amino Mint Treatment</option>
                        <option value="Cellophane">Cellophane</option>
                        <option value="Fill and Seal Vital Repair">Fill and Seal Vital Repair</option>
                        <option value="Hair Blow Dry">Hair Blow Dry</option>
                        <option value="Hair Color Promo">Hair Color Promo</option>
                        <option value="Hair Hi-Lite">Hair Hi-Lite</option>
                        <option value="Hair Mask">Hair Mask</option>
                        <option value="Hair Perming">Hair Perming</option>
                        <option value="Hair Rebonding any Lenght">Hair Rebonding any lenght</option>
                        <option value="Hair Relax">Hair Relax</option>
                        <option value="Hair Spa">Hair Spa</option>
                        <option value="Haircut">Haircut</option>
                        <option value="Hair Wax">Hair Wax</option>
                        <option value="Hot Oil">Hot Oil </option>
                        <option value="Ionic Keratin Treatment">Ionic Keratin Treatment</option>
                        <option value="Iron Straight">Iron Straight</option>
                        <option value="Keratin Treatment">Keratin Treatment</option>
                        <option value="Loreal Gloss Treatment">Loreal Gloss Treatment</option>
                        <option value="Loreal Hair Color">Loreal Hair Color</option>
                        <option value="Loreal Hair Spa">Loreal Hair Spa</option>
                        <option value="Milk Relax w/ free hot oil and haircut">Milk Relax w/ free hot oil and Haircut</option>
                        <option value="Powerdose">Powerdose</option>
                        <option value="Semi De Lino">Semi De Lino</option>
                        <option value="Semi Permanent Hair Color">Semi Permanent Hair Color</option>

                                            </select>



                    </p>
                    <p>&nbsp; </p></td>
                  </tr>


              </table>
              </fieldset>


              <p>&nbsp;</p>
              <p>&nbsp;</p>
              <input name="termsCondition" type="checkbox" value="check" />
            By clicking the box, you Agree to the <a href="termsCondition.php" target="_blank">Terms and Conditions</a> of Keith Louise Salon and Spa.   

              <input type="submit" name="submit" value="Reserve"/>

            </form>

      </div>
     </div>
     </div>
  <div id="sidebar">
        <div id="news" class="boxed1">
            <h2 class="title">News &amp; Updates</h2>
            <div class="content">

              <p>&nbsp;</p>
              <p>&nbsp;</p>
            </div>
    </div>
    </div>
    <!-- end #sidebar -->
    <div style="clear: both; height: 1px;"></div>
</div>
<!-- end #page -->

<script type="text/javascript">
<!--
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
//-->
</script>

<div id="footer">
    <h6 align="center" class="style7"><span class="title style9">Copyright &copy; 2011 Keith Louise Salon and Spa! Designed by<a href="http://www.facebook.com/welovebarney" > D'otherz Company</a></span></h6>
</div>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.