Jump to content


Photo

space in input name


  • Please log in to reply
6 replies to this topic

#1 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 28 September 2006 - 12:56 PM

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
<?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>";
}
?>

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

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

Thanks

Ray

#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 28 September 2006 - 01:08 PM

Anyone???

#3 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 28 September 2006 - 01:13 PM

Try renaming the field to "Old_Lot", will work.

#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 28 September 2006 - 01:22 PM

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.
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>";
}
?>

Ray

#5 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 28 September 2006 - 01:26 PM

I'm quite lost in what you mean :/

#6 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 28 September 2006 - 02:25 PM

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.

<?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>";
}
?>

Ray

#7 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 28 September 2006 - 02:33 PM

oh, so have you not tried...

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users