Jump to content

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

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.