Jump to content

Inserting a row is blank.


rubbertoad

Recommended Posts

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!?!

Link to comment
https://forums.phpfreaks.com/topic/152584-inserting-a-row-is-blank/
Share on other sites

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);

// 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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.