Why MySQL won't update via php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brandon slakter
    New Member
    • Jan 2011
    • 9

    Why MySQL won't update via php?

    Everything is working on my website from it showing the details when i click modify from the database.. BUT when i try and change something, then click submit. BAMM. shows a windows cannot be displayed and wont do anything NOR update.. here is the final code i believe is having an issue while posting.


    Code:
    <h1>modify info</h1>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Name <input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
    Description <input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
     <br />
     <input type="hidden" name="id" value="<?php $_GET['id']; ?>" />
     <input type="submit" name="submit" value="Modify" />
     </form>
     <?php
    if (isset($_POST['submit'])) {
    	$u = "UPDATE people SET 'Name'='$_POST[inputName]', 'Description'='$_POST[inputDesc]' WHERE ID = $_POST[id]";
    	mysql_query($u) or die(mysql_error());
    	echo "User has been created";
    	header("Location: index.php");
    	}
    	?>
    Last edited by Niheel; Jan 18 '11, 10:19 AM. Reason: Don't beg "PLEASE HELP" and please use code tags to display code
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Code:
    $u = "UPDATE people SET 'Name'='$_POST[inputName]', 
        'Description'='$_POST[inputDesc]' WHERE ID = $_POST[id]";
    A few things here,
    First you don't have error_reporting switched on because this would report a syntax error.
    Directly inputting POST values from a web-site to a database should never be done this way. Have a look at SQL injection.
    You don't need quotes around field names.
    For PHP to parse array values such as $_POST in a string, curly braces are needed.

    Comment

    • brandon slakter
      New Member
      • Jan 2011
      • 9

      #3
      I pretty much got the code off youtube. Could you give me another option on how do make this update how things already are? Im still very new to all this. Thank you again for all your help

      Comment

      • nathj
        Recognized Expert Contributor
        • May 2007
        • 937

        #4
        Hi,

        Take a look at this article here on Bytes. It got me started and it is a good foundation for building a proper DAL.

        Once you are up and running with this post back with any questions you may have.
        Cheers
        Nathan

        PS Here is a good introduction

        Comment

        • brandon slakter
          New Member
          • Jan 2011
          • 9

          #5
          Thanks! Unfortunately I have spent 12 hours today on this and want to just finish it up since not much is left ... txs again

          Comment

          • kovik
            Recognized Expert Top Contributor
            • Jun 2007
            • 1044

            #6
            Did that code work? I'd imagine not since you are still using single quotes instead of backticks.

            Code:
            $u = "UPDATE `people` SET `Name`='$_POST[inputName]', `Description`='$_POST[inputDesc]' WHERE ID = $_POST[id]";

            Comment

            • code green
              Recognized Expert Top Contributor
              • Mar 2007
              • 1726

              #7
              I never thought that Brandon meant to use backticks instead of quotes.
              Will this work Kovik? I'm pretty sure he will need braces
              Code:
              $u = "UPDATE `people` SET `Name`='{$_POST[inputName]}', 
              `Description`='{$_POST[inputDesc]}' WHERE ID = {$_POST[id]}";
              Or am I getting confused with something else?

              Comment

              • kovik
                Recognized Expert Top Contributor
                • Jun 2007
                • 1044

                #8
                If you use the braces, you're supposed to use quotation marks on the key. Otherwise, they are optional.

                Code:
                $u = "UPDATE `people` SET `Name`='$_POST[inputName]', `Description`='$_POST[inputDesc]' WHERE ID = $_POST[id]";
                or

                Code:
                $u = "UPDATE `people` SET `Name`='{$_POST['inputName']}', `Description`='{$_POST['inputDesc']}' WHERE ID = {$_POST['id']}";

                Comment

                • brandon slakter
                  New Member
                  • Jan 2011
                  • 9

                  #9
                  Let me give you my whole modify.php code. it looks like when I view the webpage on localhost it doesnt display an error. but when i look elsewhere it says that line 13 has a undefined variable, and other stuff. here is the code below. I thought i had everything right?? let me know if you guys can help on this one.. thanks!


                  <?php
                  include "connection.php ";
                  if (!isset($_POST['submit'])) {
                  $q = "SELECT * FROM people WHERE ID = $_GET[id]";
                  $result = mysql_query($q) ;
                  $person = mysql_fetch_arr ay($result);

                  }

                  ?>
                  <h1>modify info</h1>
                  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                  Name<input type="text" name="inputName " value="<?php echo $person['Name']; ?>" /><br />
                  Description<inp ut type="text" name="inputDesc " value="<?php echo $person['Description']; ?>" />
                  <br />
                  <input type="hidden" name="id" value="<?php $_GET['id']; ?>" />
                  <input type="submit" name="submit" value="Modify" />
                  </form>
                  <?php
                  if (isset($_POST['submit'])) {
                  $u = "UPDATE `people` SET `Name`='$_POST[inputName]', `Description`=' $_POST[inputDesc]' WHERE ID = $_POST[id]";
                  mysql_query($u) or die(mysql_error ());
                  echo "User has been created";
                  header("Locatio n: index.php");
                  }
                  ?>

                  Comment

                  • kovik
                    Recognized Expert Top Contributor
                    • Jun 2007
                    • 1044

                    #10
                    If it says a variable is undefined on line 13, chances are a variable is undefined on line 13. Try echoing it to check.

                    Comment

                    • brandon slakter
                      New Member
                      • Jan 2011
                      • 9

                      #11
                      Name<input type="text" name="inputName " value="<?php echo $person['Name']; ?>" /><br />

                      thats line 13. it is echoed?

                      Comment

                      • kovik
                        Recognized Expert Top Contributor
                        • Jun 2007
                        • 1044

                        #12
                        And is there a value? Try print_r($person ). It's possible that your query returned no results, which you currently aren't checking for.

                        Comment

                        • brandon slakter
                          New Member
                          • Jan 2011
                          • 9

                          #13
                          Can I email u my complete script.? Im so close to finishing this :(

                          Comment

                          • kovik
                            Recognized Expert Top Contributor
                            • Jun 2007
                            • 1044

                            #14
                            Programming is my job. As such, I don't work for free. Advice is free. Labor is not. Now start debugging.

                            Comment

                            • brandon slakter
                              New Member
                              • Jan 2011
                              • 9

                              #15
                              yeah unfortunately I wouldn't even know what to do next as I took this code from someone that already completed it.. and i'm way to new.. I've tried the things you shown above in the examples above yet still does not work..

                              Comment

                              Working...