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.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    PhpMySQL update tables

    Hello everyone! I'm having a small issue with updating tables in MySQL.


    As you see here in phpmyadmin:

    I have sid and id, the only problem is that they both need to be auto-increments, how can I do that? I know a few methods, but they can be a bit confusing, what is the best method for this?


    The problem php code that I've narrowed down is (if there is a problem, I'm sure there's not, but just to make sure):

    Code:
    $idChar = $_GET['id'];
    if(!isset($idChar)){//this value MUST be set in order for the next query to work
    
    
    $x = mysql_query("SELECT sid FROM simpsons ORDER BY sid ASC LIMIT 1");
    while($row = mysql_fetch_array($x)){
    $idChar = $row['sid'];
    }
    
    }
    
    
    $sUpdate = $_GET['update'];
    if($sUpdate == "yes") {
    //echo "<br />Form has been submitted<br />";
    
    $newCharF = $_POST['fname'];
    $newCharL = $_POST['lname'];
    $newCharDes = $_POST['description'];
    echo $newCharF . " | " . $newCharL . " | " . $newCharDes;
    
    
    $sql = "UPDATE simpsons SET
    fname=\"$newCharF\",
    lname=\"$newCharL\",
    description=\"$newCharDes\"
    WHERE sid=$idChar";
    //echo $idChar;
    
    $results3 = mysql_query($sql) or die (mysql_error());
    }
    Last edited by Fou-Lu; 08-09-2012 at 08:17 PM. Reason: No reason to empty a post with replies.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Why would you want two auto increment fields or two fields doing the same thing at all for that matter? Seems silly, but to answer your question you could use a trigger to copy the value to the second field when a new row is created, or grab the auto_increment id with mysqli_insert_id() and copy the same value from the sid to id. Again, I can't see a reason to do this at all, but it's certainly possible.

  • Users who have thanked Inigoesdr for this post:

    newGenWeb (08-09-2012)

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I hate my school, but thanks for your input.
    Last edited by newGenWeb; 08-09-2012 at 07:43 PM.


  •  

    Tags for this Thread

    Posting Permissions

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