?
Solved

to insert data on form into database using PHP MySQL

Posted on 2007-10-20
21
Medium Priority
?
265 Views
Last Modified: 2012-06-27
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);
?>














0
Comment
Question by:evry2004
  • 11
  • 8
  • 2
21 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 600 total points
ID: 20115415
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
 

Author Comment

by:evry2004
ID: 20115454
thanks didnt spot.

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

dont know why its not inserting
0
 
LVL 20

Expert Comment

by:Gawai
ID: 20115467
echo"<\select>";
select ends with / slash.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:evry2004
ID: 20115498
still no data in database.
0
 
LVL 20

Assisted Solution

by:Gawai
Gawai earned 900 total points
ID: 20115518
$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
 

Author Comment

by:evry2004
ID: 20115561
table is status.
put it but wasnt writing on database
0
 

Author Comment

by:evry2004
ID: 20115570
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
 
LVL 20

Assisted Solution

by:Gawai
Gawai earned 900 total points
ID: 20115574
change
$result= mysql_query($sql,$connect);
to
$result= mysql_query($sql);
0
 
LVL 20

Expert Comment

by:Gawai
ID: 20115585
BTW what is the error msg ?
0
 

Author Comment

by:evry2004
ID: 20115592
error meesage
"Qunatity missing

Back"

and not writing to database
0
 
LVL 20

Expert Comment

by:Gawai
ID: 20115633


make sure there is value for quantity
0
 

Author Comment

by:evry2004
ID: 20115653
i have doing that
0
 
LVL 20

Assisted Solution

by:Gawai
Gawai earned 900 total points
ID: 20115667
just echo you sql query and see what it returns

echo "$sql"
0
 

Author Comment

by:evry2004
ID: 20115702
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
 

Author Comment

by:evry2004
ID: 20115737
quantity missing
0
 
LVL 20

Assisted Solution

by:Gawai
Gawai earned 900 total points
ID: 20115738
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
 
LVL 20

Assisted Solution

by:Gawai
Gawai earned 900 total points
ID: 20115742
i mean ***insert data manually **
0
 

Author Comment

by:evry2004
ID: 20115747
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
 

Author Comment

by:evry2004
ID: 20115779
ok I got you now.
 I am doing that manually.
no writing to database.
0
 

Author Comment

by:evry2004
ID: 20115786
fine manually.
error comes from the script
0
 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 600 total points
ID: 20117235
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month16 days, 22 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question