1

I'm not in a very good level in php coding. i have a php interface(code: insert.php) which has four forms that are used to enter data to four different tables in my database and data entry to the forms are independent from each other. but, when i enter data to a form, it results in "undefined index error" pointing two variables which are related to another form in the interface. and also, data is not entered to the table in the database. not all the forms cause this error.they work fine.

this is the code of 'insert.php' the form i need data to be inserted.

<form method="post" action="input.php">
<tr>
    <td>ID</td>
    <td><input type="text" name="cat_id" size="40">
    </td>
</tr>
<tr>
    <td>Description</td>
    <td>
        <textarea NAME="desc" COLS=31 ROWS=6></textarea>
    </td>
</tr>

<tr>
    <td>
    </td>
    <td align="right">
        <input type="submit" name="submit" value="Done">
    </td>
</tr>

this is the code in 'insert.php', the error variables related to.

<form method="post" action="input.php">
<tr>
    <td>ItemID</td>
    <td><input type="text" name="item_id" size="40">
    </td>
</tr>
<tr>
    <td>EPF</td>
    <td><input type="text" name="epf" size="40">
    </td>
</tr>
<tr>
    <td>Quantity</td>
    <td><input type="text" name="quan" size="40">
    </td>
</tr>
<tr>
    <td>Date</td>
    <td><input type="date" name="date" size="40">
    </td>
</tr>
<tr>
    <td>
    </td>
    <td align="right">
        <input type="submit" name="submit" value="Done">
    </td>
</tr>

this is the code in 'input.php'.

<?php  
    $cat_id=$_POST['cat_id'];
    $cat_descr=$_POST['desc'];

    $query_cat = "INSERT INTO 'category'  ( id, description)
                    VALUES   ('$cat_id','$cat_descr')" or die (mysql_error());

     $result_cat = mysql_query($query_cat);      

  ?>      

<?php 

    $item_id=$_POST['item_id'];
    $epf2=$_POST['epf'];
    $quan=$_POST['quan'];
    $date=$_POST['date'];

    $query_itemEmp = "INSERT INTO 'emp_div_item'  ( epf, item ,quantity, date)
                    VALUES   ('$epf2','$item_id','$quan','$date')"  or die (mysql_error());

     $result_itemEmp = mysql_query($query_itemEmp);      

  ?> 

 <?php 
  if( $result_emp || $result_cat || $result_item || $result_itemEmp){
                 echo("<br>Input data is succeed");
            } else{
                     echo("<br>Input data is fail");
                     }
  ?>

the variables which the error points out are $quan and $date.. $result_item and $result_emp are query results get from other forms which work fine.please note that users dont need to enter data to all forms at a time. they can chose whatever the number of forms to be filled at a time.

plese help me to solve this problem and thank you in advance.

2 Answers 2

0

I think the problem is with the HTML code, you are having two seperate forms :

<form method="post" action="input.php">  ---first one

<form method="post" action="input.php">   --- second one

So when you click on first form's submit, only that form's data will be submitted (here , only cat_id and desc will be available in input.php)

And then you try access other forms values in the same input.php ($item_id=$_POST['item_id']; which is not present with the first form's data.

Hence you get this error.

Also if you will try to submit the second form, the you will get the same error for $cat_id and $cat_descr variables.

So keep all the data in a single form.

3
  • thank you so much for the quick response.. i made changes as you instructed and now that error is gone..!! but, the result is "input data is fail" so, where do i have the problem now?? is that with four submit buttons?? please give a help in this too.
    – MNA Sha
    Jun 27, 2013 at 5:13
  • "input data fail" error occured due to misplacement of </form> tag. it's my mistake. when posting the question i didnt expect such quick responses from you all. i could make my work easy.. :) thanks again..
    – MNA Sha
    Jun 27, 2013 at 5:38
  • need more help.. my new problem is with "Data entry to the fields that are primary key fields".. even the data entry succeeded, my database tables don't show the data of primary key fields. but, other columns are filled.
    – MNA Sha
    Jun 27, 2013 at 5:43
0

As far as your queries concerned: don't put quotes around db identifiers. Use ticks if you need to.

That being said change

$query_cat = "INSERT INTO 'category'  (id, description) VALUES ('$cat_id','$cat_descr')" or die (mysql_error());
                          ^        ^

to

$query_cat = "INSERT INTO category  (id, description) VALUES ('$cat_id','$cat_descr')" or die (mysql_error());

and

$query_itemEmp = "INSERT INTO 'emp_div_item'  (epf, item ,quantity, date) VALUES ('$epf2','$item_id','$quan','$date')"  or die (mysql_error());
                              ^            ^

to

$query_itemEmp = "INSERT INTO emp_div_item  (epf, item ,quantity, date) VALUES ('$epf2','$item_id','$quan','$date')"  or die (mysql_error());

On a side note: your code in current state vulnerable to sql-injections. Learn and use prepared statements with either mysqli or PDO. mysql_* extension is deprecated and is no longer supported.

11
  • Thank you for the quick response and advise.. it doesn't work even without quotes.
    – MNA Sha
    Jun 27, 2013 at 5:01
  • Where is your code for connecting to db server? I can't see it in index.php that you posted.
    – peterm
    Jun 27, 2013 at 5:03
  • index.php??? i'm sorry, i didnt get your question. anyway, db server connection is ok in my case.
    – MNA Sha
    Jun 27, 2013 at 5:17
  • thank you for taking your own time to reply. my error was with forms. when posting the question i didnt expect such quick responses from you all. i could make my work easy.. :)
    – MNA Sha
    Jun 27, 2013 at 5:37
  • need more help.. my new problem is with "Data entry to the fields that are primary key fields".. even the data entry succeeded, my database tables don't show the data of primary key fields. but, other columns are filled.
    – MNA Sha
    Jun 27, 2013 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.