Updating MySQL table with PHP

Discussion in 'PHP' started by yoyo99, Apr 1, 2009.

  1. #1
    Hey, this is probably simple, I just can't figure it out. I have a table that is being populated from a database. However in the table there are some input boxes for where I want the user to input data which then I want to update my record in the database.

    So here is an example:
    In the table I have -
    Name: Bob
    Address: 55 Sample Rd
    Phone: <user input>
    <submit button>

    The "Name" and "Address" fields are getting their info from the database. Upon the user entering in the info for the "Phone" field and hitting submit, I would like my database to update the record and put in the phone number for the field.

    Here's what I got so far, but it's not updating my record:
    
    <?
    $sql = "SELECT * FROM myTable WHERE username='".$_SESSION['username']."'";
    $result = mysql_query($sql) or die(mysql_error());
    
    if (isset($_POST['submit'])){
    	
    $lName = $_POST['lName'];
    $fName =  $_POST['fName'];
    $clientEmail = $_POST['clientEmail'];
    
    $ins = "UPDATE myTable SET lname='{$lname}', fName='{$fName}', clientEmail='{$clientEmail}' WHERE id=".$row['id'];
    	
    $query = mysql_query($ins, $connection) or die(mysql_error());
    if ($query){
    	echo "Listings Submitted Successfully";
    }else 
     	echo mysql_error();
    	
    }             // end if isset
    ?>
    <body>
    <form method="post" action="myPage.php">
    <table cellpadding="3" width="600px" border="1">
    <tr>
     <td nowrap>Address</td>
     <td nowrap>City</td>
     <td nowrap>Last Name</td>
     <td nowrap>First Name</td>
     <td nowrap>Email</td>
    </tr>
    
    <? 
    while ($row = mysql_fetch_assoc($result)){
    $city1 = $row['city'];
    $city = str_replace("_", " ", $city1);
    	
    echo "<tr>
    	<td nowrap>".$row['address']."</td>
    	<td nowrap>".$city."</td>
    	<td nowrap>".'<input type="text" name="lName" value="'.$lName.'">'."</td>
    	<td nowrap>".'<input type="text" name="fName" value="'.$fName.'">'."</td>
    	<td nowrap>".'<input type="text" name="clientEmail" value="'.$clientEmail.'">'."</td>
    		  </tr>";
    		 
    }
    ?>
    <tr>
      <td colspan="8" style="text-align:center"><input type="submit" value="Submit" /></td>
    </tr>
    </table>
    </form>
    </body>
    
    Code (markup):
    I want the user to fill in the "lName", "fName", and "clientEmail" fields. Any and all help or ideas would be greatly appreciated! THanks :)
     
    yoyo99, Apr 1, 2009 IP
  2. kusal

    kusal Peon

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try giving a name to the submit button

    <input type="submit" name="submit" value="Submit" />
     
    kusal, Apr 1, 2009 IP
  3. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This should help a bit haha.
     
    NatalicWolf, Apr 1, 2009 IP
  4. yoyo99

    yoyo99 Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    LoL can't believe i missed that. Oh well, its late. However, that unfortunately didn't fix my problem. I'm not sure if its because I am receiving multiple records from the 1st query? Any ideas?
     
    yoyo99, Apr 2, 2009 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    You need something to ID each row - as I can see it, you use WHERE id=".$row['id']; <- where does it get the row['id'] from? If you are gonna get that from the form, you need to put it in a hidden input, so it can be parsed and actually do the update statement.
     
    PoPSiCLe, Apr 2, 2009 IP
  6. yoyo99

    yoyo99 Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok, I have changed the update query and removed all the php variables to test it like so:
    UPDATE myTable Set lName='Smith', Fname='John', clientEmail= 'yoyo@somthing.com'
    
    Code (markup):
    and it updated all my db records as it should, so then I tried:
    UPDATE myTable Set lName='{$lName}', fName='{$fName}' WHERE id=1
    
    Code (markup):
    and it should update my record with id 1 with my $lName and $fName variables that I enter in, but in my database record its blank. Any ideas?
     
    yoyo99, Apr 2, 2009 IP
  7. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #7
    $sql = mysql_query("UPDATE myTable SET lName='$lName', fName='$fName' WHERE id='1'");
    PHP:
     
    ActiveFrost, Apr 2, 2009 IP
  8. yoyo99

    yoyo99 Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thanks for the reply, but still no luck =(
     
    yoyo99, Apr 2, 2009 IP
  9. yoyo99

    yoyo99 Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Ok, so I played around with the code and found what the problem is. I found that if I made a new input field in my table that is html, not being called from php, my record in the database will update. So the problem I'm having is assigning a value to my variable in the php echo statement I have.

    Here is the change I made:
    <tr>
    <td><input type="text" name="fName" value="<? echo $fName?>" /></td>
    </tr>
    Code (markup):
    And it will update the record in the database.

    So how can I get the values from my variables in my echo statement:
    
    while ($row = mysql_fetch_assoc($result)){
    echo "<tr>
    <td nowrap>".'<input type="text" name="lName" value="'.$lName.'">'."</td>
    <td nowrap>".'<input type="text" name="fName" value="'.$fName.'">'."</td>
    </tr>
    
    Code (markup):
    Any Ideas, I've been up all night and I'm sure its probably easy, I just can't get it. Any suggestions would be greatly appreciated. THANKS! :D
     
    yoyo99, Apr 2, 2009 IP
  10. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #10
    while ($row = mysql_fetch_assoc($result))
       echo '<tr>
          <td nowrap><input type="text" name="lname" value="'
          . htmlspecialchars($row['lname']) . '"></td>
          <td nowrap><input type="text" name="fname" value="'
          . htmlspecialchars($row['fname']) . '"></td>
          </tr>';
    
    Code (markup):
    Also remember that variables and array subscripts are case-sensitive in PHP. You've been back and forth between lname and lName and if you're not careful, you will cause great confusion for yourself this way. Be consistent. Better yet, turn on full error reporting so that you are informed whenever you try to access an uninitialised variable; it promotes good programming habits and highlights a lot of bugs that otherwise go unnoticed.
     
    SmallPotatoes, Apr 2, 2009 IP
  11. yoyo99

    yoyo99 Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hey Thanks for the reply, SmallPotatoes, I'm sorry for the confusion on my variable names. I wasn't copy/pasting my code, but rather typing it by hand and that was a typo.

    I tried your code and still not updating my record in my DB, Im going bald from pulling all of my hair out from frustration...

    I appreciate your time guys for replying, does anyone have any other ideas or see something else wrong? Please help =)
     
    yoyo99, Apr 2, 2009 IP
  12. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #12
    Just curious, what version of PHP and MySQL you are using ? According to your PHP tags, you are using PHP4 with MySQL 5 ?
     
    ActiveFrost, Apr 2, 2009 IP
  13. yoyo99

    yoyo99 Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    yes, its PHP4 and MySQL 5
     
    yoyo99, Apr 2, 2009 IP
  14. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #14
    I suggest you to upgrade to PHP5 and use <?php ?> tags - your problem could show up due to your version of PHP as it's using older MySQL dll ( functions, etc. ). Yes, it's still +/- the same, but .. you should always use the latest version/files :)
     
    ActiveFrost, Apr 2, 2009 IP
  15. yoyo99

    yoyo99 Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    I don't think I can upgrade my php. I'm on a shared hosting plan with godaddy and I didn't see any option to change it.
     
    yoyo99, Apr 2, 2009 IP