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 11 of 11
  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts

    Form updates table row

    I would like to make this script so that instead of inserting info it updates the code to update the first row.

    I thought of adding this to the update
    PHP Code:
    WHERE id='$id' 
    and having this to say what row
    PHP Code:
    $id $rows['id']; 
    So something like this:

    PHP Code:
    <?php
     
    require('inc/config.php');
    $id $rows['id'];
    $sql="UPDATE Persons WHERE id='$id' (FirstName, LastName, Age)
    VALUES
    ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"
    ;

    if (!
    mysql_query($sql))
      {
      die(
    'Error: ' mysql_error());
      }
    echo 
    "1 record added";
    ?>
    And the form:

    PHP Code:
    <html>
    <
    body>

    <
    form action="insert.php" method="post">
    Firstname: <input type="text" name="firstname" />
    Lastname: <input type="text" name="lastname" />
    Age: <input type="text" name="age" />
    <
    input type="submit" />
    </
    form>

    </
    body>
    </
    html
    Is that correct?

    and mostly will it work?

  • #2
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts
    Okay I currently got this for the form called page1.php
    PHP Code:
    <html>
    <
    body>

    <
    form action="upage2.php" method="post">
    Firstname: <input type="text" name="firstname" />
    Lastname: <input type="text" name="lastname" />
    middlename: <input type="text" name="middlename" />
    <
    input type="submit" />
    </
    form>

    </
    body>
    </
    html
    and this for the page2.php
    PHP Code:
    <?php
    include ('inc/config.php');
    $id $rows['id'];  

    mysql_query("
    UPDATE Persons SET
    $FirstName = 'mysql_real_escape_string ($_POST[firstname])',
    $LastName = 'mysql_real_escape_string ($_POST[lastname])',
    $MiddleName= 'mysql_real_escape_string ($_POST[middlename])';
    WHERE id='$id'
    "
    );  

    ?>
    It isnt working at all and I am getting no errors

  • #3
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,300
    Thanks
    4
    Thanked 203 Times in 200 Posts
    In page2.php where is $rows['id'] coming from?
    Dave .... HostMonster for all of your hosting needs

  • #4
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts
    I fixed that now there is a new 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 ' 'LastName', 'MiddleName'' at line 1
    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set('display_errors''1');

    require_once (
    'inc/config.php');
    $query "SELECT * FROM testing WHERE FirstName, LastName, MiddleName";
    $result mysql_query($query) or die(mysql_error()); 
    while (
    $row mysql_fetch_array ($result)) 
    {
       echo 
    $row['FirstName'];  
     echo 
    $row['LastName'];
       echo 
    $row['MiddleName'];
    }
    ?>

  • #5
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    learn SQL first, read what you are telling the query to do..it makes no sense.

    Code:
    SELECT what you want
    FROM where it's stored
    WHERE some condition is met
    at the moment you are telling SQL to SELECT all FROM testing WHERE FirstName, LastName, MiddleName...that makes no sense at all.

    either assign conditions:

    WHERE FirstName='Fred'

    etc

    or

    PHP Code:
    $q 'SELECT FirstName, LastName, MiddleName FROM testing'

    // is probably what you want.. 

  • #6
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts
    I have fixed that problem.

    Now i got a question...


    I would like to echo it separately in different places of my webpage, can I just do this?

    At the top of the page
    PHP Code:
     <?php
    require_once ('inc/config.php');
    $query "SELECT * FROM testing";
    $result mysql_query($query) or die(mysql_error());
    while (
    $row mysql_fetch_array ($result))
    ?>
    Around the webpage.


    PHP Code:
    <?php $row['MiddleName'?>
    or
    PHP Code:
    <link rel="stylesheet" type="text/css" href="<?php $row['Url'?>/css/style.css" />
    or
    PHP Code:
        <title><?php $row['WebsiteTitle'?></title>


    Like that? Or I have to have it echo?

  • #7
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    a while loop in this context will perform the code for each record in the database.

    if you have ten records, the code between the while { } is done 10 times.

    if you have just one record, you can do

    PHP Code:
    $row=mysql_fetch_array($query);

    $row['x']; // now contains an array of data from the db, so yes, you could do:

        <title><?php echo $row['WebsiteTitle']; ?> </title>
    but if you do this in a WHILE loop then you will end up with X amount of data printed out.

    what are you trying to achieve exactly, there is definitely some confusion
    Last edited by met; 11-04-2009 at 10:49 PM.

  • #8
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts
    errrh... What??

    What does that mean?
    Last edited by bucket; 11-04-2009 at 10:49 PM.

  • #9
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    PHP Code:
    <?php
    require_once ('inc/config.php');
    $query "SELECT * FROM testing";
    $result mysql_query($query) or die(mysql_error());
    while (
    $row mysql_fetch_array ($result)) { // do something 
    }

    /* ok so this code gets all the data from the table testing, and
     then you pass the result into a while loop. in the while loop,
     you are setting an array $row with the values from the query.

    this data can be accessed like so: */

    $foo =  $row['columnName'];
    echo 
    $row['someValue'];

    /* a WHILE loop is used to repeat the code between the bracers for every record returned.
     this is useful when you need to display sets of data, e.g. a list of members

    to access this data you need to do something with it during the while loop.

    */
    ?>
    you are being too vague to get any real help. What is stored in table testing? what are you trying to achieve...

  • #10
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts
    Code:
    <?php
    require_once ('inc/config.php');
    $query = "SELECT * FROM testing";
    $result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_array ($result)) {
    ?>
     
    TextHere: <br>
    <?php $foo = $row['FirstName'];?> 
    
    <br>
    Text here <br>
    <?php echo $row['MiddleName'];?> 
    
    <?php
    }
    ?>
    Will that work?

  • #11
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    technically yes

    the code i supplied was an example

    $foo = $row['FirstName']; will just put the value of FirstName in to the variable $foo and over write it each time, accomplishing nothing.

    the second will echo out the value of MiddleName for each record in the database.

    again if you could be more specific about WHAT you want to do i can help more


  •  

    Posting Permissions

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