3 Replies - 34337 Views - Last Post: 16 September 2012 - 07:58 PM Rate Topic: -----

#1 nm19907   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-September 12

how to insert dynamic data to mysql database

Posted 16 September 2012 - 06:52 PM

Hi

I am creating a web app that has a html form which allows you to add textfields dynamically through javascript
this is my javascript code that allows you to do that :


var counter = 1;
var limit = 300;
function addInput(divName){
     if (counter == limit)  {
          alert("You have reached the limit of adding " + counter + " inputs");
     }
     else {
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "Item " + (counter + 1) + " <input type='text' name='item[]'> Price<input type='text' name='price[]'>";
		  
          document.getElementById(divName).appendChild(newdiv);
          counter++;
     }
}




this is my html code:
<body>
<h1>Client Info</h1>
<form name="registeruser" method="post" action="register_script.php">
First Name:<input type="text" name="firstname" id="firstname"/><br/>
Last Name:<input type="text" name="lastname" id="lastname"/><br/>
Username:<input type="text" name="username" id="username"/><br/>
Password:<input type="password" name="password" id="password"/><br/>
Email:<input type="text" name="email" id="email"/><br/>


<h1>Menu</h1>
<div id="dynamicInput">
          Item 1 <input type="text" name="item[]"> Price <input type="text" name="price[]" />
     </div>
     <input type="button" value="Add Items" onclick="addInput('dynamicInput');">


<input type="submit" name="register" value="Register User"/>


</form>



when i try to insert the data from the two fields that are created dynamically only the last fields get inserted to my database i am using the foreach command to loop it.
this is my php code:
<?php

	require 'pEzH9izZNsqCxDTPedvQ/connect.php';
	
	$firstname = mysql_real_escape_string($_POST['firstname']);
	$lastname = mysql_real_escape_string($_POST['lastname']);
	$username = mysql_real_escape_string($_POST['username'] );
	$password =  mysql_real_escape_string( $_POST['password'] );
	$email = mysql_real_escape_string($_POST['email'] );
	$pass = sha1($password);
	
	if(isset($_POST['register']))
	{
		

		$query = "insert into users(first_name,last_name,user_name,user_password,email)values('$firstname','$lastname','$username','$pass','$email')";
		$res = mysql_query($query);
		
		
	
	
	
	
	
	
		}
		
		
		$sql = "CREATE TABLE $lastname(
	id INT NOT NULL AUTO_INCREMENT,
	item VARCHAR(100) NOT NULL,
	price INT NOT NULL,
	PRIMARY KEY(id))"; 
	
	mysql_query($sql);
	
	
	$items = $_POST["item"];
	$prices = $_POST["price"];


 
foreach($items as $a => $B)/>{
$sql1 = "insert into $lastname(item,price)values('$items[$a]','$prices[$a]')";

mysql_query($sql1);
}
?>


i have been searching how to do this and nothing seems to be working

Please help thanks

Is This A Good Question/Topic? 0
  • +

Replies To: how to insert dynamic data to mysql database

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: how to insert dynamic data to mysql database

Posted 16 September 2012 - 07:25 PM

Hey.

If you var_dump($_POST["price"]), what do you get?


A couple of things I'd also l like to point out.

First, when looping through an array like that, you'd do better to use a for loop.
for ($a = 0; $a < count($_POST["item"]); ++$a) { ... }



And second, creating a new table based on incoming user data is pretty much always a bad idea. In your case you seem to just want to store a list of items for each new client. All that requires is what we call a One-to-many relationship. Basically, you create a client table and a menu item table. All client data obviously goes into the clients table, like you already do. Likewise all the menu items go into the menu items table. In order for you to link each item to the client that owns it, you create a Foreign Key column in the menu items table, where you store the ID of the client.

I suggest you look up those terms. They are very important when it comes to proper designing relational databases. I explain some of this briefly in the following tutorial:
Relational Database Design - Normalization
Was This Post Helpful? 0
  • +
  • -

#3 nm19907   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-September 12

Re: how to insert dynamic data to mysql database

Posted 16 September 2012 - 07:32 PM

how do i do the var_dump

This post has been edited by Dormilich: 17 September 2012 - 11:57 AM
Reason for edit:: removed quote

Was This Post Helpful? 0
  • +
  • -

#4 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: how to insert dynamic data to mysql database

Posted 16 September 2012 - 07:58 PM

Copy the line I posted, add a semicolon to the end of it, and paste it pretty much anywhere in your code.

There are a bunch of examples and explanations in the manual entry for the function.

var_dump - PHP Manual
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1