Jump to content

[SOLVED] Insert dynamic fields from dynamic content


timmah1

Recommended Posts

I hope this is posted in the correct forum this time.

 

I have a form that is created dynamically from the fields in the database

<table width="100%" border="0" cellspacing="0" cellpadding="5">
<?php
$array = array();
$result = mysql_query("SELECT * FROM `main`");
$count = 1;
while($row = mysql_fetch_assoc($result))
{
foreach ($row as $k => $v)  {
?>  
<tr>
<td width="150"><strong><?php echo $k; ?>:</strong></td>
<td>
<input type="text" name="contact_[<?php echo $k; ?>]" size="15" style='font-family: Verdana; font-size: 8pt' maxlength="50" /></td>
</tr>
<?php
}
} 
?>   
</table> 

 

It creates the number of fields that is needed from how many fields are in the database.

 

Then, I want to insert that info into the database, here is where I have the problem.

 

I can echo the correct information after posting, but it will not insert into the database

Here is my insert

<?php
    if(isset($_POST['submit'])){

while (list ($name,$val) = @each (mysql_real_escape_string($_POST['contact_']))) {  	

echo $name."=".$val."<br />";

$sql = "INSERT INTO main($name) VALUES ( 
				'$val'";
				mysql_query($sql)
				or die("Sorry, there was a problem adding contact<br /> ".mysql_error());	
}
?>

 

Can anybody see what I'm doing wrong?

 

Thanks in advance

Link to comment
Share on other sites

Thank you Ken2k7, everything inserts now.

 

Now I have another problem, instead of inserting everything on the same row, it adds a new row for every value.

See image

cdb.jpg

Would that be because of the while loop?

 

I don't know any other way to do this

<?php
    if($_POST['submit']){

while (list ($name,$val) = @each (mysql_real_escape_string($_POST['contact_']))) {  	

$sql = "INSERT INTO main($name) VALUES ( 
				'$val');";
				mysql_query($sql)
				or die("Sorry, there was a problem adding contact<br /> ".mysql_error());	
}
echo $name."=".$val."<br />";
?>

Link to comment
Share on other sites

<?php
if (isset($_POST['submit'])) {
     $cols = $vals = '';
     foreach ($_POST['contact_'] as $key => $val) {
          $cols .= $key . ',';
          $vals .= '\'' . mysql_real_escape_string($val) . '\',';
     }
     $cols = rtrim($cols, ',');
     $vals = rtrim($vals, ',');
     $sql = 'INSERT INTO main (' . $cols . ') VALUES (' . $vals . ');';
     mysql_query($sql) or trigger_error('SQL failed.', E_USER_ERROR);
}

Link to comment
Share on other sites

I made this thread unsolved, because this next problem goes hand-in-hand with it.

 

How do I update the database now with this?

 

The form is the same, but I don't understand the proper way to update.

 

I have this at the bottom of my form to declare the id

<?php
$array = array();
$result = mysql_query("SELECT * FROM `main` WHERE id = '".mysql_real_escape_string($_GET['id'])."'");
$count = 1;
while($row = mysql_fetch_assoc($result))
{
foreach ($row as $k => $v)  {
?>  
<tr>
<td width="150"><strong><?php echo $k; ?>:</strong></td>
<td>
<input type="text" name="contact_[<?php echo $k; ?>]" size="15" style='font-family: Verdana; font-size: 8pt' maxlength="50" value="<?php echo $k; ?>" /></td>
</tr>
<?php
if($k == "id"){
$pid = $v;
}
?>

<?php
}
} 
?>  
<input type="text" name="id" size="15" value="<?php echo $pid; ?>" /> 

 

Then this is the update part

<?php
if (isset($_POST['submit'])) {
$cols = $vals = '';
     foreach ($_POST['contact_'] as $key => $val) {
          $cols .= $key . ',';
          $vals .= '\'' . mysql_real_escape_string($val) . '\',';
     }
     $cols = rtrim($cols, ',');
     $vals = rtrim($vals, ',');
 $id = mysql_real_escape_string($_POST['id']);
     $sql = 'UPDATE main SET (' . $cols = $vals . ') WHERE id = ('.$id.');';
     mysql_query($sql) or trigger_error('SQL failed.', E_USER_ERROR);
?>\

 

The error I get is this

Fatal error: SQL failed. in /derek/edit.php on line 19

 

What did I do wrong this time?

 

Thanks again in advance

Link to comment
Share on other sites

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.