cw131 Posted February 24, 2012 Share Posted February 24, 2012 Hello all, I am new to php coding and have a couple of problems with editing records in my database! I have two files below one test.php and edit.php. In the test.php the code outputs the records into a table. The problem is with the edit link as when it is selected I wish to be able to edit a record by a form, which is on edit.php. I am trying bring up the movie's information on the form to be edited. Currently on the form i get; Movie: movie Genre: Genre Year: year Any ideas how I can edit the record and then return to the test.php page? test.php <html> <body style="background-color:#669999;"> <table width="490"border=0><tr> <td colspan="2" style="background-color:#FFA500;"> <div id="header" <h3 style="color:black">This is my first web-page! Below is a database of some of my favourite movies! </h3> </td> </tr> <?php //connecting to server $con = mysql_connect("localhost","root","NYOXAkly"); if (!$con) { die('could not connect: ' . mysql_error()); } //selecting movie database mysql_select_db("my_mov",$con); //Check if add button is active, start this if(isset($_REQUEST['add'])) { echo "<meta http-equiv=\"refresh\"content=\"0;URL=form.php\">"; } $result = mysql_query("SELECT * FROM Films ORDER BY filmID"); ?> <!-------------------------------creating table------------------------------------------------------------------------------------> <table width="490" border="0" cellspacing="1" cellpadding="0"> <tr> <td> <form name="test1" method="post" action="test.php"> <table width="490" border="10" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"><tr> <td bgcolor="#FFFFFF"></td> <td align="center" colspan="6" bgcolor="#FFFFFF">Movie Database</td></tr> <td align="center" bgcolor="#FFFFFF">filmID</td> <td align="center" bgcolor="#FFFFFF">Movie</td> <td align="center" bgcolor="#FFFFFF">Genre</td> <td align="center" bgcolor="#FFFFFF">Year</td> <td align="center" bgcolor="#FFFFFF">Edit</td> <td align="center" bgcolor="#FFFFFF">Delete</td> </tr> <?php while($rows=mysql_fetch_array($result)) { ?> <tr> </td> <td bgcolor="#FFFFFF"><? echo $rows['filmID']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['movie']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['genre']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['year']; ?></td> <td bgcolor="#FFFFFF"> <a href="edit.php?filmID=<?php echo 'filmID';?>">Edit</a> <td bgcolor="#FFFFFF"> <a href="delete.php?filmID=<?php echo 'filmID';?>">Delete</a> </td> </tr> <?php } echo print_r(error_get_last()); mysql_close(); ?> <!--add button--> <tr> <td colspan="15" align="left" bgcolor="#FFFFFF"> <input name='add' type="submit" filmID="add" value="Add A New Record" action="form.php?"> </td> </tr> </table> </form> <br/> By C.M.D.W <br/> <?php echo date("Y/m/d") . "<br />"; ?> </body> </html> edit.php <html> <body style="background-color:#669999;"> <!------------------Creates a form ----------------------> <br /> <form action="" method="post"> <fieldset> <legend>Enter your movies into database here!</legend> Movie: <input type ="text" name="movie" value="<?php echo 'movie';?>"> <br /> Genre: <input type ="text" name="genre" value="<?php echo 'genre';?>"/> <br /> Year: <input type ="text" name="year" value="<?php echo 'year';?>"/> <br /> <input type="submit" name="name" value="Submit" /> </fieldset> </form> <?php //connecting to server $con = mysql_connect("localhost","root","NYOXAkly"); if (!$con) { die('could not connect: ' . mysql_error()); } //selecting movie database mysql_select_db("my_mov",$con); if (isset($_POST['submit'])) { // confirm that the 'id' value is a valid integer if (is_numeric($_POST['filmID'])) // get form data $filmID = $_POST['filmID']; $movie = mysql_real_escape_string(htmlspecialchars($_POST['movie'])); $genre = mysql_real_escape_string(htmlspecialchars($_POST['genre'])); $year = mysql_real_escape_string(htmlspecialchars($_POST['year'])); // check that fields are filled in if ($movie == '' || $genre == '' || $year == '') { // generate error message $error = 'ERROR: Please fill in all required fields!'; } else { // save the data to the database } mysql_query("UPDATE players SET movie='$movie', genre='$genre', year='$year' WHERE filmID='$filmID'") or die(mysql_error()); // once saved, redirect back to the view page header("Location: test.php"); } } if (isset($_GET['filmID']) && is_numeric($_GET['filmID']) && $_GET['filmID'] > 0) { // query db $id = $_GET['filmID']; $result = mysql_query("SELECT * FROM Films WHERE filmID=$FilmID") 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 $movie = $row['movie']; $genre = $row['genre']; $year = $row['year']; }} ?> <br/> <br/> <a href="test.php">Return To Home Page</a> <br/> <br/> By C.M.D.W <br/> <?php echo date("Y/m/d") . "<br />"; ?> </body> </html> Thanks Chris Quote Link to comment https://forums.phpfreaks.com/topic/257686-editing-records-with-phpmysql/ Share on other sites More sharing options...
Brentatechnologies Posted February 24, 2012 Share Posted February 24, 2012 As a NEWBIE PHP coder myself I'd still like to have a shot just to test myself...If this turns out to be wrong I will run in shame but nevertheless practice makes perfect... Alright, So instantly I'm going to suggest the following You said you have the following printing "Movie: movie Genre: Genre Year: year" Now I read that in your edit.php you have <form action="" method="post"> <fieldset> <legend>Enter your movies into database here!</legend> Movie: <input type ="text" name="movie" value="<?php echo 'movie';?>"> <br /> Genre: <input type ="text" name="genre" value="<?php echo 'genre';?>"/> <br /> Year: <input type ="text" name="year" value="<?php echo 'year';?>"/> <br /> <input type="submit" name="name" value="Submit" /> </fieldset> </form> Is the problem this? your not including the $_POST['movie'] ------ maybe it needs the $_POST part being part of the code? $filmID = $_POST['filmID']; $movie = mysql_real_escape_string(htmlspecialchars($_POST['movie'])); $genre = mysql_real_escape_string(htmlspecialchars($_POST['genre'])); $year = mysql_real_escape_string(htmlspecialchars($_POST['year'])); Other than that....I've got nothing else...i'll be interested in what the problem is (it'll help my learning capabilities of reading code) Sorry if this is wrong Brent. Quote Link to comment https://forums.phpfreaks.com/topic/257686-editing-records-with-phpmysql/#findComment-1320731 Share on other sites More sharing options...
cw131 Posted February 27, 2012 Author Share Posted February 27, 2012 Hello, Nope still not solved. I'm trying to get the page edit.php to output a form which will have the selected record's data in the form so it can be edited. So for example in the table i would select the edit link below to edit movie a filmID movie genre year delete edit # movie a genre a year a delete link edit link Which should then bring up the record in the edit.php form; "form"; movie: movie a genre: genre a year: year a submit Then after pressing the submit button the record should be updated and should be returned to the test.php page. This is my aim but it still doesn't work. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/257686-editing-records-with-phpmysql/#findComment-1321612 Share on other sites More sharing options...
batwimp Posted February 27, 2012 Share Posted February 27, 2012 This line: <a href="edit.php?filmID=<?php echo 'filmID';?>">Edit</a> Isn't going to do what you want it to. Clicking on this link won't send any of the POST data, only GET data, to edit.php, and the only variable you will get there is filmID ($_GET['filmID']). Your form is being sent via POST, so you need to click on a submit button within the form in order to get the information to go via POST, but it looks like your submit button is for adding a new record. If you want to do multiple things from a single form (just my opinion), you should use anchors and the GET method. So your this part of your code would be the same (with the correct variable listed): <a href="edit.php?filmID=<?php echo $rows['filmID'];?>">Edit</a> Then in your edit.php file, call the database and load up the record based on the filmID, and use that to populate the form. Do the same with delete.php, just use the link to get to the file, then query the database with the filmID to load up the form. Remember, when passing variables this way through the URL, you need to use $_GET[] to pull the filmID down into your script. I'm kind of babbling. I hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/257686-editing-records-with-phpmysql/#findComment-1321702 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.