1

I'm a newbie in PHP I just studied a while ago. I made some form that on submit, it will insert new data in mysql table. I can't seem to figure out the problem. Here's my code:

<form method="post">
        <table border='2' cellpadding=4 cellspacing=1 width=50% bgcolor=white align=center >
        <br>
            <tr>
                <td><font face=rockwell font size=5 font color=black>Master No.:</td>
                <td class="td-center"><input type="text" name="master_no" /></td>
            </tr>
            <tr>
                <td><font face=rockwell font size=5 font color=black>Alacarte No.:</td>
                <td class="td-center"><input type="text" name="alacarte_no" /></td>
            </tr>
            <tr>
                <td><font face=rockwell font size=5 font color=black>Dessert No.:</td>
                <td class="td-center"><input type="text" name="dessert_no" /></td>
            </tr>
            <tr>
                <td><font face=rockwell font size=5 font color=black>Pasta No.:</td>
                <td class="td-center"><input type="text" name="pasta_no" /></td>
            </tr>
            <tr>
                <td><font face=rockwell font size=5 font color=black>Rice No.:</td>
                <td class="td-center"><input type="text" name="rice_no" /></td>
            </tr>
            <tr>
                <td><font face=rockwell font size=5 font color=black>Wine No.:</td>
                <td class="td-center"><input type="text" name="wine_no" /></td>
            </tr>
            <tr>
                <td><font face=rockwell font size=5 font color=black>Member ID:</td>
                <td class="td-center"><input type="text" name="member_id" /></td>
            </tr>

            <tr>
                <td>&nbsp;</td>
                <td class="td-center">
                    <input type="submit" name="submit" value="Add"/>
                    <input type="Reset"/>
                    <a href="member-profile.php">
                        <input type="button" name="home" value="home"/>
                    </a><br>
                </td>
            </tr>
        </table>
        <?php   

            if (isset($_POST['submit'])) {
                $conn = mysql_connect('localhost', 'root', '');
                 if (!$conn)
                {
                 die('Could not connect: ' . mysql_error());
                }
                mysql_select_db("thesis_sofitel", $conn);

                $master_no=$_POST['master_no'] ;
                $alacarte_no= $_POST['alacarte_no'] ;   
                $dessert_no= $_POST['dessert_no'] ;
                $pasta_no= $_POST['pasta_no'] ; 
                $rice_no= $_POST['rice_no'] ;
                $wine_no= $_POST['wine_no'] ;   
                $member_id= $_POST['member_id'] ;

                mysql_query("INSERT INTO `master_list`(master_no,alacarte_no,dessert_no,pasta_no,rice_no,wine_no,member_id) 
                            VALUES ('$master_no','$alacarte_no','$dessert_no','$pasta_no','$rice_no','$wine_no','$member_id')"); 

                $message = "I was here!!!";
                echo "<script type='text/javascript'>alert('$message');</script>";

            }           
        ?>
    </form>
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
user3545006
  • 127
  • 2
  • 9
  • Since you just started, it would be so much better to start off either `mysqli` or `PDO` and stop using the _deprecated_ mysql functions. enable error reporting to know where you're messing up. – DirtyBit Sep 27 '15 at 09:37
  • 1
    what @HawasKaPujaari says and please update your question with any errors that you see – e4c5 Sep 27 '15 at 09:38
  • Close your `` tag right after `` and move the php above the HTML. – DirtyBit Sep 27 '15 at 09:39
  • @HawasKaPujaari I did that, and it still doesn't save. – user3545006 Sep 27 '15 at 09:48
  • @HawasKaPujaari bhai it is stackoverflow.com not antarvasna.com you should consider changing username :-) – Dhiraj Wakchaure Sep 27 '15 at 10:36
  • @ddw147 jaani, as long as what I do is _within_ the rules defined in the SO and is related to the core subject of the site, which is _to learn and to help_ I don't think anyone should have any objection over my name and besides acha he to hai `;-)` – DirtyBit Sep 27 '15 at 10:39
  • @HawasKaPujaari i dont have any objection. but it was unexpected here and i laughed when i saw it.n any way have a fun :-) – Dhiraj Wakchaure Sep 27 '15 at 10:49
  • @ddw147 That is the whole point of it brother, _to let people have a good laugh_ `;-)` cheers! – DirtyBit Sep 27 '15 at 10:51

1 Answers1

1

Fixed a few bugs in your code and changed it to mysqli. This should work now:

1) provide a action attribute in your form, in your case action = "".

2) echo the values to know if they're really being fetched. This is still prone to SQL Injection though. Read more here to know about why/how prepared statements help.

<?php   
    if (isset($_POST['add-submit'])) {
     $con=mysqli_connect("localhost","root","your_pass_here","thesis_sofitel");

    if (mysqli_connect_errno())
     {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }

        echo        $master_no=$_POST['master_no'] ;
        echo        $alacarte_no= $_POST['alacarte_no'] ;   
        echo        $dessert_no= $_POST['dessert_no'] ;
        echo        $pasta_no= $_POST['pasta_no'] ; 
        echo        $rice_no= $_POST['rice_no'] ;
        echo        $wine_no= $_POST['wine_no'] ;   
        echo        $member_id= $_POST['member_id'] ;

$success = mysqli_query($con,"INSERT INTO `master_list`(master_no,alacarte_no,dessert_no,pasta_no,rice_no,wine_no,member_id) 
                            VALUES ('$master_no','$alacarte_no','$dessert_no','$pasta_no','$rice_no','$wine_no','$member_id')");

                            if($success){
                $message = "I was here!!!";
                echo "<script type='text/javascript'>alert('$message');</script>";
                            }
                            else
                                echo "Query Failed.";

mysqli_close($con);

            }           
        ?>

HTML:

<form method="post" action = "">
        <table border='2' cellpadding=4 cellspacing=1 width=50% bgcolor=white align=center >
        <br>
            <tr>
                <td><font face=rockwell font size=5 font color=black>Master No.:</td>
                <td class="td-center"><input type="text" name="master_no" /></td>
            </tr>
            <tr>
                <td><font face=rockwell font size=5 font color=black>Alacarte No.:</td>
                <td class="td-center"><input type="text" name="alacarte_no" /></td>
            </tr>
            <tr>
                <td><font face=rockwell font size=5 font color=black>Dessert No.:</td>
                <td class="td-center"><input type="text" name="dessert_no" /></td>
            </tr>
            <tr>
                <td><font face=rockwell font size=5 font color=black>Pasta No.:</td>
                <td class="td-center"><input type="text" name="pasta_no" /></td>
            </tr>
            <tr>
                <td><font face=rockwell font size=5 font color=black>Rice No.:</td>
                <td class="td-center"><input type="text" name="rice_no" /></td>
            </tr>
            <tr>
                <td><font face=rockwell font size=5 font color=black>Wine No.:</td>
                <td class="td-center"><input type="text" name="wine_no" /></td>
            </tr>
            <tr>
                <td><font face=rockwell font size=5 font color=black>Member ID:</td>
                <td class="td-center"><input type="text" name="member_id" /></td>
            </tr>

            <tr>
                <td>&nbsp;</td>
                <td class="td-center">
                    <input type="submit" name="add-submit" value="Add"/>
                    <input type="Reset"/>
                    <a href="member-profile.php">
                        <input type="button" name="home" value="home"/>
                    </a><br>
                </td>
            </tr>
        </table>

    </form>

EDIT:

The above code works and to clarify it, I tested it myself. Below are the pictures attached.

The main page

After submitting the form

The results in the database

SECOND EDIT:

To show OP that if he had changed the column to INT type, it would still work:

enter image description here

Community
  • 1
  • 1
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • Thanks for this @HawasKaPujaari, however when I click save, nothing happens, no error on inspect element and it still doesn't insert on mySQL. – user3545006 Sep 27 '15 at 10:03
  • I forgot to change the name to "add-submit". Now there's an error: "78788787878787Query Failed." Where 78788 etc. is the number inputs. – user3545006 Sep 27 '15 at 10:05
  • @user3545006 it means the _query failed_ `echo` the query and what do you see? match the column names with the ones in your database. (remember they are case sensitive) – DirtyBit Sep 27 '15 at 10:07
  • all column names match in the database though. is it because the fields are in INT type? – user3545006 Sep 27 '15 at 10:21
  • @user3545006 that should not make any difference as I just changed one of the column type and tested it again. works fine. check the second edit to see the table structure `;-)` – DirtyBit Sep 27 '15 at 10:25
  • Thanks a lot @HawasKaPujaari! I just figured out the error. The database has a relationship to other tables and because of that, I need to input the same value. – user3545006 Sep 27 '15 at 10:46
  • @user3545006 Ahh, I am glad you did. Go and do something more amazing now, best of luck `;-)` cheers – DirtyBit Sep 27 '15 at 10:48