rubbertoad Posted April 4, 2009 Share Posted April 4, 2009 On the insert side of things... Why is this refusing to add values in the table? mysql_select_db($database_connC, $connC); $query_rsTableDisplay = "SELECT * FROM ck ORDER BY id ASC"; $rsTableDisplay = mysql_query($query_rsTableDisplay, $connC) or die(mysql_error()); $fields_num = mysql_num_fields($rsTableDisplay); for($i=1; $i< mysql_num_fields($rsTableDisplay); $i++) { $a= mysql_field_name($rsTableDisplay, $i); $b = $_POST[mysql_field_name($rsTableDisplay, $i)]; $sql="INSERT INTO ck($a)VALUES('$b')"; $result=mysql_query($sql, $connC); } Both $a and $b do have values is them when I echo them (the correct values), but the table just adds a row with a new id (it's set to auto increment) but all the other cells are blank!?! Quote Link to comment https://forums.phpfreaks.com/topic/152584-inserting-a-row-is-blank/ Share on other sites More sharing options...
charleshill Posted April 4, 2009 Share Posted April 4, 2009 What is the purpose of getting all the rows of the table first? Aren't you just trying to insert new row(s) to the table? edit... I think I figured out what u were trying to do... try this mysql_select_db($database_connC, $connC); $request = mysql_query("SELECT * FROM ck ORDER BY id ASC LIMIT 1", $connC) or die(mysql_error()); while ($row = mysql_fetch_assoc($request)) { $variables = array(); $values = array(); foreach ($row as $field_name => $dummy) { if (isset($_POST[$field_name])) { $variables[] = $field_name; $values[] = sprintf('\'%1$s\'', mysql_real_escape_string(htmlspecialchars((string) $_POST[$field_name]))); } } break; } mysql_free_result($request); if (!empty($variables)) $result = mysql_query(" INSERT INTO ck (" . implode(',', $variables) . ") VALUES (" . implode(',', $values) . ")", $connC); Quote Link to comment https://forums.phpfreaks.com/topic/152584-inserting-a-row-is-blank/#findComment-801373 Share on other sites More sharing options...
rubbertoad Posted April 4, 2009 Author Share Posted April 4, 2009 I would like to understand what it is that you are doing in you code. Sorry I'm still new to this. Basically I'm trying to assign the values contained in $b to the row in the table. Quote Link to comment https://forums.phpfreaks.com/topic/152584-inserting-a-row-is-blank/#findComment-801407 Share on other sites More sharing options...
rubbertoad Posted April 4, 2009 Author Share Posted April 4, 2009 I'm sorry to sound stupid, but could you explain to me what you're doing in the code. It does work exactly the way I need it to, but I would like to understand it. Quote Link to comment https://forums.phpfreaks.com/topic/152584-inserting-a-row-is-blank/#findComment-801418 Share on other sites More sharing options...
charleshill Posted April 5, 2009 Share Posted April 5, 2009 // select the database mysql_select_db($database_connC, $connC); // get a single row from the db table (ck) $request = mysql_query("SELECT * FROM ck LIMIT 1", $connC) or die(mysql_error()); // for every row of data in the result resource ($request) we do stuff... there should be only 1 though while ($row = mysql_fetch_assoc($request)) { $variables = array(); $values = array(); // build the variables and values arrays foreach ($row as $field_name => $dummy) { // if there is a $_POST variable set for a field, add it to variables and values if (isset($_POST[$field_name])) { $variables[] = $field_name; // we do it this way to ensure that the data is safe for use in a database query $values[] = sprintf('\'%1$s\'', mysql_real_escape_string(htmlspecialchars((string) $_POST[$field_name]))); } } break; } // this frees up memory by destroying the mysql result resource mysql_free_result($request); if (!empty($variables)) /* now we can insert the row into the database using the column names (variables) and the corresponding, sanitized post data (values) */ $result = mysql_query(" INSERT INTO ck (" . implode(',', $variables) . ") VALUES (" . implode(',', $values) . ")", $connC); I added comments... Quote Link to comment https://forums.phpfreaks.com/topic/152584-inserting-a-row-is-blank/#findComment-801461 Share on other sites More sharing options...
rubbertoad Posted April 5, 2009 Author Share Posted April 5, 2009 thanks, I'll do some studying now Quote Link to comment https://forums.phpfreaks.com/topic/152584-inserting-a-row-is-blank/#findComment-801492 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.