/    Sign up×
Community /Pin to ProfileBookmark

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

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmMar 07.2014 — 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
Copy linkTweet thisAlerts:
@RajeshkrathorauthorMar 10.2014 — 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
Copy linkTweet thisAlerts:
@RajeshkrathorauthorMar 10.2014 — Hi,

*****************New code this is what it will look a like and i want to insert these information into other table instead of edit.

<i>
</i>&lt;?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();
?&gt;
&lt;table&gt; &lt;tr&gt;
&lt;td &gt;Sl.No..&lt;/td&gt;
&lt;td &gt;Item&lt;/td&gt;
&lt;td &gt;Paper&lt;/td&gt;
&lt;td &gt;Finish&lt;/td&gt;
&lt;td&gt;Price&lt;/td&gt;
&lt;td &gt;Vat&lt;/td&gt;
&lt;td &gt;Total Amount&lt;/td&gt;

&lt;/tr&gt;

&lt;?php
$i=0;
while ($i &lt; $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;
?&gt;
&lt;tr&gt;
&lt;td &gt;&lt;input id="slno" name="slno" type="text" value="&lt;?php echo $f1; ?&gt;" style="width:20px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="album_name" name="album_name" value="&lt;?php echo $f2; ?&gt;" type="text" style="width:150px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="paper_used" name="paper_used" type="text" value="&lt;?php echo $f3; ?&gt;" style="width:85px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="finishing" name="finishing" value="&lt;?php echo $f4; ?&gt;" type="text" style="width:90px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="price" value="&lt;?php echo $f5; ?&gt;" name="price" type="text" style="width:25px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="vat" value="&lt;?php echo $f6; ?&gt;" name="vat" type="text" style="width:35px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="total_amt" name="total_amt" value="&lt;?php echo $f7; ?&gt;" type="text" style="width:50px;"/&gt;&lt;/td&gt;

&lt;/tr&gt;
&lt;?php
$i++;

}


?&gt;
&lt;tr&gt;
&lt;td &gt;&lt;input id="security_code" name="security_code" type="text" value="26" style="width:20px;"/&gt;&lt;/td&gt;
&lt;td&gt;&lt;input id="album_name" name="album_name" value="" type="text" style="width:150px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="paper_used" name="paper_used" type="text" value="" style="width:85px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="finishing" name="finishing" value="" type="text" style="width:90px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="price" value="" name="price" type="text" style="width:25px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="vat" value="" name="vat" type="text" style="width:35px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="total_amt" name="total_amt" value="" type="text" style="width:50px;"/&gt;&lt;/td&gt;

&lt;/tr&gt; <br/>
&lt;tr&gt;
&lt;td &gt;&lt;input id="security_code" name="security_code" value="27" type="text" style="width:20px;"/&gt;&lt;/td&gt;
&lt;td&gt;&lt;input id="album_name" name="album_name" value="" type="text" style="width:150px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="paper_used" name="paper_used" type="text" value="" style="width:85px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="finishing" name="finishing" value="" type="text" style="width:90px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="price" value="" name="price" type="text" style="width:25px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="vat" value="" name="vat" type="text" style="width:35px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="total_amt" name="total_amt" value="" type="text" style="width:50px;"/&gt;&lt;/td&gt;


&lt;/tr&gt; <br/>
&lt;tr&gt;
&lt;td &gt;&lt;input id="security_code" name="security_code" value="28" type="text" style="width:20px;"/&gt;&lt;/td&gt;
&lt;td&gt;&lt;input id="album_name" name="album_name" value="" type="text" style="width:150px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="paper_used" name="paper_used" type="text" value="" style="width:85px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="finishing" name="finishing" value="" type="text" style="width:90px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="price" value="" name="price" type="text" style="width:25px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="vat" value="" name="vat" type="text" style="width:35px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="total_amt" name="total_amt" value="" type="text" style="width:50px;"/&gt;&lt;/td&gt;


&lt;/tr&gt; <br/>
&lt;tr&gt;
&lt;td &gt;&lt;input id="security_code" name="security_code" value="29" type="text" style="width:20px;"/&gt;&lt;/td&gt;
&lt;td&gt;&lt;input id="album_name" name="album_name" value="" type="text" style="width:150px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="paper_used" name="paper_used" type="text" value="" style="width:85px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="finishing" name="finishing" value="" type="text" style="width:90px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="price" value="" name="price" type="text" style="width:25px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="vat" value="" name="vat" type="text" style="width:35px;"/&gt;&lt;/td&gt;
&lt;td &gt;&lt;input id="total_amt" name="total_amt" value="" type="text" style="width:50px;"/&gt;&lt;/td&gt;


&lt;/tr&gt; <br/>
&lt;/table&gt;


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
Copy linkTweet thisAlerts:
@ginerjmMar 10.2014 — Here is how I would do what you start with.
[code=php]
// 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();
[/code]


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.
Copy linkTweet thisAlerts:
@RajeshkrathorauthorMar 11.2014 — 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
Copy linkTweet thisAlerts:
@ginerjmMar 11.2014 — 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:

[code=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;
}
[/[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.
Copy linkTweet thisAlerts:
@RajeshkrathorauthorMar 13.2014 — 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Ⓜ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";

}



?>
Copy linkTweet thisAlerts:
@ginerjmMar 13.2014 — 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?
Copy linkTweet thisAlerts:
@RajeshkrathorauthorMar 15.2014 — hi ginerjm,

The error for

else

{

echo "error";

}

the code going in this part.

thanks
×

Success!

Help @Rajeshkrathor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...