Jump to content

Looking for code: Simple PHP online updateable list


jeffery1493

Recommended Posts

Hi All,  :shy:

 

Does anybody know of, or have seen online a code block for a simple online PHP list/form tied to a MYSQL table?

 

Basically all I want to do is create a Christmas list online.   

I don't want any bells and whistles that will confuse the fam.....they are very low tech......... just a straight row of fields down the page linked to a MYSQL table: record, record, record, maybe 2 + columns (name, item, and how much they want it 1-5)  and an update button.

 

I looked for quite awhile but most everything is vast overkill.

 

 

 

Okay.... I think this is what I wanted:

 

Pull selected fields, all data existing in single table, and display online as editable form  (display.php)

<?php

// connect to the database

mysql_connect(hostname,username,password);

 

// select the database

mysql_select_db(database) or die("Unable to select database");

 

// run the query and put the results in an array variable called $result

$result = mysql_query("SELECT * FROM table ORDER BY course");

 

// start a counter in order to number the input fields for each record

$i = 0;

 

// open a form

print "<form name='namestoupdate' method='post' action='update.php'>\n";

 

// start a loop to print all of the courses with their book information

// the mysql_fetch_array function puts each record into an array. each time it is called, it moves the array counter up until there are no more records left

while ($books = mysql_fetch_array($result)) {

 

// assuming you have three important columns (the index (id), the course name (course), and the book info (bookinfo))

  // start displaying the info; the most important part is to make the name an array (notice bookinfo[$i])

  print "<input type='hidden' name='id[$i]' value='{$books['id']}' />";

  print "<p>{$books['course']}: <input type='text' size='40' name='bookinfo[$i]' value='{$books['bookinfo']}' /></p>\n";

 

// add 1 to the count, close the loop, close the form, and the mysql connection

++$i;

}

print "<input type='submit' value='submit' />";

print "</form>";

mysql_close();

?>

 

 

And then update routine (referred to as: update.php)

<?php

// connect to the database and select the correct database

mysql_connect(hostname,username,password);

mysql_select_db(database) or die("Unable to select database");

 

// find out how many records there are to update

$size = count($_POST['bookinfo']);

 

// start a loop in order to update each record

$i = 0;

while ($i < $size) {

// define each variable

$bookinfo= $_POST['bookinfo'][$i];

$id = $_POST['id'][$i];

 

// do the update and print out some info just to provide some visual feedback

// you might need to remove the single quotes around the field names, for example bookinfo = '$bookinfo' instead of `bookinfo` = '$bookinfo'

$query = "UPDATE table SET `bookinfo` = '$bookinfo' WHERE `id` = '$id' LIMIT 1";

mysql_query($query) or die ("Error in query: $query");

print "$bookinfo<br /><br /><em>Updated!</em><br /><br />";

++$i;

}

mysql_close();

?>

 

Well thats the basics, for those of us who are PHP impaired.

I'd say, "Don't quit your day job, Jeff", except its already been outsourced, lol.

 

 

 

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

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