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 20
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Updating a database

    Hey guys, I'm having some trouble with the PHP on my update section.

    Can someone tell me what's wrong here? Thank You.

    PHP Code:

    <?php

    $nome 
    $_POST['nome'];
    $telefone $_POST['telefone'];
    $e_mail $_POST['e_mail'];
    $morada $_POST['morada'];
    $pontos$_POST['pontos'];

    $db="database";
    $link mysql_connect('localhost','root','');
    if (! 
    $link)
    die(
    "Impossível ligar ao MySQL");
    mysql_select_db($db $link)
    or die(
    "Impossível abrir $db: ".mysql_error());
    $result mysql_query('UPDATE clientes SET nome=$nome,telefone=$telefone,e_mail=$e_mail,morada=$morada,pontos=$pontos WHERE id_clientes=$id_clientes') or die(mysql_error());
    mysql_close($link);

    if ((
    $result)==1){
    echo 
    "<p>Linha actualizada com sucesso!<br>";
    }

    ?>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Hey guys, I'm having some trouble with the PHP on my update section.

    Can someone tell me what's wrong here?
    It'd be better to tell us what's your problem with that code.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Oh sorry forgot about the error.

    It's:

    Unknown column '$id_clientes' in 'where clause'

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Okay! PHP can't parse the variables inside single quotes. You'd need to either limit that quotes around the variable or use double quotes, like
    PHP Code:
    $result mysql_query("UPDATE clientes SET nome='$nome',telefone='$telefone',e_mail='$e_mail',morada='$morada',pontos='$pontos' WHERE id_clientes=$id_clientes") or die(mysql_error()); 
    (It's not an issue to put single quote inside double quote, and you should put quotes around all string variables in your query)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I tried it but now if i try to $id_clientes = $_POST['id_clientes']; in the start of the php code i get the undefined index error :S if i don't.. still get:

    Notice: Undefined variable: id_clientes in C:\wamp\www\update_clientes.php on line 17
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
    Last edited by Namii; 01-21-2010 at 12:15 PM.

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Enclose your code inside
    PHP Code:
    if(isset($_POST['id_clientes'])){

    //all your current code here


    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    New Coder
    Join Date
    Jan 2010
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ok i tried that now it just shows a blank page.. The

    PHP Code:
    if (($result)==1){
    echo 
    "<p>Linha actualizada com sucesso!<br>";

    Doesn't echo the update success, or update the database table.

  • #8
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Ok i tried that now it just shows a blank page..
    Have you submitted your form to that page? (Please don't say that you don't have a form )
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #9
    New Coder
    Join Date
    Jan 2010
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Yes i do have a form lol

    And yes i did hit submit.. Just shows the blank page.

  • #10
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by Namii View Post
    Yes i do have a form lol

    And yes i did hit submit.. Just shows the blank page.
    Then there are two possibilities. Either you don't have a form-element having name="id_clientes" or you haven't specified method="post" to that form.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    Namii (01-21-2010)

  • #11
    New Coder
    Join Date
    Jan 2010
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    OK i didnt have the input name="id_clientes" and now i do.. but we go back to the SQL syntax error:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

  • #12
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Change
    PHP Code:
    $result mysql_query("UPDATE clientes SET nome='$nome',telefone='$telefone',e_mail='$e_mail',morada='$morada',pontos='$pontos' WHERE id_clientes=$id_clientes") or die(mysql_error()); 
    to
    PHP Code:
    echo $sql="UPDATE clientes SET nome='$nome',telefone='$telefone',e_mail='$e_mail',morada='$morada',pontos='$pontos' WHERE id_clientes=$id_clientes";
    mysql_query($sql) or die(mysql_error()); 
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #13
    New Coder
    Join Date
    Jan 2010
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    It's all good till id_clientes as you can see here:

    UPDATE clientes SET nome='Manuel Cruz',telefone='912345654',e_mail='manuelcruz@mail.pt',morada='Praça das Lamúrias, Faro',pontos='66' WHERE id_clientes=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

  • #14
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    UPDATE clientes SET nome='Manuel Cruz',telefone='912345654',e_mail='manuelcruz@mail.pt',morada='Praça das Lamúrias, Faro',pontos='66' WHERE id_clientes=
    It's obvious, there's no value set for the variable $id_clientes
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #15
    New Coder
    Join Date
    Jan 2010
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Wouldnt this
    PHP Code:
     $id_clientes $_POST['id_clientes']; 
    set a value for the variable id_clientes?

    Else I didn't understand about the value set :S sorry, i'm learning by myself therefore the dumb questions..

    And now i get

    Unknown column 'post' in 'where clause'
    Last edited by Namii; 01-21-2010 at 03:00 PM.


  •  
    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
    •