Jump to content

Badic insertion in the database.


rajeshkr

Recommended Posts

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.

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

 

 

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

 

Thanks

Edited by rajeshkr
Link to comment
Share on other sites

If you are allowing users to edit the database records from your form you need to give each field in your form a unique name, currently you are giving all the fields the same name of security_code.

 

I'd setup the form like this

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

?>
<form method="post" action="update.php">
<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
while ($row = mysql_fetch_assoc($result))
{
	$id = $row['id']
?>
	<tr>
		<td><?php echo $id; ?></td>
		<td><input name="item[<?php echo $id; ?>][album_name]" value="<?php echo $row['album_name'] ?>" type="text" style="width:150px;"/></td>
		<td><input name="item[<?php echo $id; ?>][paper_used]" value="<?php echo $row['paper_used'] ?>" style="width:85px;"/></td>
		<td><input name="item[<?php echo $id; ?>][finishing]" value="<?php echo $row['finishing'] ?>" type="text" style="width:90px;"/></td>
		<td><input name="item[<?php echo $id; ?>][price]" value="<?php echo $row['price'] ?>" type="text" style="width:25px;"/></td>
		<td><input name="item[<?php echo $id; ?>][vat]" value="<?php echo $row['vat'] ?>" type="text" style="width:35px;"/></td>
		<td><input name="item[<?php echo $id; ?>][total_amt]" value="<?php echo $row['total_amt'] ?>" type="text" style="width:50px;"/></td>

	</tr>
<?php
}

?>
	</tr> 
</table>
</form>

Here the field names are named in this format item[<record id>][<table field name>]

 

When the form is submitted $_POST['item'] will contain a multidimensional array of records to be updated in the database. In update.php the code for updating the records will be like

<?php 

require_once("config.php");

// the data tpes of each field
$data_type = array(
	'album_name' => 'string',
	'paper_used' => 'string',
	'finishing'  => 'string',
	'price'      => 'float',
	'vat'        => 'float',
	'total_amt'  => 'float',
);

if(isset($_POST['submit']))
{
    if(is_array($_POST['item']))
    {
    // loop over the items in the field
    foreach($_POST['item'] as $record_id as $fields)
    {
        // dynamically create the update query for updating the record
        $query = 'UPDATE mytable SET ';
        // loop over the fields
        foreach($fields as $field_name => $field_value)
        {
            // sanitize the field value based on field data type
            switch($data_type[$field_name])
            {
                case 'float':
                    $field_value = floatval($field_value);
                break;

                case 'string':
                default:
                    $field_value = mysql_real_escape_string($field_value);

            }

            $query .= "$field_name = '$field_value' ";
        }

            // which record is being updated
            $query .= 'WHERE id=' . intval($record_id);

            // update the record
            $result = mysql_query($query);

            if($result && mysql_affected_rows())
            {
                echo "Record #$record_id has been updated!<br />";
            }
        }
    }
}

?>
Edited by Ch0cu3r
Link to comment
Share on other sites

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.

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.

******************************************************************************************* This is the new code. and i want to insert these fields into other table. not update in existing one.

<?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="slno" name="slno" 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>

 

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

Thanks

 

 

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

 

Thanks

Edited by rajeshkr
Link to comment
Share on other sites

Why? Storing the same data in multiple tables is not very good database design.

 

What purpose does this serve?

Hi,

thanks but the reason behind this is? this the first phase of the list that will be same for every customer. but at this time of registration these prices will be change customer by customer, so every customer will have different price list.

thanks

Link to comment
Share on other sites

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','$finishing','$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";




?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.