www.webdeveloper.com
Results 1 to 10 of 10

Thread: Basic insertion in the database.

  1. #1
    Join Date
    Jul 2012
    Posts
    23

    Lightbulb Basic insertion in the database.

    Hi,

    i have a registration form (screenshot attached). I create two table to store them, one customer_master, send item_details. but i want to know how to get values of these (item) textboxes to insert into the database.
    Here is the code by which these fields are generating.

    <?php
    require_once("config.php");
    $query_for_result=mysql_query("SELECT * FROM mytable ORDER BY id ASC");
    $num=mysql_numrows($query_for_result);


    mysql_close();
    ?>
    <table> <tr>
    <td >Sl.No..</td>
    <td >Item</td>
    <td >Paper</td>
    <td >Finish</td>
    <td>Price</td>
    <td >Vat</td>
    <td >Total Amount</td>

    </tr>

    <?php
    $i=0;
    while ($i < $num)
    {

    $f1=mysql_result($query_for_result,$i,"id");
    $f2=mysql_result($query_for_result,$i,"album_name");
    $f3=mysql_result($query_for_result,$i,"paper_used");
    $f4=mysql_result($query_for_result,$i,"finishing");
    $f5=mysql_result($query_for_result,$i,"price");
    $f6=mysql_result($query_for_result,$i,"vat");
    $f7=mysql_result($query_for_result,$i,"total_amt");
    $id=$i+1;
    ?>
    <tr>
    <td><input id="slno" name="security_code" type="text" value="<?php echo $f1; ?>" style="width:20px;"/></td>
    <td ><input id="security_code" name="security_code" value="<?php echo $f2; ?>" type="text" style="width:150px;"/></td>
    <td ><input id="security_code" name="security_code" type="text" value="<?php echo $f3; ?>" style="width:85px;"/></td>
    <td ><input id="security_code" name="security_code" value="<?php echo $f4; ?>" type="text" style="width:90px;"/></td>
    <td ><input id="security_code" value="<?php echo $f5; ?>" name="security_code" type="text" style="width:25px;"/></td>
    <td ><input id="security_code" value="<?php echo $f6; ?>" name="security_code" type="text" style="width:35px;"/></td>
    <td ><input id="security_code" name="security_code" value="<?php echo $f7; ?>" type="text" style="width:50px;"/></td>


    </tr>
    <?php
    $i++;

    }


    ?>
    <tr>
    <td ><input id="security_code" name="security_code" type="text" value="26" style="width:20px;"/></td>
    <td ><input id="security_code" name="security_code" value="" type="text" style="width:150px;"/></td>
    <td ><input id="security_code" name="security_code" type="text" value="" style="width:85px;"/></td>
    <td ><input id="security_code" name="security_code" value="" type="text" style="width:90px;"/></td>
    <td ><input id="security_code" value="" name="security_code" type="text" style="width:25px;"/></td>
    <td ><input id="security_code" value="" name="security_code" type="text" style="width:35px;"/></td>
    <td ><input id="security_code" name="security_code" value="" type="text" style="width:50px;"/></td>


    </tr>
    <tr>
    <td ><input id="security_code" name="security_code" value="27" type="text" style="width:20px;"/></td>
    <td ><input id="security_code" name="security_code" value="" type="text" style="width:150px;"/></td>
    <td ><input id="security_code" name="security_code" type="text" value="" style="width:85px;"/></td>
    <td ><input id="security_code" name="security_code" value="" type="text" style="width:90px;"/></td>
    <td ><input id="security_code" value="" name="security_code" type="text" style="width:25px;"/></td>
    <td ><input id="security_code" value="" name="security_code" type="text" style="width:35px;"/></td>
    <td><input id="security_code" name="security_code" value="" type="text" style="width:50px;"/></td>


    </tr>
    <tr>
    <td ><input id="security_code" name="security_code" value="28" type="text" style="width:20px;"/></td>
    <td ><input id="security_code" name="security_code" value="" type="text" style="width:150px;"/></td>
    <td><input id="security_code" name="security_code" type="text" value="" style="width:85px;"/></td>
    <td ><input id="security_code" name="security_code" value="" type="text" style="width:90px;"/></td>
    <td ><input id="security_code" value="" name="security_code" type="text" style="width:25px;"/></td>
    <td><input id="security_code" value="" name="security_code" type="text" style="width:35px;"/></td>
    <td ><input id="security_code" name="security_code" value="" type="text" style="width:50px;"/></td>


    </tr>
    <tr>
    <td ><input id="security_code" name="security_code" value="29" type="text" style="width:20px;"/></td>
    <td><input id="security_code" name="security_code" value="" type="text" style="width:150px;"/></td>
    <td><input id="security_code" name="security_code" type="text" value="" style="width:85px;"/></td>
    <td ><input id="security_code" name="security_code" value="" type="text" style="width:90px;"/></td>
    <td ><input id="security_code" value="" name="security_code" type="text" style="width:25px;"/></td>
    <td ><input id="security_code" value="" name="security_code" type="text" style="width:35px;"/></td>
    <td><input id="security_code" name="security_code" value="" type="text" style="width:50px;"/></td>


    </tr>
    </table>

    I want to store data of all 29 text boxes. but don't know how to get values of these ?
    Thanks

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    699
    Before you go further, find another MySQL interface since the MySQL_* functions are about to be removed. Look to see if your host supports either mysqli or pdo for database access.

    As it stands now, you need to change from using a function that retrieves just one value at a time to one that retrieves a whole record with one call. Much more efficient that way. Once you learn how to do that then you use a loop to cycle thru each record and ouput a table row with the current row's values.

    You need to re-think your goal here and what you want to do. As it stands you are putting your whole table of data on-screen and then allowing the user to change any record, any field. You have no form tag and no submit button so how do you send those changes back to php? When I have done that, I create a form for each table row and add a submit button to each. Then my php has everything it needs to identify the row to be updated and all the data.

    Good luck

  3. #3
    Join Date
    Jul 2012
    Posts
    23
    Hi,
    thanks ginerjm,
    I think it supports mysqli.
    And i have form tag and submit button too(as you can see on the attachment screenshot), but due to long code i didn't put all my code here.

    So can you suggest how you did, if possible by some code.
    thanks

  4. #4
    Join Date
    Jul 2012
    Posts
    23
    Hi,

    *****************New code this is what it will look a like and i want to insert these information into other table instead of edit.
    Code:
    <?php
    require_once("config.php");
    $query_for_result=mysql_query("SELECT * FROM mytable ORDER BY id ASC");
    $num=mysql_numrows($query_for_result);
    
    
    mysql_close();
    ?>
    <table> <tr>
    <td >Sl.No..</td>
    <td >Item</td>
    <td >Paper</td>
    <td >Finish</td>
    <td>Price</td>
    <td >Vat</td>
    <td >Total Amount</td>
    
    </tr>
    
    <?php
    $i=0;
    while ($i < $num)
    {
    
    $f1=mysql_result($query_for_result,$i,"id");
    $f2=mysql_result($query_for_result,$i,"album_name");
    $f3=mysql_result($query_for_result,$i,"paper_used");
    $f4=mysql_result($query_for_result,$i,"finishing");
    $f5=mysql_result($query_for_result,$i,"price");
    $f6=mysql_result($query_for_result,$i,"vat");
    $f7=mysql_result($query_for_result,$i,"total_amt");
    $id=$i+1;
    ?>
    <tr>
    <td ><input id="slno" name="slno" type="text" value="<?php echo $f1; ?>" style="width:20px;"/></td> 
       <td ><input id="album_name" name="album_name" value="<?php echo $f2; ?>" type="text" style="width:150px;"/></td> 
       <td ><input id="paper_used" name="paper_used" type="text" value="<?php echo $f3; ?>" style="width:85px;"/></td> 
       <td ><input id="finishing" name="finishing"  value="<?php echo $f4; ?>" type="text" style="width:90px;"/></td> 
       <td ><input id="price"  value="<?php echo $f5; ?>" name="price" type="text" style="width:25px;"/></td> 
       <td ><input id="vat"  value="<?php echo $f6; ?>" name="vat" type="text" style="width:35px;"/></td> 
       <td ><input id="total_amt" name="total_amt"  value="<?php echo $f7; ?>" type="text" style="width:50px;"/></td> 
    
    </tr> 
    <?php 
    $i++; 
    
    } 
    
    
    ?> 
    <tr> 
    <td ><input id="security_code" name="security_code" type="text" value="26" style="width:20px;"/></td> 
    <td><input id="album_name" name="album_name" value="" type="text" style="width:150px;"/></td> 
       <td ><input id="paper_used" name="paper_used" type="text" value="" style="width:85px;"/></td> 
       <td ><input id="finishing" name="finishing"  value="" type="text" style="width:90px;"/></td> 
       <td  ><input id="price"  value="" name="price" type="text" style="width:25px;"/></td> 
       <td ><input id="vat"  value="" name="vat" type="text" style="width:35px;"/></td> 
       <td ><input id="total_amt" name="total_amt"  value="" type="text" style="width:50px;"/></td> 
    
    </tr>  
    <tr> 
    <td ><input id="security_code" name="security_code" value="27" type="text" style="width:20px;"/></td> 
    <td><input id="album_name" name="album_name" value="" type="text" style="width:150px;"/></td> 
       <td ><input id="paper_used" name="paper_used" type="text" value="" style="width:85px;"/></td> 
       <td ><input id="finishing" name="finishing"  value="" type="text" style="width:90px;"/></td> 
       <td  ><input id="price"  value="" name="price" type="text" style="width:25px;"/></td> 
       <td ><input id="vat"  value="" name="vat" type="text" style="width:35px;"/></td> 
       <td ><input id="total_amt" name="total_amt"  value="" type="text" style="width:50px;"/></td> 
    
    
    </tr>  
    <tr> 
    <td ><input id="security_code" name="security_code" value="28" type="text" style="width:20px;"/></td> 
    <td><input id="album_name" name="album_name" value="" type="text" style="width:150px;"/></td> 
       <td ><input id="paper_used" name="paper_used" type="text" value="" style="width:85px;"/></td> 
       <td ><input id="finishing" name="finishing"  value="" type="text" style="width:90px;"/></td> 
       <td  ><input id="price"  value="" name="price" type="text" style="width:25px;"/></td> 
       <td ><input id="vat"  value="" name="vat" type="text" style="width:35px;"/></td> 
       <td ><input id="total_amt" name="total_amt"  value="" type="text" style="width:50px;"/></td> 
    
    
    </tr>  
    <tr> 
    <td ><input id="security_code" name="security_code" value="29" type="text" style="width:20px;"/></td> 
    <td><input id="album_name" name="album_name" value="" type="text" style="width:150px;"/></td> 
       <td ><input id="paper_used" name="paper_used" type="text" value="" style="width:85px;"/></td> 
       <td ><input id="finishing" name="finishing"  value="" type="text" style="width:90px;"/></td> 
       <td  ><input id="price"  value="" name="price" type="text" style="width:25px;"/></td> 
       <td ><input id="vat"  value="" name="vat" type="text" style="width:35px;"/></td> 
       <td ><input id="total_amt" name="total_amt"  value="" type="text" style="width:50px;"/></td> 
    
    
    </tr>  
    </table>
    look like more clear now.
    I want to store data of all 29 text boxes. but don't know how to get values of these ?
    Thanks

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    699
    Here is how I would do what you start with.
    PHP Code:
    //  turn on error checking and display.
    error_reporting(E_ALL E_STRICT E_NOTICE);
    ini_set('display_errors''1');    // turn this off for production
    require_once("config.php");
    //   setup query to get data
    $q "SELECT * FROM mytable ORDER BY id ASC";
    $query_for_result=mysql_query($q);
    if (!
    $query_for_result)
    {
        echo 
    "Query did not run - error msg is ".mysql_error()."<br>Query is $q";
        exit();
    }
    //  setup html table with HEADERS INSTEAD OF ROW for titles
    //  use php's 'heredocs' construct to output html and php easily
    $code =<<<heredocs
        <table>
        <col width="4%">
        <col width="33%">
        <col width="19%">
        <col width="20%">
        <col width="5%">
        <col width="8%">
        <col width="11%">
        <tr>
        <th >Sl.No..</th>
        <th >Item</th>
        <th >Paper</th>
        <th >Finish</th>
        <th>Price</th>
        <th >Vat</th>
        <th >Total Amount</th>
        </tr>
    heredocs;
    echo 
    $code;
    //  loop thru query results getting each row with one function call and
    //  parse each result column into a variable name
    while (list($id,$album_name,$paper_used,$finishing,$price,$vat,$total_amt) = mysql_fetch_assoc($query_for_result))
    {
        
    // removed styles by using html <col> tag to set width. 
        // Should be using CSS to set styles instead of the same inline styles repeatedly.
        //  
        
    $code=<<<heredocs
        <tr>
        <td >
        <input id="slno" name="slno" type="text" value="
    $id">
        </td> 
        <td >
        <input id="album_name" name="album_name" value="
    $album_name" type="text">
        </td> 
        <td >
        <input id="paper_used" name="paper_used" type="text" value="
    $paper_used>">
        </td> 
        <td >
        <input id="finishing" name="finishing"  value="
    $finishing" type="text">
        </td> 
        <td >
        <input id="price"  value="
    $price" name="price" type="text">
        </td> 
        <td >
        <input id="vat"  value="
    $vat" name="vat" type="text">
        </td> 
        <td >
        <input id="total_amt" name="total_amt"  value="
    $total_amt" type="text">
        </td> 
        </tr> 
    heredocs;
        echo 
    $code;

    echo 
    "</table>";
    echo 
    "</body></html>";
    exit(); 
    I don't know what all the rest of your table rows are for so I didn't repeat them here. Hopefully this shows you how easy it is to retrieve data and generate table rows from it.

  6. #6
    Join Date
    Jul 2012
    Posts
    23
    Hi ginerjm,
    Showing data is not a concern for me,
    i want to insert these data after taking values of these 29 fields/rows , and the rest 4 blank rows are for additional data that may be fill if necessary and will go with these 25 rows into the database table.

    Thanks

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    699
    If I understand, you want to display the contents of your table and then allow for the adding of additional records. If you are not allowing for updates to the data, then I would not use input tags for the existing data, only for the blank row that you will use to add a record.

    As for adding recs, place a 'submit' button at the end of each blank record on the display so that you can recognize and insert the data correctly. Here's how I would create the 'blank' rows:

    [php]
    // add 3 additional blank rows
    //**********************
    for ($i=1;$i<3;$i++)
    {
    $code=<<<heredocs
    <tr>
    <td >
    <input type="text" name="slno$i" value="">
    </td>
    <td >
    <input type="text" name="album_name$i" value="">
    </td>
    <td >
    <input type="text" name="paper_used$i" value="">
    </td>
    <td >
    <input type="text" name="finishing$i" value="">
    </td>
    <td >
    <input type="text" name="price$i" value="">
    </td>
    <td >
    <input type="text" name="vat$i" value="">
    </td>
    <td >
    <input type="text" name="total_amt$i" value="">
    </td>
    <td >
    <input type="submit" id="submit$i" name="submit" value="Add New Record">
    </td>
    </tr>
    heredocs;
    echo $code;
    }
    [/[php]

    To process this one would check for a $_POST['submit'] with a value of "Add New Record" and if found, check for it's id value and extract the number at the end of it and then grab the name= values that end with the number.
    Of course if you only did one blank row at a time, then you would not need to do that.

  8. #8
    Join Date
    Jul 2012
    Posts
    23
    hi,
    I change my code accordingly but this show error, going to else part
    here is my code..
    <?php
    require_once("config.php");
    $query_for_result=mysqli_query($con,"SELECT * FROM items ORDER BY id ASC");
    $num=mysqli_num_rows($query_for_result);
    mysqli_close($con);

    ?>
    <table width="50%" align="center" border="0" cellspacing="0" cellpadding="5" style="font-family:Verdana, Arial, Helvetica, sans-serif; background:#87bb2f; -moz-border-radius: 14px; border-radius: 14px; font-size:12px; color:#000000; border:solid #d6d4d5; border-width:1px; margin-top:-1px;"> <tr>
    <td valign="top" style="font-size:11px; background:#87bb2f; font-weight:bold;" align="center" >Sl.No..</td>
    <td valign="top" style="font-size:11px; background:#87bb2f; font-weight:bold;" align="center" >Item</td>
    <td valign="top" style="font-size:11px; background:#87bb2f; font-weight:bold;" align="center" >Paper</td>
    <td valign="top" style="font-size:11px; background:#87bb2f; font-weight:bold;" align="center" >Finish</td>
    <td valign="top" style="font-size:11px; background:#87bb2f; font-weight:bold;" align="center" >Price</td>
    <td valign="top" style="font-size:11px; background:#87bb2f; font-weight:bold;" align="center" >Vat</td>
    <td valign="top" style="font-size:11px; background:#87bb2f; font-weight:bold;" align="center" >Total Amount</td>

    </tr>

    <?php

    $i=0;

    while($row = mysqli_fetch_assoc($query_for_result))
    {
    $f1=$row['id'];
    $f2=$row['album_name'];
    $f3=$row['paper_used'];
    $f4=$row['finishing'];
    $f5=$row['price'];
    $f6=$row['vat'];
    $f7=$row['total_amt'];

    $id=$i+1;

    ?>
    <tr>
    <td valign="top" style="font-size:11px; " align="center" ><input id="slno" name="slno[]" type="text" value="<?php echo $f1; ?>" style="width:20px;"/></td>
    <td valign="top" style="font-size:11px; " align="center" ><input id="album_name" name="album_name[]" value="<?php echo $f2; ?>" type="text" style="width:150px;"/></td>
    <td valign="top" style="font-size:11px; " align="center" ><input id="paper_used" name="paper_used[]" type="text" value="<?php echo $f3; ?>" style="width:85px;"/></td>
    <td valign="top" style="font-size:11px; " align="center" ><input id="finishing" name="finishing[]" value="<?php echo $f4; ?>" type="text" style="width:90px;"/></td>
    <td valign="top" style="font-size:11px; " align="center" ><input id="price" value="<?php echo $f5; ?>" name="price[]" type="text" style="width:25px;"/></td>
    <td valign="top" style="font-size:11px; " align="center" ><input id="vat" value="<?php echo $f6; ?>" name="vat[]" type="text" style="width:35px;"/></td>
    <td valign="top" style="font-size:11px; " align="center" ><input id="total_amt" name="total_amt[]" value="<?php echo $f7; ?>" type="text" style="width:50px;"/></td>


    </tr>
    <?php
    $i++;

    }


    ?>

    </table>

    <?php

    if(isset($_POST['submit']))
    {
    require_once("config.php");
    foreach($_POST['slno'] as $k => $Sl_No)
    {
    if(!empty($Sl_No))
    {
    $customer_id="1";
    $customer_name="Mr.Rajesh";
    $customer_user_name="rkr";
    $date=date('d-m-Y');
    $time=date('h:m:i:A');
    $Sl_No = mysqli_real_escape_string(trim($_POST['slno'][$k]));
    $album_name = mysqli_real_escape_string(trim($_POST['album_name'][$k]));
    $paper_used = mysqli_real_escape_string(trim($_POST['paper_used'][$k]));
    $finishing = mysqli_real_escape_string(trim($_POST['finishing'][$k]));
    $price = mysqli_real_escape_string(trim($_POST['price'][$k]));
    $vat = mysqli_real_escape_string(trim($_POST['vat'][$k]));
    $total_amount = mysqli_real_escape_string(trim($_POST['total_amt'][$k]));

    $values[] = "('$customer_id','$customer_name','$customer_user_name','$Sl_No','$album_name','$paper_used','$finis hing','$price','$vat','$total_amount','$date','$time')";
    }
    }
    $sql = "INSERT INTO mytable
    (customer_id, customer_name, customer_user_name, sl_no, album_name, paper, finish, price, vat, total_amt, date, time)
    values ";
    $sql .= implode(",",$values);
    mysqli_query($con,$sql) or die(mysqli_connect_error());
    }
    else
    {
    echo "error";
    }



    ?>

  9. #9
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    699
    Still using inline styles - over and over again. Very wasteful.

    Anyway - you say you have an error . What is the error message please? Could it have anything to do with the fact that you layout a page and then start checking for input while the user hasn't seen it yet?

  10. #10
    Join Date
    Jul 2012
    Posts
    23
    hi ginerjm,
    The error for
    else
    {
    echo "error";
    }
    the code going in this part.
    thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles