to insert data on form into database using PHP MySQL

I am having the following error after submitting my data on the form:

- error meesage
Parse error: syntax error, unexpected $end in C:\program file\wamp\www\delicious\insert_it.php on line 102.

- my form codes:
<!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=iso-8859-1" />
<title>Delicious Ltd | Stock Registration</title>
<link href="delicious/titre.css" rel="stylesheet" type="text/css" />
<link href="delicious/label.css" rel="stylesheet" type="text/css" />
</head>

<body>

<?php

      $useriddb = "root";
            $passworddb = "";
     
      // Connecting to and selecting a database
      $connect = mysql_connect('localhost', $useriddb, $passworddb)
         or die('Could not connect: ' . mysql_error());
      //echo '<b>Step 1:</b> Connected successfully! <BR>';
     
      $db = 'db_delicious'; // My database is called test
      mysql_select_db($db) or die('Could not select database ('.$db.') because of : '.mysql_error());
      //echo '<b>Step 2:</b> Connected to ('.$db.') successful!<BR>';
     

// create a look up field  for product name

$sql1="      SELECT item_id, name from product";
$sql2="      SELECT supplier_name from supplier";

echo"<form method=post action=\"insert_it.php\" >";
?>


<center>
<table width="500" border="1" cellpadding="0" cellspacing="0" >
  <tr>
    <td align="center"><table width="450" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="150" rowspan="3"><img src="images/login.jpg" alt="image" width="149" height="399" /></td>
        <td width="300"><img src="images/delicious.jpg" alt="image" width="299
            " height="113" /></td>
      </tr>
      <tr>
        <td width="300" height="49"><h3 align="center" class="titre"> Stock Registration </h3></td>
      </tr>
      <tr>
        <td width="300" align="left" valign="top">
            <form name="product" method="post" action="delicious/productionn.php">
            <table width="300" border="">
  <tr>
    <td>Select Product:</td>
    <td>
      <?php
      echo" <select name=\"Item_Name\" >";
$result1=mysql_query($sql1, $connect);

while($row=mysql_fetch_array($result1)){

$item_id=$row["item_id"];
$name=$row["name"];

echo"<option value=\"$item_id\">$name</option>";
}

echo"<\select>";
      ?>
      </td>
  </tr>
  <tr>
    <td>Expire Date:</td>
    <td>
      <?php echo"<select name=\"year1\" size=\"1\">";
     
            
            echo" <option>2008</option>";
            echo" <option>2009</option>";
            echo" <option>2010</option>";
            echo" <option>2011</option>";
            echo" <option>2012</option>";
            echo" <option>2013</option>";
            echo" <option>2014</option>";
        echo"</select>";

       echo"<select name=\"mm1\" size=\"1\">";
         
        echo"<option>01</option>";
        echo"<option>02</option>";
             echo"<option>03</option>";
        echo"<option>04</option>";
        echo"<option>05</option>";
        echo"<option>06</option>";
        echo"<option>07</option>";
        echo"<option>08</option>";
        echo"<option>09</option>";
        echo"<option>10</option>";
        echo"<option>11</option>";
        echo"<option>12</option>";
      echo"</select>";
       
      
        echo"<select name=\"dd1\" size=\"1\">";
         
       
        echo"<option>01</option>";
        echo"<option>02</option>";
        echo"<option>03</option>";
        echo"<option>04</option>";
        echo"<option>05</option>";
        echo"<option>06</option>";
        echo"<option>07</option>";
        echo"<option>08</option>";
        echo"<option>09</option>";
        echo"<option>10</option>";
        echo"<option>11</option>";
        echo"<option>12</option>";
        echo"<option>13</option>";
        echo"<option>14</option>";
        echo"<option>15</option>";
        echo"<option>16</option>";
        echo"<option>17</option>";
        echo"<option>18</option>";
        echo"<option>19</option>";
        echo"<option>20</option>";
        echo"<option>21</option>";
        echo"<option>22</option>";
        echo"<option>23</option>";
        echo"<option>24</option>";
        echo"<option>25</option>";
        echo"<option>26</option>";
        echo"<option>27</option>";
        echo"<option>28</option>";
        echo"<option>29</option>";
       echo"<option>30</option>";
        echo"<option>31</option>";
        echo" </select>";
      
       ?>
       </td>
  </tr>
  <tr>
    <td>Delivery Date:</td>
    <td><?php
      echo"<select name=\"year2\" size=\"1\">";
     
            echo" <option>2007</option>";
            echo" <option>2008</option>";
            echo" <option>2009</option>";
            echo" <option>2010</option>";
            echo" <option>2011</option>";
            echo" <option>2012</option>";
            echo" <option>2013</option>";
            echo" <option>2014</option>";
        echo"</select>";

       echo"<select name=\"mm2\" size=\"1\">";
         
        echo"<option>01</option>";
        echo"<option>02</option>";
             echo"<option>03</option>";
        echo"<option>04</option>";
        echo"<option>05</option>";
        echo"<option>06</option>";
        echo"<option>07</option>";
        echo"<option>08</option>";
        echo"<option>09</option>";
        echo"<option>10</option>";
        echo"<option>11</option>";
        echo"<option>12</option>";
      echo"</select>";
       
      
        echo"<select name=\"dd2\" size=\"1\">";
         
       
        echo"<option>01</option>";
        echo"<option>02</option>";
        echo"<option>03</option>";
        echo"<option>04</option>";
        echo"<option>05</option>";
        echo"<option>06</option>";
        echo"<option>07</option>";
        echo"<option>08</option>";
        echo"<option>09</option>";
        echo"<option>10</option>";
        echo"<option>11</option>";
        echo"<option>12</option>";
        echo"<option>13</option>";
        echo"<option>14</option>";
        echo"<option>15</option>";
        echo"<option>16</option>";
        echo"<option>17</option>";
        echo"<option>18</option>";
        echo"<option>19</option>";
        echo"<option>20</option>";
        echo"<option>21</option>";
        echo"<option>22</option>";
        echo"<option>23</option>";
        echo"<option>24</option>";
        echo"<option>25</option>";
        echo"<option>26</option>";
        echo"<option>27</option>";
        echo"<option>28</option>";
        echo"<option>29</option>";
             echo"<option>30</option>";
        echo"<option>31</option>";
        echo" </select>";
       ?></td>
  </tr>
  <tr>
    <td>Select Supplier:</td>
    <td>
      <?php
      // create supplier look-up field

echo" <select name=\"supplier_name\" >";
$result2=mysql_query($sql2, $connect);

while($row=mysql_fetch_array($result2)){

$supplier_name=$row["supplier_name"];

echo"<option >$supplier_name</option>";
}

echo"<\select>";
      
       ?>
       </td>
  </tr>
  <tr>
    <td>Quantity:</td>
    <td><?php echo" <input name=\"quantity\" type=\"text\" /> "; ?></td>
  </tr>
  <tr>
    <td>Select State:</td>
    <td>
      <?php
      // create state look-up field
echo" <select name=\"state\" >";


echo"<option >Stock-In</option>";
echo"<option >Stock-Out</option>";
echo"<\select>";
      ?>
      </td>
  </tr>
 
  <tr>

    <td><input name="submit" type="submit" value="SUBMIT" /></td>
      <td><input name="reset" type="reset" value="RESET" /></td>
  </tr>
 
 
</table>

        </form>
        </td>
      </tr>
    </table></td>
  </tr>
</table>
</center>
</body>
</html>



- my script to handle it
<?php

//connect to the database server
      $useriddb = "root";
            $passworddb = "";
     
      // Connecting to and selecting a database
      $connect = mysql_connect('localhost', $useriddb, $passworddb)
         or die('Could not connect: ' . mysql_error());
      //echo '<b>Step 1:</b> Connected successfully! <BR>';
     
      $db = 'db_delicious'; // My database is called test
      mysql_select_db($db) or die('Could not select database ('.$db.') because of : '.mysql_error());
      //echo '<b>Step 2:</b> Connected to ('.$db.') successful!<BR>';
     
//get date from form
$exp_date = $_REQUEST['year1'] . "-" . $_REQUEST['mm1'] . "-" . $_REQUEST['dd1'];
$del_date = $_REQUEST['year2'] . "-" . $_REQUEST['mm2'] . "-" . $_REQUEST['dd2'];

// protect form elements
function protect_it($str){
$str=stripslashes($str);
return($str);
}

//validate for emptiness
$error=false;
if($quantity ==""){
$error=true;
echo"Qunatity missing <br>";
}

if($error){
$quantity = protect_it($quantity);
echo"<br> <a href=\"stockk.php\"> Back</a>";
exit;
}

$sql="INSERT INTO(Item_Name, exp_date,del_date, supplier_name, quantity, state)
      VALUES('$Item_Name','$exp_date','$del_date', '$supplier_name',       '$quantity','$state') ";
$result= mysql_query($sql,$connect);

if(!$result){
echo"Error, cannot add record to the database<br>";
exit;
}else{

//confirmation message displayed in a table
echo"Product added [".mysql-affected_rows()."] record added<br>";


echo"<table >";

echo"<tr>";
echo" <td>Product name:</td>";
echo" <td>$Item_Name</td>";
echo"</tr>";

echo"<tr>";
echo" <td>Expire Date:</td>";
echo" <td>$exp_date</td>";
echo"</tr>";

echo"<tr>";
echo" <td>Delivery Date:</td>";
echo" <td>$del_date</td>";
echo"</tr>";

echo"<tr>";
echo" <td>Supplier Name:</td>";
echo" <td>$supplier_name</td>";
echo"</tr>";

echo"<tr>";
echo" <td>Quantity:</td>";
echo" <td>$quantity</td>";
echo"</tr>";

echo"<tr>";
echo" <td>State:</td>";
echo" <td>$state</td>";
echo"</tr>";

echo"</table >";


// Now we close the connection...
      mysql_close($connect);
?>














evry2004Asked:
Who is Participating?
 
RoonaanCommented:
You are just missing a bracket after your else statement.

you'd need to change echo "</table>"; to

echo "</table>";
} // close else clause

-r-
0
 
evry2004Author Commented:
thanks didnt spot.

however, no data entered in my database.
I have the following.
"Qunatity missing
Back"

dont know why its not inserting
0
 
GawaiCommented:
echo"<\select>";
select ends with / slash.

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
evry2004Author Commented:
still no data in database.
0
 
GawaiCommented:
$sql="INSERT INTO(Item_Name, exp_date,del_date, supplier_name, quantity, state)
      VALUES('$Item_Name','$exp_date','$del_date', '$supplier_name',       '$quantity','$state') ";
$result= mysql_query($sql,$connect);


here i cant see Table name !!!
INSERT INTO <TABLENAME>
0
 
evry2004Author Commented:
table is status.
put it but wasnt writing on database
0
 
evry2004Author Commented:
here is my database structure to rebuild it and have a proper testing




// create database
CREATE DATABASE `db_delicious` ;


// create login table
CREATE DATABASE `db_delicious` ;

CREATE TABLE `user` (
`userid` INT( 4 ) NOT NULL ,
`password` VARCHAR( 20 ) NOT NULL ,
PRIMARY KEY ( `userid` )
) ENGINE = innodb;

// create product table
CREATE TABLE `product` (
`item_id` INT( 4 ) NOT NULL ,
`name` VARCHAR( 20 ) NOT NULL ,
PRIMARY KEY ( `item_id` )
) ENGINE = innodb;

// create status table

CREATE TABLE `status` (
`status_id` INT( 4 ) NOT NULL ,
`exp_date` DATETIME NOT NULL ,
`del_date` DATE NOT NULL ,
`supplier_name` VARCHAR( 20 ) NOT NULL ,
`quantity` INT( 3 ) NOT NULL ,
`state` VARCHAR( 9 ) NOT NULL
) ENGINE = innodb;
ALTER TABLE `Status` CHANGE `supplier_name` `supplier_name` VARCHAR( 30 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL
ALTER TABLE `status` CHANGE `status_id` `Item_Name` VARCHAR( 30 ) NOT NULL

//populate the the login table
INSERT INTO `user` ( `userid` , `password` )
VALUES (
'2050', 'emelyn'
);
INSERT INTO `user` ( `userid` , `password` )
VALUES (
'3050', 'derrick'
), (
'4050', 'evrard'
);
INSERT INTO `user` ( `userid` , `password` )
VALUES (
'5050', 'eric'
);

//Create supplier table

CREATE TABLE `supplier` (
`supplier_name` VARCHAR( 30 ) NOT NULL
) ENGINE = innodb;

0
 
GawaiCommented:
change
$result= mysql_query($sql,$connect);
to
$result= mysql_query($sql);
0
 
GawaiCommented:
BTW what is the error msg ?
0
 
evry2004Author Commented:
error meesage
"Qunatity missing

Back"

and not writing to database
0
 
GawaiCommented:


make sure there is value for quantity
0
 
evry2004Author Commented:
i have doing that
0
 
GawaiCommented:
just echo you sql query and see what it returns

echo "$sql"
0
 
evry2004Author Commented:
before i do,
to tell that I have some entries as follows:
2008-01-01 00:00:00 2007:01:01              0 at  the very bottom with the same error message
0
 
evry2004Author Commented:
quantity missing
0
 
GawaiCommented:
just insert date manually

$sql="INSERT INTO status (Item_Name, exp_date,del_date, supplier_name, quantity, state)
      VALUES('Nokia N95','2007-10-21','2007-10-21', 'Nokia', 24,'NA') ";
$result= mysql_query($sql);

//make sure table name. table name, field names are caSe SensitivE

quantity is of INT type no need to wrap with '
0
 
GawaiCommented:
i mean ***insert data manually **
0
 
evry2004Author Commented:
for table name I am fine.
it is as if no data from drop down boxes such Item_Name and supplier_name are not entered in database
0
 
evry2004Author Commented:
ok I got you now.
 I am doing that manually.
no writing to database.
0
 
evry2004Author Commented:
fine manually.
error comes from the script
0
 
RoonaanCommented:
evry2004,

Hi,

You forgot to add:
$quantity = $_REQUEST['quantity'];

Also, to prevent sql injection, you could use:
$sql= sprintf('INSERT INTO status(Item_Name, exp_date,del_date, supplier_name, quantity, state) VALUES("%s","%s","%s","%s",%d,"%s")'
             , mysql_real_escape_string($Item_Name)
             , mysql_real_escape_string($exp_date)
             , mysql_real_escape_string($del_date)
             , mysql_real_escape_string($supplier_name)
             , mysql_real_escape_string($quantity)
             , mysql_real_escape_string($state)
             );

Kind regards

-r-
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.