FairoojNazeer Posted September 2, 2014 Share Posted September 2, 2014 (edited) 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 September 2, 2014 by mac_gyver code tags please Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 2, 2014 Share Posted September 2, 2014 (edited) 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 September 2, 2014 by jcbones Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.