Jump to content


Photo

Editing MySQL data with PHP


  • Please log in to reply
7 replies to this topic

#1 Fearpig

Fearpig
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 11 August 2006 - 09:54 AM

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





"Whats wrong with the cat?" - Mrs Schrödinger

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 August 2006 - 10:02 AM

get rid on these
if(isset($submit))
{

add to post like this

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

}
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 Fearpig

Fearpig
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 11 August 2006 - 10:12 AM

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
"Whats wrong with the cat?" - Mrs Schrödinger

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 August 2006 - 10:16 AM

sorry dont change nothink 1 min reading code
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 August 2006 - 10:17 AM

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

  if($_GET["cmd"]=="edit" && $_POST["cmd"]=="edit")
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 August 2006 - 10:20 AM

//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);
        ?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 August 2006 - 10:22 AM

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

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 Fearpig

Fearpig
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 11 August 2006 - 10:34 AM

Hi Redarrow,
So which of those changes do I use.... could you post a complete script?

Thanks for all your help.
Tom
"Whats wrong with the cat?" - Mrs Schrödinger




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users