Jump to content

How to insert the dynamically created text box values to db in php ?


FairoojNazeer

Recommended Posts

my name is fairooj and ama new to php and jquery. i have a proplam. i want your help.


 




<script type="text/javascript">
var count = 0;
$(function(){
$('p#add_field').click(function(){
count += 1;
$('#container').append(
'<strong>Link #' + count + '</strong><br />' 
+ '<input id="field_' + count + '" name="fields[]' + '" type="text" />'
+ '<input id="code_' + count + '" name="code[]' + '" type="text" /><br />' );


});
});
</script> 


 

this is my script to add more text boxes..

 

and this is ma code for insert it to database 

 




<?php
//If form was submitted
if (isset($_POST['btnSubmit'])) {


//create instance of database class
$db = new mysqldb();
$db->select_db();


//Insert static values into users table
$sql_user = sprintf("INSERT INTO users (Username, Password) VALUES ('%s','%s')",
mysql_real_escape_string($_POST['name']),
mysql_real_escape_string($_POST['password']) );  
$result_user = $db->query($sql_user);




//Check if user has actually added additional fields to prevent a php error
if ($_POST['fields']) {


//get last inserted userid
$inserted_user_id = $db->last_insert_id();


//Loop through added fields
foreach ( $_POST['fields'] as $key=>$value ) {


//Insert into websites table
$sql_website = sprintf("INSERT INTO websites (Website_URL, web_Link) VALUES ('%s', '%s')",
      mysql_real_escape_string($value), mysql_real_escape_string($value) );  
$result_website = $db->query($sql_website);
$inserted_website_id = $db->last_insert_id();




//Insert into users_websites_link table
$sql_users_website = sprintf("INSERT INTO users_websites_link (UserID, WebsiteID) VALUES ('%s','%s')",
      mysql_real_escape_string($inserted_user_id),
  mysql_real_escape_string($inserted_website_id) );  
$result_users_website = $db->query($sql_users_website);


}


} else {


?>


 

 

 

the problame web_Link sql table is repeating Website_URL table value....

 

plz how can i solve this

 

am waiting for your reply..

 

Edited by mac_gyver
code tags please
Link to comment
Share on other sites

Mentalist is right, you got a little confused about your structure and what is happening.  I added a little clarity in this for you.

 
//Loop through added fields
foreach ( $_POST['fields'] as $key=>$value ) {
//for clarity:
$url = $value;
$web_link = $_POST['code'][$key];
 
//Insert into websites table
$sql_website = sprintf("INSERT INTO websites (Website_URL, web_Link) VALUES ('%s', '%s')",
      mysql_real_escape_string($url), mysql_real_escape_string($web_link) );  
$result_website = $db->query($sql_website);
$inserted_website_id = $db->last_insert_id();
 
 
 
 
//Insert into users_websites_link table
$sql_users_website = sprintf("INSERT INTO users_websites_link (UserID, WebsiteID) VALUES ('%s','%s')",
      mysql_real_escape_string($inserted_user_id),
  mysql_real_escape_string($inserted_website_id) );  
$result_users_website = $db->query($sql_users_website);
 
 
}
Edited by jcbones
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.