www.webdeveloper.com
Results 1 to 3 of 3

Thread: Updating MySQL-database with PHP

  1. #1
    Join Date
    Jan 2009
    Location
    Finland
    Posts
    5

    Question Updating MySQL-database with PHP

    Hi! I'm trying to make an update to my database through a php-page. The point is that you can save an event to the database, with id (automatically genrated), name, day, month, year, time, place and description. When one has saved an event to the database, I want it to be modified, so that the user picks from a scroll down list the name of the event and presses the button, when it loads it to textareas (with the values from that event) and then the user can change the fields he/she wants and save it aka update it to the database. This works otherwise, but it won't update the database. So what's wrong here? I guess it somehow forgets which id to update, but I'm really lost here Please help! Btw, some of it is in finnish, but the code is in English of course Here's the code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Events</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="css/main.css" rel="stylesheet" type="text/css" />
    <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="xml/rss.php" />
    </head>
    <body>

    <div id="wrapper">

    <div id="header">
    <a href="index.php">Etusivu</a>
    <a href="tapahtumat.php">Tapahtumat</a>
    <a href="jotain.php">Yhteydenotto</a>
    </div>

    <div id="content">
    <h1>Tapahtumien muokkaus</h1>
    <p>Valitse tapahtuma:<br />
    <form action="tapahtumat_mod.php" method="get">
    <p>
    <select name="tapahtumat">
    <?php
    //avataan tietokantayhteys ja haetaan kannasta tapahtumat
    //ja laitetaan ne pudotusvalikkoon
    require_once('../conf/config.php');
    require_once('../conf/dbopen.php');
    $tapahtumat = mysql_query("SELECT name
    FROM happening
    ORDER BY name ASC");
    for ($i=0; $i<mysql_num_rows($tapahtumat); $i++){
    echo "<option>".mysql_result($tapahtumat,$i)."</option>";
    }
    ?>
    </select>
    </p>
    <p>
    <input type="submit" name="mod" value="Muokkaa" />
    </p>
    </form>

    <?php
    //jos muokkaa nappia on painettu, suoritetaan haku, joka etsii valitun tapahtuman perusteella nimen,
    //ja ajankohdan ja sijoittaa ne taulukkoon tekstilaatikoiden sisään
    if ($_GET['mod'] == 'Muokkaa'){
    $query = "SELECT id, name, day, month, year, time, place, description
    FROM happening
    WHERE name = '$_GET[tapahtumat]'"
    ;

    //Suoritetaan SQL-kysely:
    $result= mysql_query($query)
    or die("Kyselyssä tapahtui virhe.: " . mysql_error());

    // Käydään rivejä niin kauan kuin niitä riittää
    $rivit =mysql_num_rows($result);
    echo "<table>";
    // Käydään jokainen rivi läpi
    while ($rivi = mysql_fetch_array($result)){
    echo "<form>\n" .
    "<input type='hidden' name='hideme' value='hidden' />\n" .
    " {$rivi['0']}" .
    "</form>\n" .
    "<tr>\n" .
    "<textarea cols='45' rows='2'>\n" .
    " {$rivi['1']}" .
    "</textarea>\n" .
    "</tr>\n" .
    "<tr>\n" .
    "<textarea cols='10' rows='1'>\n" .
    " {$rivi['2']}" .
    "</textarea>\n" .
    "</tr>\n" .
    "<tr>\n" .
    "<textarea cols='10' rows='1'>\n" .
    " {$rivi['3']}" .
    "</textarea>\n" .
    "</tr>\n" .
    "<tr>\n" .
    "<textarea cols='10' rows='1'>\n" .
    " {$rivi['4']}" .
    "</textarea>\n" .
    "</tr>\n" .
    "<tr>\n" .
    "<textarea cols='10' rows='1'>\n" .
    " {$rivi['5']}" .
    "</textarea>\n" .
    "</tr>\n" .
    "<tr>\n" .
    "<textarea cols='45' rows='1'>\n" .
    " {$rivi['6']}" .
    "</textarea>\n" .
    "</tr>\n" .
    "<tr>\n" .
    "<textarea cols='45' rows='10'>\n" .
    " {$rivi['7']}" .
    "</textarea>\n" .
    "</tr>\n" ;
    }
    echo "</table>";
    }
    <form action="tapahtumat_mod.php" method="post">
    <input type="submit" name="save" value="Tallenna" />
    <input name="id" type="hidden" text="{$rivi['0']}" />
    </form>
    //jos on painettu tallenna-nappia
    if ($_POST['save'] == 'Tallenna'){
    $query = "UPDATE happening
    SET name='$rivi[1]', day='$rivi[2]', month='$rivi[3]', year='$rivi[4]', time='$rivi[5]', place='$rivi[6]', description='$rivi[7]'
    WHERE id='$rivi[0]'";

    //Suoritetaan SQL-kysely:
    $result= mysql_query($query)
    or die("Kyselyssä tapahtui virhe.: " . mysql_error());

    }
    require_once('../conf/dbclose.php');
    ?>

    </div>

    <div id="rightcolumn">
    </div>

    </div>
    </body>
    </html>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,148
    I can't read through all that code (please see this board's [php]...[/php] BBCode tags), but are you including the DB record ID as part of the form data? I usually have it in a hidden field.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2009
    Location
    Finland
    Posts
    5

    The code again, with the tags (hopefully works)

    The id was also in that code which prints the info what the user wants to modify, because I thought it has to be there. It was suppose to be hidden, but it didn't work..

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Events</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="css/main.css" rel="stylesheet" type="text/css" />
    <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="xml/rss.php" />
    </head>
    <body>
    
    <div id="wrapper">
    
    <div id="header">
    <a href="index.php">Etusivu</a>
    <a href="tapahtumat.php">Tapahtumat</a>
    <a href="jotain.php">Yhteydenotto</a>
    </div>
    
    <div id="content">
    <h1>Tapahtumien muokkaus</h1>
    <p>Valitse tapahtuma:<br />
    <form action="tapahtumat_mod.php" method="get">
    <p>
    <select name="tapahtumat">
    PHP Code:
    <?php
    //avataan tietokantayhteys ja haetaan kannasta tapahtumat
    //ja laitetaan ne pudotusvalikkoon
    require_once('../conf/config.php');
    require_once(
    '../conf/dbopen.php');
    $tapahtumat mysql_query("SELECT name
    FROM happening
    ORDER BY name ASC"
    );
    for (
    $i=0$i<mysql_num_rows($tapahtumat); $i++){
    echo 
    "<option>".mysql_result($tapahtumat,$i)."</option>";
    }
    ?>
    HTML Code:
    </select>
    </p>
    <p>
    <input type="submit" name="mod" value="Muokkaa" />
    </p>
    </form>
    PHP Code:
    <?php
    //jos muokkaa nappia on painettu, suoritetaan haku, joka etsii valitun tapahtuman perusteella nimen,
    //ja ajankohdan ja sijoittaa ne taulukkoon tekstilaatikoiden sisään
    if ($_GET['mod'] == 'Muokkaa'){
    $query "SELECT id, name, day, month, year, time, place, description
    FROM happening
    WHERE name = '
    $_GET[tapahtumat]'"
    ;

    //Suoritetaan SQL-kysely:
    $resultmysql_query($query)
    or die(
    "Kyselyssä tapahtui virhe.: " mysql_error());

    // Käydään rivejä niin kauan kuin niitä riittää
    $rivit =mysql_num_rows($result);
    echo 
    "<table>";
    // Käydään jokainen rivi läpi
    while ($rivi mysql_fetch_array($result)){
    echo 
    "<form>\n" .
    "<input type='hidden' name='hideme' value='hidden' />\n" .
    {$rivi['0']}.
    "</form>\n" .
    "<tr>\n" .
    "<textarea cols='45' rows='2'>\n" .
    {$rivi['1']}.
    "</textarea>\n" .
    "</tr>\n" .
    "<tr>\n" .
    "<textarea cols='10' rows='1'>\n" .
    {$rivi['2']}.
    "</textarea>\n" .
    "</tr>\n" .
    "<tr>\n" .
    "<textarea cols='10' rows='1'>\n" .
    {$rivi['3']}.
    "</textarea>\n" .
    "</tr>\n" .
    "<tr>\n" .
    "<textarea cols='10' rows='1'>\n" .
    {$rivi['4']}.
    "</textarea>\n" .
    "</tr>\n" .
    "<tr>\n" .
    "<textarea cols='10' rows='1'>\n" .
    {$rivi['5']}.
    "</textarea>\n" .
    "</tr>\n" .
    "<tr>\n" .
    "<textarea cols='45' rows='1'>\n" .
    {$rivi['6']}.
    "</textarea>\n" .
    "</tr>\n" .
    "<tr>\n" .
    "<textarea cols='45' rows='10'>\n" .
    {$rivi['7']}.
    "</textarea>\n" .
    "</tr>\n" ;
    }
    echo 
    "</table>";
    }
    <
    form action="tapahtumat_mod.php" method="post">
    <
    input type="submit" name="save" value="Tallenna" />
    <
    input name="id" type="hidden" text="{$rivi['0']}/>
    </
    form>
    //jos on painettu tallenna-nappia
    if ($_POST['save'] == 'Tallenna'){
    $query "UPDATE happening
    SET name='
    $rivi[1]', day='$rivi[2]', month='$rivi[3]', year='$rivi[4]', time='$rivi[5]', place='$rivi[6]', description='$rivi[7]'
    WHERE id='
    $rivi[0]'";

    //Suoritetaan SQL-kysely:
    $resultmysql_query($query)
    or die(
    "Kyselyssä tapahtui virhe.: " mysql_error());

    }
    require_once(
    '../conf/dbclose.php');
    ?>
    HTML Code:
    </div>
    
    <div id="rightcolumn">
    </div>
    
    </div>
    </body>
    </html> 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles