Jump to content

Archived

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

Fearpig

Editing MySQL data with PHP

Recommended Posts

Hi Guys,
Can anyone help me? I've been struggling through various tutorials and I'm stuck on editing MySQL data with PHP.

Can someone take a looka t the code below and suggest where I'm going wrong?  :)




<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","password");
mysql_select_db("Telephonelist");

//If cmd has not been initialized
if(!isset($cmd))
{
  //display all the people
  $result = mysql_query("SELECT * FROM tbl_telephonenumbers ORDER BY ID");
 
  //run the while loop that grabs all the news scripts
  while($r=mysql_fetch_array($result))
  {
      //grab the title and the ID of the news
      $First=$r["First_Name"];//take out the title
  $Last=$r["Last_Name"];//take out the title
      $ID=$r["ID"];//take out the id
   
//make the title a link
      echo "<a href='Edit_SpecificPerson.php?cmd=edit&ID=$ID'>$First $Last</a>";
      echo "<br>";
    }
}


//If cmd has been initialized
if(isset($cmd))
{
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
  if (!isset($_POST["submit"]))
  {
      $id = $_GET["ID"];
      $sql = "SELECT * FROM tbl_telephonenumbers WHERE ID=$ID";
      $result = mysql_query($sql);       
      $myrow = mysql_fetch_array($result);
      ?>
 
      <form action="Edit_SpecificPerson.php" method="post">
      <input type=hidden name="id" value="<?php echo $myrow["ID"] ?>">
 
      First Name:<INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["First_Name"] ?>" SIZE=30><br>
      Last Name:<INPUT TYPE="TEXT" NAME="message" VALUE="<?php echo $myrow["Last_Name"] ?>" SIZE=30><br>
      Role:<INPUT TYPE="TEXT" NAME="who" VALUE="<?php echo $myrow["Role"] ?>" SIZE=30><br>
 
        <input type="hidden" name="cmd" value="edit">
 
      <input type="submit" name="submit" value="submit">
 
          </form>
 
<? }
}

if(isset($submit))
{
  if ($_POST["$submit"])
  {
      $First = $_POST["First_Name"];
  $Last = $_POST["Last_Name"];
  $Role = $_POST["Role"];
 
  $sql = "UPDATE tbl_telephonenumbers SET First_Name='$First',Last_Name='$Last',Role='$Role' WHERE ID=$ID";
      //replace news with your table name above
      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
}
}
}

?>


The first two pass throughs it works OK.
- On the first pass it list all the names as a link.
- On the second pass it populate text boxes with the details you selected (by which name you clicked on)
- On the third pass it fails. This is when you click submit and the database should be updated with any changes made into the text boxes.

This is the error page that comes up:

Notice: Undefined index: cmd in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 31

Notice: Undefined index: First_Name in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 61

Notice: Undefined index: Last_Name in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 62

Notice: Undefined index: Role in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 63

Notice: Undefined variable: ID in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 65
Thank you! Information updated.


- the information is not updated and any help will be REALLY appreciated as I've been hacking at this for days now and it all seems a bit of a blur!

Cheers Tom




Share this post


Link to post
Share on other sites
get rid on these
if(isset($submit))
{

add to post like this

if(isset($_POST['submit'])){

}

Share this post


Link to post
Share on other sites
Hi Redarrow,
Do you mean change just the end section to:


if(isset($_POST['submit'])
{
      $First = $_POST["First_Name"];
  $Last = $_POST["Last_Name"];
  $Role = $_POST["Role"];
 
  $sql = "UPDATE tbl_telephonenumbers SET First_Name='$First',Last_Name='$Last',Role='$Role' WHERE ID=$ID";
      //replace news with your table name above
      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
}
}

If so it now won't run up at all and has the error:

Parse error: parse error, unexpected T_VARIABLE in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 59

Share this post


Link to post
Share on other sites
  if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
to

  if($_GET["cmd"]=="edit" && $_POST["cmd"]=="edit")

Share this post


Link to post
Share on other sites
//If cmd has been initialized
if(isset($cmd))
{
  if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
  {
      if (!isset($_POST["submit"]))<<<<<< ! that be there ?
      {
        $id = $_GET["ID"];
        $sql = "SELECT * FROM tbl_telephonenumbers WHERE ID=$ID";
        $result = mysql_query($sql);       
        $myrow = mysql_fetch_array($result);
        ?>

Share this post


Link to post
Share on other sites
you set 

$id=$_GET['ID'];

  $sql = "SELECT * FROM tbl_telephonenumbers WHERE ID=$ID";
to

  $sql = "SELECT * FROM tbl_telephonenumbers WHERE id=$id";

get me you made the letters id small haha

Share this post


Link to post
Share on other sites
Hi Redarrow,
So which of those changes do I use.... could you post a complete script?

Thanks for all your help.
Tom

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.