Jump to content
Stef's Coding Community
Sign in to follow this  
edoplaza

Trouble updating records

Recommended Posts


color
<?php echo $row_Recordset1['color']; ?> 



<?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
">First
<?php } // Show if not first page ?>
<?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
">Previous
<?php } // Show if not first page ?>
<?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
">Next
<?php } // Show if not last page ?>
<?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
">Last
<?php } // Show if not last page ?>

Records <?php echo ($startRow_Recordset1 + 1) ?> to <?php echo min($startRow_Recordset1 + $maxRows_Recordset1, $totalRows_Recordset1) ?> of <?php echo $totalRows_Recordset1 ?>
Edited by edoplaza

Share this post


Link to post
Share on other sites

I don't have the time to look at this in detail at the moment, but I know that it has to do with these lines:

 

$query_Recordset1 = "SELECT * FROM colores";
$Recordset1 = mysql_query($query_Recordset1, $conn2) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

 

Rather than selecting all of the records (SELECT *) you need to get the value of the query from the URL (using $_GET[]) and get that specific data. Probably something along the lines of "SELECT * FROM colores WHERE id=(URL value here)"

 

More info: http://www.tizag.com/mysqlTutorial/mysqlwhere.php

 

One comment... When you click a link to edit a record, this is the url that I get: http://www.puntabahareque.com/enterupdate/update.php?1=1 or http://www.puntabahareque.com/enterupdate/update.php?2=2. I think it would make a lot more sense to have "id=1" or "id=2" rather than "1=1" or "2=2".

 

To do that, you'd need to change this line in detail.php:

 

Update

 

to

 

Update

Share this post


Link to post
Share on other sites

I'm still struggling with this code.

 

I've tried this:

 

 

$value= $_GET['id'];

$query_Recordset1 ="SELECT * FROM colores WHERE id='value'";

 

 

and all I get is blank results:

 

Id:

Color:

Cosa:

Numero:

 

I guess the id value is not passing correctly from the detail.php to update.php.

 

It shouldn't be that hard, though.... It's a simple insert/update record form

 

Eduardo

Share this post


Link to post
Share on other sites

hi

$value= $_GET['id'];
$query_Recordset1 ="SELECT * FROM colores WHERE id='value'";

 

I think you missed the $ in 'value'.

if this is not an example then this one below is right.

$value= $_GET['id'];
$query_Recordset1 ="SELECT * FROM colores WHERE id='".$value."'";

Edited by bishwadeep

Share this post


Link to post
Share on other sites

Thanks bishwadeep,

 

I copied and pasted your code, but I still get blank results

 

 

Could you point me to a insert/update record tutorial??

 

I just need this:

 

a) A simple for where I can insert data

B) A page with all data displayed

c) A form where I can update the data for each record

 

I already have a) and B) I just need c)

 

Thanks

 

Eduardo

Share this post


Link to post
Share on other sites

I'll see if I can put together a basic tutorial for you.

 

One question: how much of this are you actually coding, and how much of this is Dreamweaver doing for you? Just curious, since I see a lot of Dreamweaver generated code in your example code.

Share this post


Link to post
Share on other sites

Hello Benjamin,

 

You are right; you see a lot of Dreamweaver because I am a complete newbie in php. I used to know a lot of Action Script but I haven't had the time to learn php correctly. Now, my ignorance is giving me a hard time and I'm paying for it... and I know it's very amateurish of me to ask for big pieces of code in this forum, so I apologize for that.

 

When you start using a software like Dreamweaver you think the built-in features are going to do everything for you (spry, wizards, etc.), but then, when you need to code an email form, or other more complicated things, you are lost.

 

I think next month I'll start learning php in the correct way (I already bought some books) so I won't have to ask for help every time.

 

Now, I really need to make this work, for I have to build a web site very soon.

 

Thank you,

 

Eduardo

 

N.B. Not an excuse, but I am not a professional programmer, I am a professional musician who loves computers?

Share this post


Link to post
Share on other sites

Nothing wrong with being a beginner and asking for help. I do agree -- WYSIWYG editors like Dreamweaver do a good job with visual elements, but for actual programming you kinda need to know what you are doing.

 

As for resources for learning PHP whenever you get to that, here are a couple:

-- http://www.killerphp.com, obviously

-- http://phpvideotutorials.com (both the free and paid videos are good quality -- that's partially how I got started)

-- http://blog.themeforest.net/?s=diving+into+php+day (I've heard a lot of good things about this free video screencast series. I plan to do it at some point myself.)

-- http://www.tizag.com/ has good PHP/MySQL tutorials as well

Share this post


Link to post
Share on other sites

(Anyone with PHP knowledge is welcome to comment on the code. If there are issues I haven't noticed, please let me know. Do realize that it is intended for beginners, so I didn't want to do anything too advanced that might lead to confusion. Yes, I realize I could use OOP, or could separate some of these out into methods, etc. etc.)

 

OK... Here's some code for you to play with. It's a basic system that allows you to:

-- view existing records

-- edit existing records

-- delete existing records

-- add new records

 

Basically, just imagine that you are in charge of a sports team, and you want to keep a list of all your player's contact information. The code I've created could be a starting point for that (it only includes fields for their first name/last name, but could obviously could be expanded to use more fields)

 

This is just a starting point for you, and you'll need to adapt it if you want it to work in your project. I know it may seem a lot to understand at first, but read all the comments in the code -- I try to explain what I am doing step by step. I'm also happy to help if you have questions.

 

How to create a system that allows a user to add/edit/remove data in a database seems to be a commonly asked topic, so I may adapt this into an actual tutorial at some point in the future.

 

DATABASE:

-- You'll need to create a database (I named mine 'records' but it can be changed) using PHPMyAdmin

-- Save the included sql file on your desktop as a .txt file

-- Once you've created the database, make sure the database is selected, then click the "import" tab

-- Select the .txt file on your desktop, and import it into your database. PHPMyAdmin will create all of the necessary tables/import some test data for you to play with

 

SQL file:

--
-- Table structure for table `players`
--

CREATE TABLE `players` (
 `id` tinyint(4) NOT NULL auto_increment,
 `firstname` varchar(32) NOT NULL,
 `lastname` varchar(32) NOT NULL,
 PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `players`
--

INSERT INTO `players` VALUES(1, 'Bob', 'Baker');
INSERT INTO `players` VALUES(2, 'Tim', 'Thomas');
INSERT INTO `players` VALUES(3, 'Rachel', 'Roberts');
INSERT INTO `players` VALUES(4, 'Sam', 'Smith');

 

Save these php files all in the same folder in a place where you can run them using your server (I'm assuming you are using something like WAMP for the server? I'm not sure if Dreamweaver includes something like that by default.)

 

connect-db.php

/* 
   CONNECT-DB.PHP
   Allows PHP to connect to your database
*/

   // Database Variables (edit with your own server information)
   $server = 'localhost';
   $user = 'root';
   $pass = 'root';
   $db = 'records';

   // Connect to Database
   $connection = mysql_connect($server, $user, $pass) 
       or die ("Could not connect to server ... \n" . mysql_error ());
   mysql_select_db($db) 
       or die ("Could not connect to database ... \n" . mysql_error ());


?>

 

view.php

>


View Records



/* 
   VIEW.PHP
   Displays all data from 'players' table
*/

   // connect to the database
   include('connect-db.php');

   // get results from database
   $result = mysql_query("SELECT * FROM players") 
       or die(mysql_error());  

   // display data in table
   echo "</pre>
<table border="1" cellpadding="10">";
   echo "ID First Name Last Name  ";

   // loop through results of database query, displaying them in the table
   while($row = mysql_fetch_array( $result )) {

       // echo out the contents of each row into a table
       echo "";
       echo '' . $row['id'] . '';
       echo '' . $row['firstname'] . '';
       echo '' . $row['lastname'] . '';
       echo 'Edit';
       echo 'Delete';
       echo ""; 
   } 

   // close table>
   echo "</table>";<br>?><br><p>Add a new record</p>
<br><br><br

 

new.php

>/* 
   NEW.PHP
   Allows user to create a new entry in the database
*/

   // connect to the database
   include('connect-db.php');

   // check if the form has been submitted. If it has, process the form and save it to the database
   if (isset($_POST['submit']))
   {    
       // get form data, making sure it is valid
       $firstname = htmlspecialchars($_POST['firstname']);
       $lastname = htmlspecialchars($_POST['lastname']);

       // save the data to the database
       mysql_query("INSERT players SET firstname='$firstname', lastname='$lastname'")
           or die(mysql_error()); 

       // once saved, redirect back to the view page
       header("Location: view.php");
   }
   else
   // if the form hasn't been submitted, display the form
   {
       ?>


New Record


</pre>
<form action="" method="post">

First Name: 

Last Name: 





</form>    <br><br><br>   }<br>?&g

 

edit.php

>/* 
   EDIT.PHP
   Allows user to edit specific entry in database
*/

   // connect to the database
   include('connect-db.php');

   // check if the form has been submitted. If it has, process the form and save it to the database
   if (isset($_POST['submit']))
   {    
       // confirm that the 'id' value is a valid integer before getting the form data
       if (is_numeric($_POST['id']))
       {
           // get form data, making sure it is valid
           $id = $_POST['id'];
           $firstname = htmlspecialchars($_POST['firstname']);
           $lastname = htmlspecialchars($_POST['lastname']);

           // save the data to the database
           mysql_query("UPDATE players SET firstname='$firstname', lastname='$lastname' WHERE id='$id'")
               or die(mysql_error()); 

           // once saved, redirect back to the view page
           header("Location: view.php");
       }
       else
       {
           // if the 'id' isn't valid, display an error
           echo 'Error!';
       }
   }
   else
   // if the form hasn't been submitted, get the data from the db and display the form
   {

       // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
       if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
       {
           // query db
           $id = $_GET['id'];
           $result = mysql_query("SELECT * FROM players WHERE id=$id")
               or die(mysql_error()); 
           $row = mysql_fetch_array($result);

           // check that the 'id' matches up with a row in the databse
           if($row)
           {

               // get data from db
               $firstname = $row['firstname'];
               $lastname = $row['lastname'];

               // display the form
               ?>


Edit Record


</pre>
<form action="" method="post">



ID: 
First Name: 

Last Name: 





</form>    <br><br><br>           }<br>           else<br>           // if no match, display result<br>           {<br>               echo "No results!";<br>           }<br>       }<br>       else<br>       // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error<br>       {<br>           echo 'Error!';<br>       }<br>   }<br

 

delete.php

/* 
   DELETE.PHP
   Deletes a specific entry from the 'players' table
*/

   // connect to the database
   include('connect-db.php');

   // check if the 'id' variable is set in URL, and check that it is valid
   if (isset($_GET['id']) && is_numeric($_GET['id']))
   {
       // get id value
       $id = $_GET['id'];

       // delete the entry
       $result = mysql_query("DELETE FROM players WHERE id=$id")
           or die(mysql_error()); 

       // redirect back to the view page
       header("Location: view.php");
   }
   else
   // if id isn't set, or isn't valid, redirect back to view page
   {
       header("Location: view.php");
   }

?>

Share this post


Link to post
Share on other sites

Benjamin,

 

Thank you very much, this is exactly what I was looking for. I promise I'll try to LEARN php and won't ask for pieces of code anymore...

 

Best regards,

 

Eduardo

Share this post


Link to post
Share on other sites

Just one last question,

 

What would be the best way to link all this to a css style sheet? I notice that in the view.php tables are not shown as a html format but inside a php code.

 

Thanks Eduardo

Share this post


Link to post
Share on other sites

go to the view.php page and then do a view source to see the html code produced by the php file.

CSS can be embedded in the head of the page or in an external file sheet and linked just like an html page.

Share this post


Link to post
Share on other sites

Glad to hear that it is working for you!

 

Just one last question,

 

What would be the best way to link all this to a css style sheet? I notice that in the view.php tables are not shown as a html format but inside a php code.

 

Just look for the

tags within the php files, and add a link to the CSS file there.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...