0

In my application have a option for added new product in my product list by its name and date wise.When i try to add same product in another date it doesn`t insert.

my table looks like that:

Date | Materials_code | Materials_name | Parts_code | Unit | Input_qty

2019-05-22, 1200 , H1 , A12 , Pcs , 50
2019-05-21, 1250, mat , B25 , Pcs , 50

now I want to insert again H1 (Materials_name) where date and Input_qty is change

Date | Materials_code | Materials_name | Parts_code | Unit | Input_qty

2019-05-25, 1200 , H1 , A12 , Pcs , 100

and my expected output is

Date | Materials_code | Materials_name | Parts_code | Unit | Input_qty

2019-05-25, 1200 , H1 , A12 , Pcs, 100

2019-05-22, 1200 , H1 , A12 Pcs , 50
2019-05-21, 1250, mat , B25 , Pcs , 50

HTML:

<form action="add.php" method="post" name="form1">
                                            <table width="25%" border="0">
                                                <tr> 
                                                    <td>Date</td>
                                                    <td><input type="date" name="date[]" ></td>
                                                </tr>
                                                <tr> 
                                                    <td>Materials Code</td>
                                                    <td><input type="text" name="m_code[]"></td>
                                                </tr>
                                                <tr> 
                                                    <td>Materials Name</td>
                                                    <td><input type="text" name="m_name[]"></td>
                                                </tr>
                                                <tr> 
                                                    <td>Parts Code</td>
                                                    <td><input type="text" name="parts_code[]"></td>
                                                </tr>
                                                <tr> 
                                                    <td>Unit</td>
                                                    <td><input type="text" name="unit[]"></td>
                                                </tr>
                                                <tr> 
                                                    <td>Qty</td>
                                                    <td><input type="number" name="qty[]"></td>
                                                </tr>

                                                <tr> 
                                                    <td></td>
                                                    <td><input type="submit" name="Submit" value="Add"></td>
                                                </tr>
                                            </table>    
                                        </form>

I try this code..

if(isset($_POST['Submit'])) {  
    $myDate = $_POST['date'];
    $m_code = $_POST['m_code'];
    $m_name = $_POST['m_name'];
    $parts_code = $_POST['parts_code'];
    $unit = $_POST['unit'];
    $qty = $_POST['qty'];

      for ($i = 0; $i <= count($m_name); $i++) { 
           $m_code = (!empty($m_code[$i])) ? $m_code[$i] : '';
           $m_name = (!empty($m_name[$i])) ? $m_name[$i] : '';
    $parts_code = (!empty($parts_code[$i])) ? $parts_code[$i] : '';
           $unit = (!empty($unit[$i])) ? $unit[$i] : '';
           $qty = (!empty($qty[$i])) ? $qty[$i] : '';
         $result = mysqli_query($con, "INSERT INTO input(Date,Materials_code, Materials_name, Parts_code, Unit, Input_qty)VALUES('$myDate','$m_code', '$m_name', '$parts_code', '$unit', '$qty')");

using this code show that output:

Date | Materials_code | Materials_name | Parts_code | Unit | Input_qty

2019-05-25, 2 , 1 , 1 , c, 1

2019-05-22, 1200 , H1 , A12 Pcs , 50
2019-05-21, 1250, mat , B25 , Pcs , 50

15
  • Without seeing the HTML I would guess that you are using select elements on your page. You have a value set for each item. This value is being sent through not the actual data that you want. Commented May 25, 2019 at 4:53
  • Why you expecting 25th date. you are inserting 22nd right. or the question is wrong? Commented May 25, 2019 at 4:54
  • 1
    Use parameterized queries. Use error reporting.. Add the table defintions to the question. Commented May 25, 2019 at 4:55
  • @jeff now i added my html ,please checck and help me out this problem Commented May 25, 2019 at 5:02
  • 1
    Hey, your code is wide open to SQL injection attacks. Use prepared/parameterized queries to avoid this problem. You will be hacked if you haven't been already, not to mention all of the random application errors you'll end up with if your users use quote marks and what not in their text values. Commented May 25, 2019 at 5:53

1 Answer 1

3

Just write this query in your db phpmyadmin

ALTER TABLE input
DROP INDEX Materials_name;

To drop primary key use this one -

ALTER TABLE input
DROP INDEX `PRIMARY`;
Sign up to request clarification or add additional context in comments.

5 Comments

what will happen when i use this?
@FazlulHoqueSawrav it will remove primary key in the table.
its work but when i inseted one data its insert 2 data at a time.like: > 2019-05-25, 1200, H1, A12, Pcs ,80 >> 0000-00-00, 2, 1, 1, c, 0
please give me a final solution
@FazlulHoqueSawrav make another column primary & unique and then check if it works.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.