need help with script (update databse)

Discussion in 'PHP' started by saturn100, Mar 9, 2012.

  1. #1
    Hi
    OK

    I have a script that updates a database from a HTML form
    Its work on one level if you say the row in the form (where ID = '1') but the variable doesnt work (where ID ='$id')

    The way it is meant to work is you go in the index and imput a number eg 2
    This brings up a table which is populated from data from the database
    But when you go edit it and update it doesnt work

    (it says it does but it doesnt)

    OK the code is three parts
    index which works:

    <html>
    <head>
    </head>
    
    <body>
    <form action="update.php" method="get">
    
    ID: <input type="text" name="id" />
    <input type="submit" />
    </form>
    
    </body>
    </html>
    HTML:
    Then the update script which is PHP


    <?php
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name=""; // Database name 
    $tbl_name=""; // Table name
    $id=$_GET["id"];
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // get value of id that sent from address bar
    
    
    
    // Retrieve data from database
    $sql="SELECT * FROM $tbl_name WHERE ID='$id'";
    $result=mysql_query($sql);
    
    $rows=mysql_fetch_array($result);
    ?>
    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <form name="form1" method="post" action="update_ac.php">
    <td>
    <table width="100%" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td>&nbsp;</td>
    <td colspan="3"><strong>Update data in mysql</strong> </td>
    </tr>
    <tr>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    </tr>
    <tr>
    <td align="center">&nbsp;</td>
    <td align="center"><strong>field1</strong></td>
    <td align="center"><strong>field2</strong></td>
    <td align="center"><strong>field3</strong></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td align="center"><input name="Name" type="text" id="Name" value="<? echo $rows['Name']; ?>"></td>
    <td align="center"><input name="Adress" type="text" id="Adress" value="<? echo $rows['Adress']; ?>" size="15"></td>
    <td><input name="Adress2" type="text" id="Adress2" value="<? echo $rows['Adress2']; ?>" size="15"></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><input name="id"  type="hidden" id="id" value="<? echo $rows['ID']; ?>"></td>
    <td align="center"><input type="submit" name="Submit" value="Submit"></td>
    <td>&nbsp;</td>
    </tr>
    </table>
    </td>
    </form>
    </tr>
    </table>
    
    <?
    
    // close connection
    mysql_close();
    
    ?>
    PHP:

    Then we have the code that is meant to update the databse which is where the problem is

    <?php
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name=""; // Database name 
    $tbl_name=""; // Table name
    
    
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // update data in mysql database
    $sql="UPDATE $tbl_name SET Name='$Name', Adress='$Adress', Adress2='$Adress2' WHERE ID='$id'";
    $result=mysql_query($sql);
    
    // if successfully updated.
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='list_records.php'>View result</a>";
    }
    
    else {
    echo "ERROR";
    }
    
    ?>
    PHP:
    I believe the problem is with line 9 here

    $sql="UPDATE $tbl_name SET Name='$Name', Adress='$Adress', Adress2='$Adress2' WHERE ID='$id'";

    It doesnt seem to know which row to edit

    Can anyone help I up til 2am last night trying to figgure it out
     
    saturn100, Mar 9, 2012 IP
  2. saturn100

    saturn100 Well-Known Member

    Messages:
    465
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    111
    #2
    hey never mind I fixed it
     
    saturn100, Mar 9, 2012 IP
  3. minhazikram

    minhazikram Greenhorn

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    may be you should use '{$id}' in place of '$id';
     
    minhazikram, Mar 9, 2012 IP