Click here to Skip to main content
14,424,779 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have created a table which consist of two columns id and name .Then I created rows dynmacially.What I want to do is to store data into db.But I am getting "Undefined offset"error inside for loop


$con = mysqli_connect('localhost', 'root', '', 'testing');
$count = $_POST['txt'];

for ($i = 0; $i < $count; ++$i) {
    $query = "insert into emps (name) values('".$_POST['txt'][$i]."')";
    mysqli_query($con, $query);
echo 'ok';

What I have tried:

<form id="frm" name="frm">

<table id="tbl">
<tr><td><input type="text" name="txt[]"><button type="button" id="btn">Add</button>	</td>

</tr>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      u
<button type="button" id="save"></button>

	var i=1;
	$('#tbl').append('<tr id="row'+i+'"><td><input type="text" name="txt[]"><button type="button" id="'+i+'" class="remove">x</button>	</td>')

	var id=$(this).attr('id');


Updated 6-Nov-18 4:53am
Rate this:
Please Sign up or sign in to vote.

Solution 1

$query = "insert into emps (name) values('".$_POST['txt'][$i]."')";

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
Rate this:
Please Sign up or sign in to vote.

Solution 2

$count = $_POST['txt'];

You're missing a call to the count function[^]:
$count = count($_POST['txt']);

Alternatively, use a foreach loop instead of a for loop:
foreach ($_POST['txt'] as $txt)

PHP Tutorial - PHP Form Multi-Value Fields[^]

And make sure you don't ignore Patrice's warning about the SQL Injection vulnerability!

PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]
Member 3722539 7-Nov-18 12:18pm
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100