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

    Edit a Database with PHP

    Hi guys. I need some help in the edit part of the PHP.

    I've created a form and tried to fetch or request the data from the database and i still cant do it.

    In the HTML i tried:

    <p align="center" class="style1">ID: <?php echo $_REQUEST['id_fornec'];?><br/>

    And in the PHP:

    $update = "UPDATE fornecedores set nome = '".$_REQUEST ['nome']."',telefone = '".$_REQUEST['telefone']."',e_mail = '".$_REQUEST['e_mail']."',empresa = '".$_REQUEST['empresa'] where id = .$_REQUEST['id']."'";

    $result = mysql_query($update, $connection);

    And how do i navigate in the page to edit the other ones.

    Thanks a lot and sorry for bothering.. I'm still a PHP rookie.

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    You do understand that PHP is a server side language and that it runs before html gets output, so doing stuff on the html page won't do anything unless you post/get your data to a script. If you want some help with this you will need to post your whole code
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    My PHP code is the following:

    <?php

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

    if ($nome == '' || $telefone == '' || $e_mail == '' || $empresa == ''){
    echo '<p align=center>Campos em falta.
    <a href="edit_fornec.html">Clique</a> para voltar e tente de novo.'; exit;}

    $connection = mysql_connect('localhost','root','');

    if (!$connection)
    {echo '<p> Erro: Falha na ligação.'; exit;}

    mysql_select_db('database', $connection);

    $update = "UPDATE fornecedores set nome = '".$_REQUEST ['nome']."',telefone = '".$_REQUEST['telefone']."',e_mail = '".$_REQUEST['e_mail']."',empresa = '".$_REQUEST['empresa'] where id = .$_REQUEST['id']."'";

    $result = mysql_query($update, $connection);

    if (($result)==1){
    echo "<p>Dados alterados<br>";
    }

    ?>

    And the HTML code of the form is:

    <form method = "POST" action = "edit_fornec.php">
    <fieldset>
    <p align="center" class="style1"><font color="#0000CC">-= Editar Fornecedores =-</font></p>
    <p align="center" class="style1">ID: <?php echo $_REQUEST['id_fornec'];?><br/>
    Nome:
    <input type = "text" name = "nome">
    </p>
    <p align="center" class="style1">Telefone:
    <input type = "text" name = "telefone">
    </p>
    <p align="center" class="style1">E-Mail:
    <input type = "text" name = "e_mail">
    </p>
    <p align="center" class="style1">Empresa:
    <input type = "text" name = "empresa">
    </p>
    <p align="center" class="style1">
    <input type = "submit" value = "Alterar">
    </fieldset>
    </form>

  • #4
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    I don't see a POST submission area on your code. Try this.

    Code:
    <?php
    
    if ($_POST['submit'])
    {
    
    $nome = $_POST['nome'];
    $telefone = $_POST['telefone'];
    $e_mail = $_POST['e_mail'];
    $morada = $_POST['morada'];
    $pontos= $_POST['pontos'];
    
    if ($nome == '' || $telefone == '' || $e_mail == '' || $empresa == ''){
    echo '<p align=center>Campos em falta.
    <a href="edit_fornec.html">Clique</a> para voltar e tente de novo.'; exit;}
    
    $connection = mysql_connect('localhost','root','');
    
    if (!$connection)
    {echo '<p> Erro: Falha na ligação.'; exit;}
    
    mysql_select_db('database', $connection);
    
    $update = "UPDATE fornecedores set nome = '".$_REQUEST ['nome']."',telefone = '".$_REQUEST['telefone']."',e_mail = '".$_REQUEST['e_mail']."',empresa = '".$_REQUEST['empresa'] where id = .$_REQUEST['id']."'";
    
    $result = mysql_query($update, $connection);
    
    if (($result)==1){
    echo "<p>Dados alterados<br>";
    }
    }
    else {
    ?>
    
    And the HTML code of the form is:
    
    <form method = "POST" action = "edit_fornec.php">
    <fieldset>
    <p align="center" class="style1"><font color="#0000CC">-= Editar Fornecedores =-</font></p>
    <p align="center" class="style1">ID: <?php echo $_REQUEST['id_fornec'];?><br/>
    Nome:
    <input type = "text" name = "nome">
    </p>
    <p align="center" class="style1">Telefone:
    <input type = "text" name = "telefone">
    </p>
    <p align="center" class="style1">E-Mail:
    <input type = "text" name = "e_mail">
    </p>
    <p align="center" class="style1">Empresa:
    <input type = "text" name = "empresa">
    </p>
    <p align="center" class="style1">
    <input type = "submit" name="submit" value = "Alterar">
    </fieldset>
    </form>
    <?php } ?>
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ok i tried it but now it gives me an error in line 23.. Which is:

    Parse error: parse error in C:\wamp\www\editar_fornec.php on line 23

    and line 23 is

    $update = "UPDATE fornecedores set nome = '".$_REQUEST ['nome']."',telefone = '".$_REQUEST['telefone']."',e_mail = '".$_REQUEST['e_mail']."',empresa = '".$_REQUEST['empresa'] where id = .$_REQUEST['id_fornec']."'";

    I don't see anything wrong there, maybe because i'm not very familiar with PHP yet don't know..

  • #6
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    Use [php ] and [/php ] to enclose your php code on the forums...
    You messed up the string concatenation with respect to the last 2 variables and I'm not sure if "$_REQUEST ['nome']" with the `extra` space is valid either.
    One more thing: you're already assigning the $_POST values to some variables so, no need to use the $_REQUEST superglobal.
    PHP Code:
    $update "UPDATE fornecedores set nome = '".$nome."',telefone = '".$telefone'."',e_mail '".$e_mail."',empresa '".$_POST['empresa']."' where id '".$_POST['id_fornec']."'"; 
    I'm only using $_POST for the last 2 variables as you haven't assigned them to anything as opposed to the previous variables.

  • Users who have thanked shadowmaniac for this post:

    Namii (01-15-2010)

  • #7
    New Coder
    Join Date
    Jan 2010
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ok i tried that too but now i'm getting Notices about undefined index of all the variables i'm trying to use.. Even submit ( Notice: Undefined index: submit in C:\wamp\www\editar_fornec.php on line 3 )

    At the moment my code is the following

    PHP Code:

    <?php 

    if ($_POST['submit'])
    {

    $id_fornec $_POST['id_fornec'];
    $nome $_POST['nome'];
    $telefone $_POST['telefone'];
    $e_mail $_POST['e_mail'];
    $empresa $_POST['empresa'];

    if (
    $nome == '' || $telefone == '' || $e_mail == '' || $empresa == ''){
    echo 
    '<p align=center>Campos em falta.
    <a href="editar_fornec.html">Clique</a> para voltar e tente de novo.'
    ; exit;}

    $connection mysql_connect('localhost','root','');

    if (!
    $connection)
    {echo 
    '<p> Erro: Falha na ligação.'; exit;}

    mysql_select_db('database'$connection);

    $update "UPDATE fornecedores set nome = '".$nome."',telefone = '".$telefone."',e_mail = '".$e_mail."',empresa = '".$_POST[$empresa]."' where id = '".$_POST['id_fornec']."'";  

    $result mysql_query($update$connection);

    if ((
    $result)==1){
    echo 
    "<p>Dados alterados<br>";
    }
    }
    else {

    ?>

    <form method = "POST" action = "editar_fornec.php">
    <fieldset>
        <p align="center" class="style1"><font color="#0000CC">-= Editar Fornecedores =-</font></p>
        <p align="center" class="style1">ID: <?php echo $_POST['id_fornec'];?><br/>
          Nome: <?php echo $_POST['nome'];?>
          <input type = "text" name = "nome">
          </p>
        <p align="center" class="style1">Telefone: <?php echo $_POST['telefone'];?>
          <input type = "text" name = "telefone">
          </p>
        <p align="center" class="style1">E-Mail: <?php echo $_POST['e_mail'];?>
          <input type = "text" name = "e_mail">
          </p>
        <p align="center" class="style1">Empresa: <?php echo $_POST['empresa'];?>
          <input type = "text" name = "empresa">
          </p>
        <p align="center" class="style1">
          <input type = "submit" name="submit" value = "Alterar">
    </fieldset>
    </form>
    <?php ?>
    Notice: Undefined index: id_fornec in C:\wamp\www\editar_fornec.php on line 57
    Notice: Undefined index: nome in C:\wamp\www\editar_fornec.php on line 58
    Notice: Undefined index: telefone in C:\wamp\www\editar_fornec.php on line 61
    Notice: Undefined index: e_mail in C:\wamp\www\editar_fornec.php on line 64
    Notice: Undefined index: empresa in C:\wamp\www\editar_fornec.php on line 67

    Thank you for your patience ( and i didn't knew about the php tags to put the code sorry)
    Last edited by Namii; 01-14-2010 at 10:48 PM. Reason: information missing

  • #8
    Regular Coder
    Join Date
    Nov 2009
    Location
    Hamilton, New Zealand
    Posts
    126
    Thanks
    0
    Thanked 17 Times in 17 Posts
    You need to test if it isset first.

    PHP Code:

    if (isset($_POST['submit']) && $_POST['submit']) 
    Affordable Web Design (New Zealand Based)
    Internet Marketing Guru
    PHP/mySQL Expert
    -------------------------------------------

  • Users who have thanked Rebbu for this post:

    Namii (01-15-2010)

  • #9
    New Coder
    Join Date
    Jan 2010
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I tried it the line 3 notice is gone thank you but the other ones still pervade

  • #10
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    Same deal as pointed out by Rebbu. In your HTML code section, wherever you have something like
    PHP Code:
    <?php echo $_POST['id_fornec'];?>
    change it to
    PHP Code:
    <?php if(isset($_POST['id_fornec'])) { echo $_POST['id_fornec']; } ?>
    As per your provided code and as reported by php, 5 such changes have to be made.

    Reason it's not working is because the first time you access the page, you have not yet submitted any data yet so variables like $_POST['id_fornec'] are basically non-existent hence the php warnings and the reason why you need to check whether they've been set first using the isset() function before trying to echo them out.

  • Users who have thanked shadowmaniac for this post:

    Namii (01-15-2010)

  • #11
    New Coder
    Join Date
    Jan 2010
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ok i tried that and the Notices are gone. Thank You. But still the table in the database exists and no information is shown to change.

    Here's the code at the moment:

    PHP Code:

    <?php 


    if (isset($_POST['submit']) && $_POST['submit'])  {

    $id_fornec $_POST['id_fornec'];
    $nome $_POST['nome'];
    $telefone $_POST['telefone'];
    $e_mail $_POST['e_mail'];
    $empresa $_POST['empresa'];

    if (
    $nome == '' || $telefone == '' || $e_mail == '' || $empresa == ''){
    echo 
    '<p align=center>Campos em falta.
    <a href="editar_fornec.html">Clique</a> para voltar e tente de novo.'
    ; exit;}

    $connection mysql_connect('localhost','root','');

    if (!
    $connection)
    {echo 
    '<p> Erro: Falha na ligação.'; exit;}

    mysql_select_db('database'$connection);

    $update "UPDATE fornecedores set nome = '".$nome."',telefone = '".$telefone."',e_mail = '".$e_mail."',empresa = '".$_POST[$empresa]."' where id = '".$_POST['id_fornec']."'";  

    $result mysql_query($update$connection);

    if ((
    $result)==1){
    echo 
    "<p>Dados alterados<br>";
    }
    }
    else {

    ?>

    <form method = "POST" action = "editar_fornec.php">
    <fieldset>
        <p align="center" class="style1"><font color="#0000CC">-= Editar Fornecedores =-</font></p>
        <p align="center" class="style1">ID:   <?php if(isset($_POST['id_fornec'])) { echo $_POST['id_fornec']; } ?>   <br/>
          Nome:   <?php if(isset($_POST['nome'])) { echo $_POST['nome']; } ?>   
          <input type = "text" name = "nome">
          </p>
        <p align="center" class="style1">Telefone:   <?php if(isset($_POST['telefone'])) { echo $_POST['telefone']; } ?>   
          <input type = "text" name = "telefone">
          </p>
        <p align="center" class="style1">E-Mail:   <?php if(isset($_POST['e_mail'])) { echo $_POST['e_mail']; } ?>   
          <input type = "text" name = "e_mail">
          </p>
        <p align="center" class="style1">Empresa:   <?php if(isset($_POST['empresa'])) { echo $_POST['empresa']; } ?>   
          <input type = "text" name = "empresa">
          </p>
        <p align="center" class="style1">
          <input type = "submit" name="submit" value = "Alterar">
    </fieldset>
    </form>
    <?php ?>

  • #12
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Try
    Code:
    $result = mysql_query($update, $connection) 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
    Hmm that didn't help.. Also because it doesn't return any errors just.. Doesn't show anything in the form.

  • #14
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Quote Originally Posted by Namii View Post
    Hmm that didn't help.. Also because it doesn't return any errors just.. Doesn't show anything in the form.
    Looks like you haven't turned error reporting on. If so, do it first by adding
    PHP Code:
    <?php
    ini_set
    ('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting (E_ALL);
    ?>
    at the top of your code.
    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
    Still doesn't show any errors. It's still the same.


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