Jump to content

read & update mysql table from php help...


bluefrog

Recommended Posts

MOD: Thanks for moving to the correct forum - much appreciated!

 

OK, I've taken a look around and cannot find a complete tutorial on how to do this.

 

I have created a database with a table and the following fields;

 

id, day, month, year, event, type

 

What I am trying to do is read the data held in the table by referencing the type field (unique data), have it populate data firleds which are user editable and then once all changes are made have it update into the table.

 

I can't imagine that this is that hard at all, but I cannot for the life of me figure it out!

 

Anybody got a tutorial or some code which will do this?

Link to comment
Share on other sites

Well to get the primary values I used this (after connecting etc...)

 

$query  = "SELECT day, month, year, event, type FROM event";
$result = mysql_query($query);

while($row = mysql_fetch_row($result))
{
    $day    = $row[0];
    $month = $row[1];
    $year = $row[2];
    $event = $row[3];
    $type = $row[4];


    echo "Day :$day <br>" .
         "Month : $month <br>" .
         "Year : $year <br><br>";
         "Event : $event <br><br>";
         "type : $type <br><br>";
}

 

Effectively to read the data, assign the variable and then print to screen to show the output.

 

I have tried then using a basic html form with the variable names inserted (in php tags) to write the data into the fields. All this worked ok but the trouble I am having is re-writing the new data to the database.

 

The form has to somehow write back to the db, but only on the chosen row (in this case not by ID but rather type) all of the values input by the user such as year, from say 2010 to 2011 as the new value.

 

The examples I have seen post to an external php file, but don;t give any information on the contents of the php file lol, since I am VERY new to php and mysql, I'm fairly stuck at this point.

 

I understand how to write back to the table by means of hardcoded information, what I'm stuck with is converting the new data into a variable (or similar) and writing the variable to the sql string for writing back.

 

Thanks

Link to comment
Share on other sites

In addendum to blueflog:

 

Instead of mysql_fetch_row() use mysql_fetch_assoc() to get an associative array.  That way if later someone changes the order of the fields fetched it won't affect the rest of the script.  So

$day    = $row[0];

      becomes   

$day = $row['day'];

 

Then to update:

$sql="UPDATE `event` SET `day`=$day, `month`=$month, `year`=$year`, `event`='$event', `type`=$type WHERE `id` = $id";
mysql_query($sql);

 

Of course to do this you'll need to retrieve the ID field in the original SELECT query.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.