Jump to content

Editing MySQL data with PHP


Fearpig

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




Link to comment
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
Link to comment
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);
        ?>
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.