LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php page displaying text that is supposed to be part of php code (https://www.linuxquestions.org/questions/programming-9/php-page-displaying-text-that-is-supposed-to-be-part-of-php-code-659331/)

DragonM15 07-30-2008 03:11 PM

php page displaying text that is supposed to be part of php code
 
Hello all,

I am working on php and putting html and php into the same page. My code is as follows:
Code:

<?
//connect to mysql
mysql_connect("localhost","user","password");

//select which database you want to edit
mysql_select_db("databasename");

//If cmd has not been initialized
if(!isset($cmd))
{
  //display all the items
  $result = mysql_query("select * from items order by type");

  //run the while loop that grabs all the items scripts
  while($r=mysql_fetch_array($result))
  {
      //grab the columns from items
      $rbuilding=$r["building"];//take out the building
      $rroom=$r["room"];//take out the room
      $rtype=$r["type"];
      $rmodel=$r["model"];
      $rserial=$r["serial"];
      $rspecial=$r["special"];
      $rcomments=$r["comments"];
        //make the title a link
        echo "<a href='edit.php?cmd=edit&building=$rbuilding&room=$rroom&type=$rtype&model=$rmodel&serial=$rserial&special=$rspecial&comments=$rcomments'>Edit</a>";
//        echo "<br \>";
          }
}

?>
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
  if (!isset($_POST["submit"]))
  {
      $building = $_GET["rbuilding"];
      $room = $_GET["rroom"];
      $type = $_GET["rtype"];
      $make = $_GET["rmake"];
      $model = $_GET["rmodel"];
      $serial = $_GET["rserial"];
      $special = $_GET["rspecial"];
      $comments = $_GET["rcomments"];

      $sql = "SELECT * FROM items WHERE building='$building' && room='$room' && type='$type' && make='$make' && model='$model' && serial='$serial' && special='$special' && comments='$comments'";
      $result = mysql_query($sql);
      $myrow = mysql_fetch_array($result);
       
 ?>

    <form action="edit.php" method="post">

      Building: <INPUT TYPE="TEXT" NAME="building" VALUE="<? $myrow["building"] ?>" SIZE=30 /><br />
      Room: <INPUT TYPE="TEXT" NAME="room" VALUE="<? echo $myrow["room"] ?>" SIZE=30 /><br />
      Type: <INPUT TYPE="TEXT" NAME="type" VALUE="<? echo $myrow["type"] ?>" SIZE=30 /><br />
      Make: <INPUT TYPE="TEXT" NAME="make" VALUE="<? echo $myrow["make"] ?>" SIZE=30 /><br />
      Model: <INPUT TYPE="TEXT" NAME="model" VALUE="<? echo $myrow["model"] ?>" SIZE=30 /><br />
      Serial: <INPUT TYPE="TEXT" NAME="serial" VALUE="<? echo $myrow["serial"] ?>" SIZE=30 /><br />
      Special: <INPUT TYPE="TEXT" NAME="special" VALUE="<? echo $myrow["special"] ?>" SIZE=30 /><br />
      Comments: <INPUT TYPE="TEXT" NAME="comments" VALUE="<? echo $myrow["comments"] ?>" SIZE=30 /><br />

      <input type="submit" name="submit" value="submit">

      </form>

<? } ?>
<?
        if ($_POST["$submit"])
        {
        $building = $_POST["building"];
        $room = $_POST["room"];
        $type = $_POST["type"];
        $make = $_POST["make"];
        $model = $_POST["model"];
        $serial = $_POST["serial"];
        $special = $_POST["special"];
        $comments = $_POST["comments"];

        $sql = "UPDATE items SET building='$building',room='$room',type='$type',make='$make',model='$model',serial='$serial',special='$special',comments='$comments'";
        $result = mysql_query($sql);
        echo "Thank you! Information updated.";
        }
}
?>

However when I run this code I get my text boxes, but they are filled in with
Code:

<? echo $myrow[
, and after each text box it prints
Code:

" SIZE=30 />
, and before my text boxes
Code:

Edit"; // echo "
"; } } ?>

I was hoping that you would be able to see my mistake. I have been almost to the point of banging my head against the desk.

Thanks as always for your help.

DragonM15

paulsm4 07-30-2008 03:26 PM

Hi -

You might try something like this:
Code:

  // Original:
  Building: <INPUT TYPE="TEXT" NAME="building" VALUE="<? echo $myrow["building"] ?>" SIZE=30 /><br />
 
  // New:
  Building: <input type="TEXT" name="building" value=<? echo "\"$myrow["building"]\""; ?> size=30 /><br />

In other words, control the quotes from your PHP, instead of your XHTML.

'Hope that helps .. PSM

DragonM15 07-30-2008 03:45 PM

That made the amount of text inside the text box decrease to
Code:

<?
instead of
Code:

<? echo $myrow[
Apache error_log shows no errors either

Thanks,
DragonM15

jlinkels 07-30-2008 06:27 PM

I think you named your file file.html. It should by file.php

I was able to reproduce your error in file.html, but the form shows fine in file.php

jlinkels

DragonM15 07-30-2008 07:42 PM

Its in a php file... edit.php
I tried it on 2 computers (one windows with php and one slackware 12.1 with php) and both give the same problem.

DragonM15

DragonM15 07-30-2008 07:50 PM

As a side-note I am running apache-2.2.9 on a Slackware 12.1 box. All my other PHP files work perfect, it is just this one that isn't working properly.

DragonM15

jlinkels 07-30-2008 08:36 PM

I tried it on my own server after editing a few statements to get it to work without that particular database. It works all right.

See if it runs on your server, and if so, undo all my changes until it goes wrong.

What is the contents of the array returned by the database? Are there some stray quotes in?

The file name I used is build.php - should have been edit.php of course but typing was faster than thinking

PHP Code:

<?
//connect to mysql
//mysql_connect("localhost","user","password");

//select which database you want to edit
//mysql_select_db("databasename");

//If cmd has not been initialized
if(!isset($cmd))
{
   
//display all the items
   //$result = mysql_query("select * from items order by type");

   //run the while loop that grabs all the items scripts
   //while($r=mysql_fetch_array($result))
$r=2;
   while(
$r 0)
    
$r--;
   {
      
//grab the columns from items
      
$rbuilding=$r["building"];//take out the building
      
$rroom=$r["room"];//take out the room
      
$rtype=$r["type"];
      
$rmodel=$r["model"];
      
$rserial=$r["serial"];
      
$rspecial=$r["special"];
      
$rcomments=$r["comments"];
     
//make the title a link
     
echo "<a href='edit.php?cmd=edit&building=$rbuilding&room=$rroom&type=$rtype&model=$rmodel&serial=$rserial&special=$rspecial&comments=$rcomments'>Edit</a>";
//     echo "<br \>";
      
}
}

?>
<?
//if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
if(1)
{
   if (!isset(
$_POST["submit"]))
   {
      
$building $_GET["rbuilding"];
      
$room $_GET["rroom"];
      
$type $_GET["rtype"];
      
$make $_GET["rmake"];
      
$model $_GET["rmodel"];
      
$serial $_GET["rserial"];
      
$special $_GET["rspecial"];
      
$comments $_GET["rcomments"];

      
$sql "SELECT * FROM items WHERE building='$building' && room='$room' && type='$type' && make='$make' && model='$model' && serial='$serial' && special='$special' && comments='$comments'";
      
//$result = mysql_query($sql);
      //$myrow = mysql_fetch_array($result);
    
 
?>

     <form action="edit.php" method="post">

      Building: <INPUT TYPE="TEXT" NAME="building" VALUE="<? $myrow["building"?>" SIZE=30 /><br />
      Room: <INPUT TYPE="TEXT" NAME="room" VALUE="<? echo $myrow["room"?>" SIZE=30 /><br />
      Type: <INPUT TYPE="TEXT" NAME="type" VALUE="<? echo $myrow["type"?>" SIZE=30 /><br />
      Make: <INPUT TYPE="TEXT" NAME="make" VALUE="<? echo $myrow["make"?>" SIZE=30 /><br />
      Model: <INPUT TYPE="TEXT" NAME="model" VALUE="<? echo $myrow["model"?>" SIZE=30 /><br />
      Serial: <INPUT TYPE="TEXT" NAME="serial" VALUE="<? echo $myrow["serial"?>" SIZE=30 /><br />
      Special: <INPUT TYPE="TEXT" NAME="special" VALUE="<? echo $myrow["special"?>" SIZE=30 /><br />
      Comments: <INPUT TYPE="TEXT" NAME="comments" VALUE="<? echo $myrow["comments"?>" SIZE=30 /><br />

      <input type="submit" name="submit" value="submit">

      </form>

<? ?>
<?
    
if ($_POST["$submit"])
    {
    
$building $_POST["building"];
    
$room $_POST["room"];
    
$type $_POST["type"];
    
$make $_POST["make"];
    
$model $_POST["model"];
    
$serial $_POST["serial"];
    
$special $_POST["special"];
    
$comments $_POST["comments"];

    
$sql "UPDATE items SET building='$building',room='$room',type='$type',make='$make',model='$model',serial='$serial',special='$special',comments='$comments'"
    
//$result = mysql_query($sql);
    
echo "Thank you! Information updated.";
    }
}
?>

I am running Apache 1.3, PHP 4.3

jlinkels

DragonM15 07-31-2008 11:00 AM

jlinkels, I put your exact code into a new php file titled edit1.php and I also modified all statements involving edit.php to edit1.php. However that actually made the page look worse.

The error looks like:
http://i59.photobucket.com/albums/g2...pediterror.jpg

As always, the apache error log shows no errors.

DragonM15

jlinkels 07-31-2008 02:27 PM

It is not worse, on the contrary it provides more information. :)

I created exactly the same screen output as you have by setting short_open_tag = Off in /etc/php4/apache/php.ini. It starts exactly at "0) $r--; ...." as yours does. You have to restart apache after changing.

Watch out, there might be more php.ini files around on your system.

I hope this is your problem, although I am amazed if it happens on two different systems, and you say other scripts do work. Do they use the short open tags as well, or the long <?php tag?

In PHP 2 '>' was allowed as closing tag, after that it became depreciated and unusable. I assume you don't run PHP 2, do you?

jlinkels

DragonM15 07-31-2008 04:58 PM

Well... That fixed the problem! I feel like an idiot right about now, but the problem is fixed. Ill bet that fixes one of my other problems with my delete php. And for the record, in Slackware the php.ini is in /etc/httpd/php.ini for anyone having the same problem I was.

No I am not using php2 I am using php-5.2.6 (current version).

And also, the pages that were in the making that I was also having problems with had <? ?>'s and were having problems. All the ones that worked were <?php 's.

Thank you for your help in solving my problem!

DragonM15

P.S. That only took 2 days of staring and pulling my hair out....


All times are GMT -5. The time now is 03:56 AM.