Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Php issue - updating table in mysql

    Hi All, simply trying to make a update in content for editing a user information: Here is my code:

    I seem to believe it is not working because i havent applied it to a variable in my update_ac.php

    So here goes:

    EDIT.PHP

    Code:
    <?php
    // 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");
    
    
    echo("Logged In As: $UserName");
    echo "<br />";
    echo("We are editing Data for ID: $adminid");
    echo "<br />";
    echo "<a href=test.php>Go back to panel</a>";
    
    $id=$_GET['id'];
    // Retrieve data from database 
    $sql="SELECT * FROM admin WHERE id='$id'";
    $result=mysql_query($sql) or die(mysql_error());
    
    $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>Name</strong></td>
    <td align="center"><strong>Main Content</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="mainContent" type="text" id="mainContent" value="<? echo $rows['mainContent']; ?>" 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>
    
    <?
    mysql_close();
     ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Edit Page</title>
    </head>
    
    <body>
    <h2>Edit Page (<?php echo ("$adminid"); ?>)</h2>
    </body>
    </html>
    UPDATE_AC.PHP

    Code:
    <?php
    // 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 admin SET name='$name', mainContent='$mainContent' WHERE id='$id'";
    $result=mysql_query($sql) or die(mysql_error());;
    
    // if successfully updated. 
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='test.php'>Back to panel</a>";
    }
    
    else {
    echo "ERROR";
    }
    
    ?>

    I must state that I am not getting a error, therefore must be something to do with update_ac.php.

    Everything works successfull but the table does not upload: I seem to believe it could be because i havent assigned it to a _POST variable for each form field.

  • #2
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by jonny1990 View Post
    Hi All, simply trying to make a update in content for editing a user information: Here is my code:

    I seem to believe it is not working because i havent applied it to a variable in my update_ac.php

    So here goes:

    EDIT.PHP

    Code:
    <?php
    // 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");
    
    
    echo("Logged In As: $UserName");
    echo "<br />";
    echo("We are editing Data for ID: $adminid");
    echo "<br />";
    echo "<a href=test.php>Go back to panel</a>";
    
    $id=$_GET['id'];
    // Retrieve data from database 
    $sql="SELECT * FROM admin WHERE id='$id'";
    $result=mysql_query($sql) or die(mysql_error());
    
    $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>Name</strong></td>
    <td align="center"><strong>Main Content</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="mainContent" type="text" id="mainContent" value="<? echo $rows['mainContent']; ?>" 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>
    
    <?
    mysql_close();
     ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Edit Page</title>
    </head>
    
    <body>
    <h2>Edit Page (<?php echo ("$adminid"); ?>)</h2>
    </body>
    </html>
    UPDATE_AC.PHP

    Code:
    <?php
    // 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 admin SET name='$name', mainContent='$mainContent' WHERE id='$id'";
    $result=mysql_query($sql) or die(mysql_error());;
    
    // if successfully updated. 
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='test.php'>Back to panel</a>";
    }
    
    else {
    echo "ERROR";
    }
    
    ?>

    I must state that I am not getting a error, therefore must be something to do with update_ac.php.

    Everything works successfull but the table does not upload: I seem to believe it could be because i havent assigned it to a _POST variable for each form field.
    You will not get any errors as there are none.

    You have not caught what is being sent from the form.

    You will needed to catch the post variables and then input into the database.

    Another thing is you are not making variables that are being input into the database safe.

    You need to mysql escape them, also i would advice trim and htmlentities as well as making sure that the id is infact and int

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can anyone else give me a hand still stuck on this issue!

    Here i have entered teh _POST so the forms get sent to a variable, but still no luck!

    update_ac.php

    PHP Code:

    <?php
    $host
    ="localhost"// Host name 
    $username="jonathon_admin"// Mysql username 
    $password="hello123"// Mysql password 
    $db_name="jonathon_admin1"// Database name 
    $tbl_name="admin"// 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 

    $firstName $_POST["name"];
    $mainText $_POST["mainContent"];

    $sql="UPDATE admin SET name='$firstName', mainContent='$mainText' WHERE id='$id'";
    $result=mysql_query($sql) or die(mysql_error());;

    // if successfully updated. 
    if($result){
    echo 
    "Successful";
    echo 
    "<BR>";
    echo 
    "<a href='test.php'>Back to panel</a>";
    }

    else {
    echo 
    "ERROR";
    }

    ?>

  • #4
    New Coder
    Join Date
    Apr 2011
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devinmaking View Post
    You will not get any errors as there are none.

    You have not caught what is being sent from the form.

    You will needed to catch the post variables and then input into the database.

    Another thing is you are not making variables that are being input into the database safe.

    You need to mysql escape them, also i would advice trim and htmlentities as well as making sure that the id is infact and int
    Thanks, but this is not helping me learn, i understand the first two points, atleast if i am trying to do a step by step then i will understand, in regards to the mysql escape, i have not covered this therefore will need to take this into account when i deal with this...

  • #5
    New Coder
    Join Date
    Apr 2011
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Unfortunately i still havent managed to resolve this issue, anyone have any pointers?

  • #6
    New Coder
    Join Date
    Apr 2011
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have found a result guys, but not to complete one, when i change this line of code:

    $sql="UPDATE admin SET name='$firstName', mainContent='$mainText' WHERE id='1'";

    changing the ID to a fix ID it works fine and updates. So this must be a variable issue?

  • #7
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    Post all the code you currently have

  • #8
    New Coder
    Join Date
    Apr 2011
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    obviously i have loads more but nothing is linked to the edit.php apart from probs the SESSION

    These are the only two main focus pages, everything else is not related to this

  • #9
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    Code:
    $sql="UPDATE admin SET name='$firstName', mainContent='$mainText' WHERE id='$id'";
    You have not defined the variable $id anywhere. (Well i did not see it.)

  • #10
    New Coder
    Join Date
    Apr 2011
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yep it has been defined

  • #11
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    Quote Originally Posted by jonny1990 View Post
    yep it has been defined
    Where? if it is in a separate php file, then you will need to use include

  • #12
    New Coder
    Join Date
    Apr 2011
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <?php
    // 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");


    echo("Logged In As: $UserName");
    echo "<br />";
    echo("We are editing Data for ID: $adminid");
    echo "<br />";
    echo "<a href=test.php>Go back to panel</a>";

    $id=$_GET['id'];

  • #13
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    Yes, and I think you will find, that it is NOT in update_ac.php, so it cannot be used........

  • #14
    New Coder
    Join Date
    Apr 2011
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, but i re-declared it in my updated version, i will take another look, should this work?

  • #15
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    If you had a hidden field in your form,
    Code:
    <input type="hidden" name="ID" value="$id">
    then in update_ac.php

    you can use

    Code:
    $id = $_POST['ID'];


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •