FairoojNazeer Posted September 2, 2014 Share Posted September 2, 2014 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.. Link to comment https://forums.phpfreaks.com/topic/290793-how-to-insert-the-dynamically-created-text-box-values-to-db-in-php/ Share on other sites More sharing options...
mentalist Posted September 2, 2014 Share Posted September 2, 2014 mysql_real_escape_string($value), mysql_real_escape_string($value) ); The first $value should be $key? Link to comment https://forums.phpfreaks.com/topic/290793-how-to-insert-the-dynamically-created-text-box-values-to-db-in-php/#findComment-1489652 Share on other sites More sharing options...
jcbones Posted September 2, 2014 Share Posted September 2, 2014 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); } Link to comment https://forums.phpfreaks.com/topic/290793-how-to-insert-the-dynamically-created-text-box-values-to-db-in-php/#findComment-1489685 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.