Click here to Skip to main content
15,885,753 members
Please Sign up or sign in to vote.
1.00/5 (1 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


<?php

$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
</table>
<button type="button" id="save"></button>
</form>
</body>
</html>

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

$(document).on('click','.remove',function(){
	var id=$(this).attr('id');
	$('#row'+id+'').css('background-color','#ccc');
	$('#row'+id+'').fadeOut('slow');

})

$('#save').click(function(){
	$.ajax({
		url:"ajax.php",
		method:"post",
		data:$("#frm").serialize(),
		success:function(data){
			alert(data);
			$('#frm')[0].reset();
Posted
Updated 6-Nov-18 3:53am

Quote:
$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[^]
 
Share this answer
 
Quote:
$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[^]
 
Share this answer
 
Comments
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900