Stock Recording System - I basically wants to see the data recorded in the table sql. yet this is not the case.

mysql_query("INSERT INTO Produk FROM stock VALUES ('Produk')");
mysql_query("INSERT INTO Jumlah FROM stock VALUES ('Jumlah')");
mysql_query("INSERT INTO Tanggal_Masuk FROM stock VALUES ('Tanggal_Masuk')");

Is that instruction not enough to record data? Thanks.

<h4>STOCK RECORDING SYSTEM</h4>

<form action="action.php" method="post">

<table border="0" width="300">
<tr>
    <tr>
        <td>Produk  :</td><br>
        <td>
          <select name="batch" onchange="submit();">
            <option value="-1" >-- Select Address --</option>
            <?php

            $username='root';
            $password='';

            $con = mysql_connect('localhost', $username, $password);
            mysql_select_db('snack', $con);

            $result = mysql_query("SELECT * FROM PO");

            while($row = mysql_fetch_array($result))
            {
             ?>
            <option value="<?php echo $row['PO'];?>">
            <?php echo $row['PO'];?></option>
            <?php            
            }


            ?>
        </select>
        </td>    
    </tr>    
    <tr>
        <td>Jumlah  :</td>
        <td><input type="text" size="12" maxlength="22" name="Jumlah" value="2 box 1 pack"></td><br> 
    <tr>
        <?php // use date picker to automate date ?>
        <td>Tanggal :</td>
        <td><input type="date" id="datepicker" size="12" maxlength="22" name="Tanggal" value=""></td><br>
    </tr>    
</table>


</form>

<?php

$username='root';
$password='';

$con = mysql_connect('localhost', $username, $password);
mysql_select_db('snack', $con);
?>
<form action="print.php" method="post">
<?php
mysql_query("INSERT INTO Produk FROM stock VALUES ('Produk')");
mysql_query("INSERT INTO Jumlah FROM stock VALUES ('Jumlah')");
mysql_query("INSERT INTO Tanggal_Masuk FROM stock VALUES ('Tanggal_Masuk')");
?>
<input type="submit" value="Submit">
</form>

</html>

The syntax is this (if the tables are identical):

INSERT INTO Produk SELECT * FROM stock

no VALUES ?

I tried and they do not save the data into mysql tables.

INSERT INTO `table` (`field`,`field2`,`field3`) VALUES ('value1','value2',4);

or expanding on the select query pritaeas posted, for different table structures

INSERT INTO `table` (`fielda`,`fieldb`,`fieldc`) select `field1`,`field2`,`field3` FROM `table2`;

Still it won't store the information.

<form action="print.php" method="post">
    <?php
    mysql_query("INSERT INTO Stock ('Produk', 'Jumlah', 'Tanggal Masuk') VALUES ('Produk', 'Jumlah', 'Tanggal')");
    ?>
    <input type="submit" value="Submit">

back ticks not single quotes, they are next to 1 under the escape key( on my keyboard at least). you use back ticks to enclose field or table names in mysql

mysql_query("INSERT INTO Stock (`Produk`, `Jumlah`, `Tanggal Masuk`) VALUES ('Produk', 'Jumlah', 'Tanggal')");

I change it. It still won't store any data. I wonder why.

I try to enter the mysql_query in the phpmyadmin:

1054 - Unknown column 'Tanggal' in 'field list'

INSERT INTO Stock( Produk , Jumlah , Tanggal Masuk )
VALUES (
Produk , Jumlah , Tanggal
)

Still won't record the data:

<h4>STOCK RECORDING SYSTEM</h4>

<form action="action.php" method="post">

<table border="0" width="300">
<tr>
    <tr>
        <td>Produk  :</td><br>
        <td>
          <select name="batch" onchange="submit();">
            <option value="-1" >-- Select Address --</option>
            <?php

            $username='root';
            $password='';

            $con = mysql_connect('localhost', $username, $password);
            mysql_select_db('snack', $con);

            $result = mysql_query("SELECT * FROM PO");

            while($row = mysql_fetch_array($result))
            {
             ?>
            <option value="<?php echo $row['PO'];?>">
            <?php echo $row['PO'];?></option>
            <?php            
            }


            ?>
        </select>
        </td>    
    </tr>    
    <tr>
        <td>Jumlah  :</td>
        <td><input type="text" size="12" maxlength="22" name="Jumlah" value="2 box 1 pack"></td><br> 
    <tr>
        <?php // use date picker to automate date ?>
        <td>Tanggal :</td>
        <td><input type="date" id="datepicker" size="12" maxlength="22" name="Tanggal" value=""></td><br>
    </tr>    
</table>


</form>

<?php

$username='root';
$password='';

$con = mysql_connect('localhost', $username, $password);
mysql_select_db('snack', $con);
?>
<form action="print.php" method="post">
<?php
mysql_query("INSERT INTO Stock (`Produk`, `Jumlah`,`Tanggal`) VALUES (`PO`, `Jumlah`, `Tanggal`)");
?>
<input type="submit" value="Submit">
</form>

</html>

I never use backticks, or quotes around field names, also make sure there are no spaces

mysql_query("INSERT INTO Stock (Produk,Jumlah,Tanggal) VALUES ('PO','Jumlah','Tanggal')");

spaces will upset mysql!

Member Avatar for diafol

I never use backticks, or quotes around field names, also make sure there are no spaces

It's good practice to do so as reserved words can often mess up your queries. Anyway - backticks around fieldnames, single quotes around values (if in a double quoted string and data is not numeric).

Tony's sql should work.

It's good practice to do so as reserved words can often mess up your queries

Quite right Diafol, I tend to forget as I only code for myself and therefore shouldn't mess it up!

Produk Jumlah Tanggal (title)
Stick Biru 2 box 1 pack 2013-01-25
PO Jumlah 0000-00-00

I get this as a result. The first row I insert it manually. The next one, by form. I revise the mysql just like Tony said. It's getting closer yet it just no quite right.

<h4>STOCK RECORDING SYSTEM</h4>


<form action="print.php" method="post">

<table border="0" width="300">
<tr>
    <tr>
        <td>Produk  :</td><br>
        <td>
          <select name="batch" onchange="submit();">
            <option value="-1" >-- Select Address --</option>
            <?php

            $username='root';
            $password='';

            $con = mysql_connect('localhost', $username, $password);
            mysql_select_db('snack', $con);

            $result = mysql_query("SELECT * FROM PO");

            while($row = mysql_fetch_array($result))
            {
             ?>
            <option value="<?php echo $row['PO'];?>">
            <?php echo $row['PO'];?></option>
            <?php            
            }


            ?>
        </select>
        </td>    
    </tr>    
    <tr>
        <td>Jumlah  :</td>
        <td><input type="text" size="12" maxlength="22" name="Jumlah" value="2 box 1 pack"></td><br> 
    <tr>
        <?php // use date picker to automate date ?>
        <td>Tanggal :</td>
        <td><input type="date" id="datepicker" size="12" maxlength="22" name="Tanggal" value=""></td><br>
    </tr>    
    <?php
        mysql_query("INSERT INTO Stock (Produk,Jumlah,Tanggal) VALUES ('PO','Jumlah','Tanggal')");
    ?>
    </table>

    <input type="submit" value="Submit">

</form>   
</html>

I just revisited your code, several points to check on.
The <form> has action="print.php" , so it's sending post data elsewhere.
The mysql query is within the <form> tags.
If you are 'POST'ing your form you need to create the variables to put into mysql
i.e.
$PO=$_POST['batch'];
$tanggal=$_POST['Tanggal'];
etc;
And your <select> element has the onClick method so it's gonna submit the form as soon as it's changed.

I still don't get it. How exactly I have to code it.

I don't wish to seem rude or unhelpful, but we have given you a lot of help on this one already. Download the manual that Pritaeas pointed you to and also the php manual is downloadable, by using those carefully you should be able to work this out for yourself.
Nobody is going to write the code for you.

Well here is a revision:

<form action="print.php" method="post">

<table border="0" width="300">
<tr>
    <tr>
        <td>Produk  :</td><br>
        <td>
          <select name="batch" onchange="submit();">
            <option value="-1" >-- Select Address --</option>
            <?php

            $username='root';
            $password='';

            $con = mysql_connect('localhost', $username, $password);
            mysql_select_db('snack', $con);

            $result = mysql_query("SELECT * FROM PO");

            while($row = mysql_fetch_array($result))
            {
             ?>
            <option value="<?php echo $row['PO'];?>">
            <?php echo $row['PO'];?></option>
            <?php            
            }

            ?>
        </select>
        </td>    
    </tr>    
    <tr>
        <td>Jumlah  :</td>
        <td><input type="text" size="12" maxlength="22" name="Jumlah" value="2 box 1 pack"></td><br> 
    <tr>
        <?php // use date picker to automate date ?>
        <td>Tanggal :</td>
        <td><input type="date" id="datepicker" size="12" maxlength="22" name="Tanggal" value=""></td><br>
    </tr>    
    <?php
        mysql_query("INSERT INTO Stock (Produk,Jumlah,Tanggal) VALUES ('$_POST[PO]','$_POST[Jumlah]','$_POST[Tanggal]')");
    ?>
    </table>

Notice: Undefined index: PO in C:\xampp\htdocs\Innovation\script_shop\stock.php on line 70

Notice: Undefined index: Jumlah in C:\xampp\htdocs\Innovation\script_shop\stock.php on line 70

Notice: Undefined index: Tanggal in C:\xampp\htdocs\Innovation\script_shop\stock.php on line 70

line 70: mysql_query("INSERT INTO Stock (Produk,Jumlah,Tanggal) VALUES ('$_POST[PO]','$_POST[Jumlah]','$_POST[Tanggal]')");

Try This:

<form action="" method="post">
    <table border="0" width="300">
    <tr>
    <tr>
    <td>Produk :</td><br>
    <td>
    <select name="batch">
    <option value="-1" >-- Select Address --</option>
    <?php
    $username='root';
    $password='';
    $con = mysql_connect('localhost', $username, $password);
    mysql_select_db('snack', $con);
    $result = mysql_query("SELECT * FROM PO");
    while($row = mysql_fetch_array($result))
    {
    ?>
    <option value="<?php echo $row['PO'];?>">
    <?php echo $row['PO'];?></option>
    <?php
    }
    ?>
    </select>
    </td>
    </tr>
    <tr>
    <td>Jumlah :</td>
    <td><input type="text" size="12" maxlength="22" name="Jumlah" value="2 box 1 pack"></td><br>
    <tr>
    <!-- use date picker to automate date -->
    <td>Tanggal :</td>
    <td><input type="date" id="datepicker" size="12" maxlength="22" name="Tanggal" value=""></td><br>
    </tr>
    <tr> <td align="center" colspan="2"><input type="submit" name="submit" value="Send"></td>
    </tr>
    </table>
    </form>


    <?php
    if(isset($_POST['submit'])){
    $PO=$_POST['batch']; //the name of your select element
    $Jumlah=$_POST['Jumlah'];
    $Tanggal=$_POST['Tanggal'];

    $result=mysql_query("INSERT INTO Stock (Produk,Jumlah,Tanggal) VALUES ('$PO','$Jumlah','$Tanggal')");
    // for debugging
    if(!$result){
    echo "Query not processed".mysql_error();
    }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.