Jump to content

Editor script erroring out


jeicrash

Recommended Posts

I am trying to create a simple script to edit mysql rows in php. I copied and pasted the code from spoono tutorial

www.spoono.com/php/tutorials/tutorial.php?id=23

 

Edited all the values to fit my database and its not working. I am very new to this part of php, so far the add and remove functions are working perfectly.

 

Heres my code. without the db connection info.

 


//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the tripreport
   $result = mysql_query("select * from tripreport order by id"); 
   
   //run the while loop that grabs all the tripreport scripts
   while($r = mysql_fetch_array($result))
   { 
      //grab the customername and the ID of the tripreport
      $customername=$r["customername"];//take out the customername
      $id=$r["id"];//take out the id
     
 //make the customername a link
      echo "<a href='edit.php?cmd=edit&id=$id'>$customername - Edit</a>";
      echo "<br>";
    }
}
?>
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {

      $id = $_GET["id"];
      $sql = "SELECT * FROM tripreport WHERE id=$id";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
  
      <form action="edit.php" method="post">
      <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
   
      customername:<INPUT TYPE="TEXT" NAME="customername" VALUE="<?php echo $myrow["customername"] ?>" SIZE=30><br>
      
   
      <input type="hidden" name="cmd" value="edit">
   
      <input type="submit" name="submit" value="submit">
   
      </form>
   
<? } ?>
<?
   if ($_POST["$submit"])
   {
      $customername = $_POST["customername"];

  
  $sql = "UPDATE tripreport SET customername='$customername' WHERE id=$id";
      //replace tripreport with your table name above
      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
}
}
?>

 

The customer name is turned into a link, but on the form section i am getting the error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /sandbox/edit.php on line 37

 

line 37 is

$myrow = mysql_fetch_array($result);

 

Any help is appreciated, i'll try to get back and check as soon as i can.

Link to comment
Share on other sites

Ok I did that. Now I am getting

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

Heres the top portion of my script, Perhaps I have the wrong "connect" options. The script that adds and deletes records works fine and it has a different connect string.

 

<? 
mysql_connect("MYSQLHOST","username","dbpass"); 
mysql_select_db("databasename"); 
if(!isset($cmd)) 
{
   //display all the tripreport
   $result = mysql_query("select * from tripreport order by id"); 
   
   //run the while loop that grabs all the tripreport scripts
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the customername and the ID of the tripreport
      $customername=$r["customername"];//take out the customername
      $id=$r["id"];//take out the id
     
 //make the customername a link
      echo "<a href='edit.php?cmd=edit&id=$id'>$customername - Edit</a>";
      echo "<br>";
    }
}
?>

Link to comment
Share on other sites

Ok, I finally figured it out. Had some typos "CAPS" do matter. lol, i feel so stupid.

 

Thanks for the help it did help me greatly track down my problem once I actually looked at the code line for line.

 

Here is the Finished Edit.php code in case anyone wants it.

 

<HTML>
<BODY>
<STYLE TYPE="text/css">
<?php
    // Connect to the database server
    $dbcnx = @mysql_connect("MYSQLHOST", "username", "dbpass");
    if (!$dbcnx) {
      echo( "<P>Unable to connect to the " .
            "database server at this time.</P>" );
      exit();
    }

    // Select the client database
    if (! @mysql_select_db("databasename") ) {
      echo( "<P>Unable to locate the client " .
            "database at this time.</P>" );
      exit();
    }

//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the tripreport
   $result = mysql_query("select * from tripreport order by ID"); 
   
   //run the while loop that grabs all the tripreport scripts
   while($r = mysql_fetch_array($result)) { 
      $ID=$r["ID"];
      $customername=$r["customername"];

 //make the customername a link
         echo "<A HREF='$PHP_SELF?cmd=edit&ID=$r[iD]'>$customername - Edit </a>";
         echo "<br>";
    }
} ?>
<?php
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {

      $ID = $_GET["ID"];
      $sql = "SELECT * FROM tripreport WHERE ID=$ID";
          $result = mysql_query($sql) or die(mysql_error());        
      $myrow = mysql_fetch_array($result);
      ?>
  
      <form action="edit.php" method="post">
      <input type=hidden name="ID" value="<?php echo $myrow["ID"] ?>">
   
      customername:<INPUT TYPE="TEXT" NAME="customername" VALUE="<?php echo $myrow["customername"] ?>" SIZE=30><br>
      
   
      <input type="hidden" name="cmd" value="edit">
   
      <input type="submit" name="submit" value="submit">
   
      </form>
   
  
<?php } ?>
<?php
   if ($_POST["$submit"])
   {
      $customername = $_POST["customername"];

  
  $sql = "UPDATE tripreport SET customername='$customername' WHERE ID=$ID";
      //replace tripreport with your table name above
      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
}
}
?>

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.