Jump to content

Archived

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

craygo

space in input name

Recommended Posts

I have inherited a database adn the tables have spaces in the field names. I have a script to update the record but when the POST data is sent it is sending the field name thru with an "_" instead of a space.

Here in the page
[code]<?php
if(isset($_POST['submit'])){

  $cpc = $_POST['CPC'];

  foreach($_POST as $field => $value){
    if($field <> 'CPC'){
      if($field <> 'submit'){
        if($field <> 'keyword'){
          if($field <> 'stype'){
          if($field <> 'field'){

          $sql = "UPDATE parcels SET
            `".$field."` = ".$value." WHERE CPC = '$cpc'";

            echo "$sql<br>";
            //$res = mysql_query($sql) or die (mysql_error());
            }
          }
        }
      }
    }
  }

  echo "Data inserted into table<br>
        Click <a href=\"home.php?keyword=".$_POST['keyword']."&stype=".$_POST['stype']."&field=".$_POST['field']."&submit=submit\">HERE</a> to return to list or<br>
        Click <a href=home.php>HERE</a> to return to search page";
} else {
?>
<table width=600 align=center border=1>
  <form name=cpcform method=POST action="<?=$_SERVER['PHP_SELF']?>">
  <input type=hidden name=CPC value="<?=$_GET['cpc']?>">
  <input type=hidden name=keyword value="<?=$_GET['keyword']?>">
  <input type=hidden name=stype value="<?=$_GET['stype']?>">
  <input type=hidden name=field value="<?=$_GET['field']?>">
<?php
// start your count here for row offsets
$k=0;
// Query database for data
$sql = "SELECT * FROM parcels WHERE CPC='". $_GET['cpc']."'" ;
// run query
  $res = mysql_query($sql) or die (mysql_error());
//count how many fields in table
$num_fields = mysql_num_fields($res);
// Get data from query
$r = mysql_fetch_row($res);
// start loop
    while($k < $num_fields){
      // Fetch field names
      $meta = mysql_fetch_field($res, $k);
      // Substitute N/A if field is blank
      if(empty($r[$k])){
        $field = "N/A";
        } else {
        $field = $r[$k];
        }
        // Start output rows

        echo "<tr>\n";
        echo "<td width=150><font color=#FF0000>".$meta->name."</font></td>\n";
        if($meta->name == 'CPC'){
        echo "<td width=200><b><input type=text name=\"".$meta->name."\" value=\"$field\" size=60 disabled></b></td>\n";
        } else {
        echo "<td width=200><input type=text name=\"".$meta->name."\" value=\"$field\" size=60></td>\n";
        }
        echo "</tr>\n";

$k++;
}
echo "<tr>
      <td colspan=2 align=center><input type=submit name=submit value=Submit></td>
      </tr>
      </form>
      </table>";
}
?>[/code]

One of the field names is Old Plat. When I look at the source code for the form it says
[code]<td width=200><input type=text name="Old Lot" value="937, 938; 74, 72 & 83" size=60></td>[/code]

But when the form is submitted the $_POST is called $_POST['Old_Lot'] Not $_POST['Old Lot']

Thanks

Ray

Share this post


Link to post
Share on other sites
Try renaming the field to "Old_Lot", will work.

Share this post


Link to post
Share on other sites
I know I can do that. There is alot of code that references the fields already so I need a fix.

This is what i had to use but don't understand why I would need to do it.
[code]if(isset($_POST['submit'])){
  // update query here

  $cpc = $_POST['CPC'];
foreach($_POST as $fields => $value){
  $field = ereg_replace("\+", " ", $fields);
    if($field <> 'CPC'){
    if($field <> 'submit'){
        if($field <> 'keyword'){
          if($field <> 'stype'){
          if($field <> 'field'){

          $sql = "UPDATE parcels SET
            `".$field."` = '".$value."' WHERE CPC = '$cpc'";

            //echo "$sql<br>";
            $res = mysql_query($sql) or die (mysql_error());
            }
          }
        }
      }
    }
  }

  echo "Data inserted into table<br>
        Click <a href=\"home.php?keyword=".$_POST['keyword']."&stype=".$_POST['stype']."&field=".$_POST['field']."&submit=submit\">HERE</a> to return to list or<br>
        Click <a href=home.php>HERE</a> to return to search page";
} else {
?>
<table width=600 align=center border=1>
  <form name=cpcform method=POST action="<?=$_SERVER['PHP_SELF']?>">
  <input type=hidden name=CPC value="<?=$_GET['cpc']?>">
  <input type=hidden name=keyword value="<?=$_GET['keyword']?>">
  <input type=hidden name=stype value="<?=$_GET['stype']?>">
  <input type=hidden name=field value="<?=$_GET['field']?>">
<?php
// start your count here for row offsets
$k=0;
// Query database for data
$sql = "SELECT * FROM parcels WHERE CPC='". $_GET['cpc']."'" ;
// run query
  $res = mysql_query($sql) or die (mysql_error());
//count how many fields in table
$num_fields = mysql_num_fields($res);
// Get data from query
$r = mysql_fetch_row($res);
// start loop
    while($k < $num_fields){
      // Fetch field names
      $meta = mysql_fetch_field($res, $k);
      // Substitute N/A if field is blank
      if(empty($r[$k])){
        $field = "N/A";
        } else {
        $field = $r[$k];
        }
      $fieldname = ereg_replace(" ", "+", $meta->name);
        // Start output rows

        echo "<tr>\n";
        echo "<td width=150><font color=#FF0000>".$meta->name."</font></td>\n";
        if($meta->name == 'CPC'){
        echo "<td width=200><b><input type=text name=\"".$fieldname."\" value=\"$field\" size=60 disabled></b></td>\n";
        } else {
        echo "<td width=200><input type=text name=\"".$fieldname."\" value=\"$field\" size=60></td>\n";
        }
        echo "</tr>\n";

$k++;
}
echo "<tr>
      <td colspan=2 align=center><input type=submit name=submit value=Submit></td>
      </tr>
      </form>
      </table>";
}
?>[/code]

Ray

Share this post


Link to post
Share on other sites
I'm quite lost in what you mean :/

Share this post


Link to post
Share on other sites
Since I am editing all the fields in the database I run a query to fetch all the field names and all the values, then I make the form name the actual name of the field. That way when the POST values come thru it should be fieldname=newvalue. But what is happening is the fieldnames that have spaces are coming thru the POST with an "_" instead of the space. So to work around it I replaced the space with a "+" in the form name, then when the form is submitted I replaced the "+" back to a space.

[code]<?php
if(isset($_POST['submit'])){
  // update query here

  $cpc = $_POST['CPC'];
foreach($_POST as $fields => $value){
// Replace the + with a space so the field name is correct
  $field = ereg_replace("\+", " ", $fields); 
    if($field <> 'CPC'){
    if($field <> 'submit'){
        if($field <> 'keyword'){
          if($field <> 'stype'){
          if($field <> 'field'){

          $sql = "UPDATE parcels SET
            `".$field."` = '".$value."' WHERE CPC = '$cpc'";

            //echo "$sql<br>";
            $res = mysql_query($sql) or die (mysql_error());
            }
          }
        }
      }
    }
  }

  echo "Data inserted into table<br>
        Click <a href=\"home.php?keyword=".$_POST['keyword']."&stype=".$_POST['stype']."&field=".$_POST['field']."&submit=submit\">HERE</a> to return to list or<br>
        Click <a href=home.php>HERE</a> to return to search page";
} else {
?>
<table width=600 align=center border=1>
  <form name=cpcform method=POST action="<?=$_SERVER['PHP_SELF']?>">
  <input type=hidden name=CPC value="<?=$_GET['cpc']?>">
  <input type=hidden name=keyword value="<?=$_GET['keyword']?>">
  <input type=hidden name=stype value="<?=$_GET['stype']?>">
  <input type=hidden name=field value="<?=$_GET['field']?>">
<?php
// start your count here for row offsets
$k=0;
// Query database for data
$sql = "SELECT * FROM parcels WHERE CPC='". $_GET['cpc']."'" ;
// run query
  $res = mysql_query($sql) or die (mysql_error());
//count how many fields in table
$num_fields = mysql_num_fields($res);
// Get data from query
$r = mysql_fetch_row($res);
// start loop
    while($k < $num_fields){
      // Fetch field names
      $meta = mysql_fetch_field($res, $k);
      // Substitute N/A if field is blank
      if(empty($r[$k])){
        $field = "N/A";
        } else {
        $field = $r[$k];
        }
// First add this in to put a plus in the form name, which is the field name, in place of the space
      $fieldname = ereg_replace(" ", "+", $meta->name);
        // Start output rows

        echo "<tr>\n";
        echo "<td width=150><font color=#FF0000>".$meta->name."</font></td>\n";
        if($meta->name == 'CPC'){
        echo "<td width=200><b><input type=text name=\"".$fieldname."\" value=\"$field\" size=60 disabled></b></td>\n";
        } else {
        echo "<td width=200><input type=text name=\"".$fieldname."\" value=\"$field\" size=60></td>\n";
        }
        echo "</tr>\n";

$k++;
}
echo "<tr>
      <td colspan=2 align=center><input type=submit name=submit value=Submit></td>
      </tr>
      </form>
      </table>";
}
?>[/code]

Ray

Share this post


Link to post
Share on other sites
oh, so have you not tried...

mysql_query("UPDATE parcels SET fiield1='".$_POST['field1]."',
field2='".$_POST[field2]."',
field3='".$_POST[field3]."'"); // use , after each field?

Share this post


Link to post
Share on other sites

×

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.