Jump to content

Another Question


mada

Recommended Posts

Same script as my previous post...

Now trying to add a delete function.

[code=edit.php]
<?php
  include('mysql.php');

//If cmd has not been initialized
if(!isset($cmd))
{
  //display all the peopleextensions
  $result = mysql_query("select * from peopleextensions order by ID");
 
  //run the while loop that grabs all the peopleextensions scripts
  while($r=mysql_fetch_array($result))
  {
      //grab the Name and the ID of the peopleextensions
      $Name=$r["Name"];//take out the Name
      $ID=$r["ID"];//take out the ID
   
//make the Name a link
      echo "<a href='edit.php?cmd=edit&ID=$ID'>$Name - Edit</a>";
      echo "<br />";
    }
}
?>
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
  if (!isset($_POST["submit"]))
  {
      $ID = $_GET["ID"];
      $sql = "SELECT * FROM peopleextensions 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"] ?>">
 
      Name:<INPUT TYPE="TEXT" NAME="Name" VALUE="<?php echo $myrow["Name"] ?>" SIZE=30><br />
      Area Code:<INPUT TYPE="TEXT" NAME="AreaCode" VALUE="<?php echo $myrow["AreaCode"] ?>" SIZE=30><br />
      Number:<INPUT TYPE="TEXT" NAME="Number" VALUE="<?php echo $myrow["Number"] ?>" SIZE=30><br />
      Role:<INPUT TYPE="TEXT" NAME="Role" VALUE="<?php echo $myrow["Role"] ?>" SIZE=30><br />
      Department:<INPUT TYPE="TEXT" NAME="Department" VALUE="<?php echo $myrow["Department"] ?>" SIZE=30><br />
      <input type="hidden" Name="cmd" value="edit">
 
      <input type="submit" Name="submit" value="Save" /><br />
      <input type="submit" value="Delete" Name="delete" />
 
      </form>
<? } ?>
<?
  if ($_POST["submit"])
  {
  $id = $_POST["ID"];
          $Name = $_POST["Name"];
  $AreaCode = $_POST["AreaCode"];
  $Number = $_POST["Number"];
          $Role = $_POST["Role"];
  $Department = $_POST["Department"];

  $sql = "UPDATE peopleextensions SET Name='$Name',AreaCode='$AreaCode',Number='$Number',Role='$Role',Department='$Department'

WHERE ID=$ID";
      //replace peopleextensions with your table Name above
      $result = mysql_query($sql);
}

  if ($_POST["delete"])
{
  $id = $_POST["ID"];
  mysql_query('DELETE FROM peopleextensions WHERE ID=$ID');
}


if($result)
{
echo "Update successful";
echo "Thank you! Information updated.";
echo "<br>";
echo "<a href='index.php'>Home</a>";
}

// use else to execute the code if $result was false

else
{
echo "Update failed, please try again";
echo "<br>";
echo "<a href='index.php'>Home</a>";
}
}



?>
[/code]

Returns the error : [quote=error]Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\httpd\html\helpdesk\AdamTestArea\phonebook\edit.php on line 31[/quote]

I know it's related to [code]      $result = mysql_query($sql);
}

  if ($_POST["delete"])
{
  $id = $_POST["ID"];
  mysql_query('DELETE FROM peopleextensions WHERE ID=$ID');
}[/code] but i'm not sure how to fix it as when I try the query in PHPMyAdmin it doesn't cause any problems. (I substitute $ID with a number)
Link to comment
Share on other sites

well first of all you call your $id variable with uppercase in your query:

$result = mysql_query($sql);
}

  if ($_POST["delete"])
{
  $id = $_POST["ID"];
  mysql_query('DELETE FROM peopleextensions WHERE ID=$ID');
}

It should be mysql_query('DELETE FROM peopleextensions WHERE ID='$id''); because you call the variable with lowercase. Also put it in '' like i did
Link to comment
Share on other sites

JJ,  + Jesisrose, your edits have made the script functional, however oddly enough the error message still appears.  ???


The script is currently as follows:-
[code=edit.php]<?php
  include('mysql.php');

//If cmd has not been initialized
if(!isset($cmd))
{
  //display all the peopleextensions
  $result = mysql_query("select * from peopleextensions order by ID");
 
  //run the while loop that grabs all the peopleextensions scripts
  while($r=mysql_fetch_array($result))
  {
      //grab the Name and the ID of the peopleextensions
      $Name=$r["Name"];//take out the Name
      $id=$r["ID"];//take out the ID
   
//make the Name a link
      echo "<a href='edit.php?cmd=edit&ID=$id'>$Name - Edit</a>";
      echo "<br />";
    }
}
?>
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
  if (!isset($_POST["submit"]))
  {
      $id = $_GET["ID"];
      $sql = "SELECT * FROM peopleextensions 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"] ?>">
 
      Name:<INPUT TYPE="TEXT" NAME="Name" VALUE="<?php echo $myrow["Name"] ?>" SIZE=30><br />
      Area Code:<INPUT TYPE="TEXT" NAME="AreaCode" VALUE="<?php echo $myrow["AreaCode"] ?>" SIZE=30><br />
      Number:<INPUT TYPE="TEXT" NAME="Number" VALUE="<?php echo $myrow["Number"] ?>" SIZE=30><br />
      Role:<INPUT TYPE="TEXT" NAME="Role" VALUE="<?php echo $myrow["Role"] ?>" SIZE=30><br />
      Department:<INPUT TYPE="TEXT" NAME="Department" VALUE="<?php echo $myrow["Department"] ?>" SIZE=30><br />
      <input type="hidden" Name="cmd" value="edit">
 
      <input type="submit" Name="submit" value="Save" /><br />
      <input type="submit" value="Delete" Name="delete" />
 
      </form>
<? } ?>
<?
  if ($_POST["submit"])
  {
  $id = $_POST["ID"];
          $Name = $_POST["Name"];
  $AreaCode = $_POST["AreaCode"];
  $Number = $_POST["Number"];
          $Role = $_POST["Role"];
  $Department = $_POST["Department"];

  $sql = "UPDATE peopleextensions SET Name='$Name',AreaCode='$AreaCode',Number='$Number',Role='$Role',Department='$Department'

WHERE ID=$id";
      //replace peopleextensions with your table Name above
      $result = mysql_query($sql);
}

  if ($_POST["delete"])
{
  $id = $_POST["ID"];
  mysql_query("DELETE FROM peopleextensions WHERE ID='$id'");
}


if($result)
{
echo "Update successful";
echo "Thank you! Information updated.";
echo "<br>";
echo "<a href='index.php'>Home</a>";
}

// use else to execute the code if $result was false

else
{
echo "Update failed, please try again";
echo "<br>";
echo "<a href='index.php'>Home</a>";
}
}



?>
[/code]

The error displayed is:-
[quote]
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\httpd\html\helpdesk\AdamTestArea\phonebook\edit.php on line 31
[/quote]

Lines 22-32 read:-
[code]
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
  if (!isset($_POST["submit"]))
  {
      $id = $_GET["ID"];
      $sql = "SELECT * FROM peopleextensions WHERE ID=$id";
      $result = mysql_query($sql);       
      $myrow = mysql_fetch_array($result);
      ?>
[/code]


[quote author=trochia link=topic=123151.msg508633#msg508633 date=1169221886]
Maybe rename the Subject " Another question regarding ??  "

:-)
[/quote]

If the system would allow me to edit the original topic title I would.
Link to comment
Share on other sites

[quote author=dgiberson link=topic=123151.msg508661#msg508661 date=1169224244]
Most likely there is an issue with the query, echo $sql and try to run it in Query Browser/php MyAdmin whatever you use and see if it returns a recordset or not.
[/quote]

It turned out I forgot the ' ' either side of the variable in the sql query.

Thanks for all of your help!
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.