Lee-Bartlett Posted October 14, 2008 Share Posted October 14, 2008 Ok here is my page, let me explain the page, its a table where all my db table is listed, on that page it has a delete button, which deletes that record and it is supposed to have an update button, which will update the record. Im not sure what im doing wrong now, I been trying get help, but from more then one place, it gets very confusing, other people have other ideas etc... So heres my code so far. Can anyone help please <?php require_once("includes/db_connection.php"); ?> <html> <title></title> <style type="text/css"> <!-- .style1 { font-size: 36px; color: #FFFFFF; } --> </style> </head> <body> <table width="617" border="1" align="center" cellpadding="5" cellspacing="0" bordercolor="#000000"> <tr> <td colspan="2" bgcolor="#0099FF"><p> </p> <p align="center" class="style1">Nexodom.com</p> <p> </p></td> </tr> <tr> <td width="129" height="318" align="left" valign="top"><p><br> <a href="du.php">Delete/Update</a></p> </td> <td width="482" align="left" valign="top"><p align="center"> </p> <?php if ($_POST['updatebutton']) { // an update button was pressed } if ($_POST['deletebutton']) { $id = mysql_real_escape_string($_POST['id']); $delete = mysql_query("DELETE FROM tblbasicform WHERE id='$id'"); } $sql = "SELECT * from tblbasicform"; $res = mysql_query($sql) or die(mysql_error()); echo "<table border=1 align=centre>"; echo "<tr><td>id</td> <td>Name</td><td>Email</td><td>Buissnes Name</td><td>Location</td><td>Latitude</td><td>Longitude</td><td>Free or Paid</td><td>Delete</td><td>Update</td></tr>"; while ($row=mysql_fetch_assoc($res)) { echo "<tr><td>".$row['id']."</td>"; echo "<td>".$row['name']."</td>"; echo "<td>".$row['email']."</td>"; echo "<td>".$row['buissnes_name']."</td>"; echo "<td>".$row['location']."</td>"; echo "<td>".$row['latitude']."</td>"; echo "<td>".$row['longitude']."</td>"; echo "<td>".$row['type']."</td>"; echo "<td>"<form action="form2.php'.basename($_SERVER['PHP_SELF']).'" method="POST"> echo "<input type=\"hidden\" value=\"{$row['id']}\" name=\"id\" />"; echo "<input type=\"submit\" value=\"update\" name=\"updatebutton\" />"; echo "<input type=\"submit\" value=\"delete\" name=\"deletebutton\" />"; echo "</form></td>"; } echo "</tr>"; echo "</table><br>"; ?> <a href="adminuserform.php">Add a new user.</a> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p></td> </tr> <tr> <td height="20" colspan="2" bgcolor="#0099FF"> </td> </tr> </table> </body> </html> And the page which makes the update bit work. <?php require_once("includes/db_connection.php"); $name = $_POST['name']; echo "<form action=\"form3.php\" method=\"post\">"; $sql = "SELECT * from tblbasicform WHERE name = '$name'"; $res = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_array($res)){ $id = $row['id']; $name = $row['name']; $email = $row['email']; $buissnes_name = $row['buissnes_name']; $location = $row['location']; $type = $row['type']; ?> Input Name: <input type="text" name="name" value="<? echo $name ?>" /><br /> Input Email: <input type="text" name="email" value="<? echo $email?>" /><br /> Input Buissnes Name: <input type="text" name="buissnes_name" value="<? echo $buissnes_name?>" /><br /> Input Location: <input type="text" name="location" value="<? echo $location?>" /><br /> Input Free or Paid: <input type="text" name="type" value="<? echo $type?>" /><br /> <input type="hidden" name="id" value="<? echo $id?>"> <input type="submit" name="submit" value="Update Data" /> </form> <?php } ?> Quote Link to comment Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 So what's the problem, besides: Im not sure what im doing wrong now Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted October 14, 2008 Author Share Posted October 14, 2008 Oh woops, its not working, i cant get the update button to work. Im not sure how to code it in a sense so i need some help or tips on how to get this to work. Atm i got no t varible, but i think thats a small part of my problem Quote Link to comment Share on other sites More sharing options...
revraz Posted October 14, 2008 Share Posted October 14, 2008 In my opinion, you are going about it the wrong way. Why are you using a FORM to do this? Just create a Link with the ID number. On your page use $_GET to determine the Action (delete/update) and also to read in the ID number of the row. Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted October 14, 2008 Author Share Posted October 14, 2008 I am very new to php so im just trying to piece together the stuff i know to try make it work. Can you give me a basic example and ill try work into a page and ill try use the basics to make it work in mine, good way to learn but not the easy way. Quote Link to comment Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 Well I guess you haven't done anything so far by looking at your code. Why can't you just update after the isset function in the other script? if ($_POST['updatebutton']) { mysql_query("UPDATE your_table SET email = '$_POST['email']', name = '$_POST['name']', etc... WHERE id = '$_POST['id']' "); } Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted October 14, 2008 Author Share Posted October 14, 2008 Forgot to show form 3, do you mean somthing like this?? <?php require_once("includes/db_connection.php"); $name = $_POST['name']; $email = $_POST['email']; $location = $_POST['location']; $type = $_POST['type']; $buissnes_name = $_POST['buissnes_name']; $id = $_POST['id']; $query = "UPDATE tblbasicform SET name='$name',location='$location',email='$email', buissnes_name='$buissnes_name', type='$type' WHERE id='$id'"; $res = mysql_query($query) or die("Error: " . mysql_error()); echo "Database Updated<br>"; echo "Name $name<br>"; echo "Email $email<br>"; echo "Location $location<br>"; echo "Buissnes_name $buissnes_name<br>"; echo "Type $type<br>"; echo "ID $id<br>"; ?> <a href="updatedelete.php">Start Over</a> Quote Link to comment Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 Yeah. I don't understand the problem. Do you get any errors? What gets put in the DB, anything? Do you have error displaying turned on? ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment Share on other sites More sharing options...
Andy17 Posted October 14, 2008 Share Posted October 14, 2008 Forgot to show form 3, do you mean somthing like this?? <?php require_once("includes/db_connection.php"); $name = $_POST['name']; $email = $_POST['email']; $location = $_POST['location']; $type = $_POST['type']; $buissnes_name = $_POST['buissnes_name']; $id = $_POST['id']; $query = "UPDATE tblbasicform SET name='$name',location='$location',email='$email', buissnes_name='$buissnes_name', type='$type' WHERE id='$id'"; $res = mysql_query($query) or die("Error: " . mysql_error()); echo "Database Updated<br>"; echo "Name $name<br>"; echo "Email $email<br>"; echo "Location $location<br>"; echo "Buissnes_name $buissnes_name<br>"; echo "Type $type<br>"; echo "ID $id<br>"; ?> <a href="updatedelete.php">Start Over</a> Yeah, I was about to suggest something like that. I never updated more than one column at a time but I guess that would work for ya. Just remember to check if the button has been pressed before running the update query. Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted October 14, 2008 Author Share Posted October 14, 2008 Maybe if i explain my scenario first, ok, I would like to have an update button on the far right of my table where all of my database is echoed. I would like to get that update button, when clicked to update that row where the update button is. For example... Name = Bob, when i click the update button i would like it to give me the option of updating bob. Atm im getting unexpected t varible on 59 but im not sure it will work if that is fixxed. Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted October 14, 2008 Author Share Posted October 14, 2008 I can get form 2 and 3 to work, but it isnt getting the user data in there to update it, i used to have a drop down box which id pick a user from, but now, its seems more time saving if its in each row of the table Quote Link to comment Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 What you should do is submit it to itself but I don't want to open a can of worms. Unless someone has a better idea. The way you have it I would debug first like this: Echo the $_POSTS in form3.php to make sure you're retrieving the values. Because if you hit 'update' and you get to the form3.php then you're submitting it correctly. Also, you should move the form tags down to HTML part instead of echoing it out in PHP. </pre> <form action="form3.php" method="post"> Input Name: Input Email: < Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted October 14, 2008 Author Share Posted October 14, 2008 So in my first piece of code, i should move all my form down into the html section ? Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted October 14, 2008 Author Share Posted October 14, 2008 There is a problem putting stuff outside the php tags, when the php genereates, it does a new form for each entry in the database, putting it in html tags stops that. this is what i have so far, i have unexpected t echo on line 60 <?php require_once("includes/db_connection.php"); ?> <html> <title></title> <style type="text/css"> <!-- .style1 { font-size: 36px; color: #FFFFFF; } --> </style> </head> <body> <table width="617" border="1" align="center" cellpadding="5" cellspacing="0" bordercolor="#000000"> <tr> <td colspan="2" bgcolor="#0099FF"><p> </p> <p align="center" class="style1">Nexodom.com</p> <p> </p></td> </tr> <tr> <td width="129" height="318" align="left" valign="top"><p><br> <a href="du.php">Delete/Update</a></p> </td> <td width="482" align="left" valign="top"><p align="center"> </p> <?php if(isset($_POST['id'])) { $id = $_POST['id']; $delete = mysql_query("DELETE FROM tblbasicform WHERE id='$id'"); } $sql = "SELECT * from tblbasicform"; $res = mysql_query($sql) or die(mysql_error()); echo "<table border=1 align=centre>"; echo "<tr><td>id</td> <td>Name</td><td>Email</td><td>Buissnes Name</td><td>Location</td><td>Latitude</td><td>Longitude</td><td>Free or Paid</td><td>Delete</td><td>Update</td></tr>"; while($row = MYSQL_FETCH_ARRAY($res)) { echo "<tr><td>".$row['id']."</td>"; echo "<td>".$row['name']."</td>"; echo "<td>".$row['email']."</td>"; echo "<td>".$row['buissnes_name']."</td>"; echo "<td>".$row['location']."</td>"; echo "<td>".$row['latitude']."</td>"; echo "<td>".$row['longitude']."</td>"; echo "<td>".$row['type']."</td>"; echo '<form action="'.basename($_SERVER['PHP_SELF']).'" method="POST"> <input type="hidden" name="id" value="'.$row['id'].'">'; echo "<td> <input type=\"submit\" value=\"update\" name=\"updatebutton\" > </td>" ( THIS IS LINE 60) echo "<td><input type=\"submit\" value=\"delete\" name=\"deletebutton\" > </td>" echo "</form></td>" echo "</tr>" } ?> </table><br> <a href="adminuserform.php">Add a new user.</a> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p></td> </tr> <tr> <td height="20" colspan="2" bgcolor="#0099FF"> </td> </tr> </table> </body> </html> Quote Link to comment Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 You're problem is line 59. You write in HTML instead of PHP. Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted October 14, 2008 Author Share Posted October 14, 2008 I put echo on line 60 and didnt work, it just stays greyed out, Quote Link to comment Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 I told you it's line 59 not 60. Change to: echo ""; (THIS IS LINE 59) echo " " ( THIS IS LINE 60) Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted October 14, 2008 Author Share Posted October 14, 2008 now i am getting Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/nexodom/public_html/website/admin/du.php on line 57 on this line echo "<input type=\"hidden\" name=\"id\" value=\"'.$row['id'].'\">"; happend before because i closed it i think Quote Link to comment Share on other sites More sharing options...
Andy17 Posted October 14, 2008 Share Posted October 14, 2008 <?php echo '<input type="hidden" name="id" value="' . $row['id'] . '">'; ?> Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted October 14, 2008 Author Share Posted October 14, 2008 now im getting Parse error: syntax error, unexpected '<' in /home/nexodom/public_html/website/admin/du.php on line 57 the relvent bits of code, the post in method is in black and i dont think its ment to stay that colour in my text editor, its ment to be grey <?php echo <form action="'.basename($_SERVER['PHP_SELF']).'" method="POST"> echo '<input type="hidden" name="id" value="' . $row['id'] . '">'; echo "<td> <input type=\"submit\" value=\"update\" name=\"updatebutton\" > </td>" echo "<td> <input type=\"submit\" value=\"update\" name=\"updatebutton\" > </td>" echo "<td><input type=\"submit\" value=\"delete\" name=\"deletebutton\" > </td>" echo "</form></td>" echo "</tr>" } ?> Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 14, 2008 Share Posted October 14, 2008 Your syntax is all messed up. You are missing quotes and semicolons everywhere... like your first echo doesn't start or end with a quote....you don't have a semicolon after each statement... Quote Link to comment Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 You would be better off making these lines HTML rather than PHP because you have so much more HTML. You need to look at some basic syntax for this because your code a all over the place. Try it, post it, and we'll correct it. echo </pre> <form action="'.basename(%24_SERVER%5B'PHP_SELF'%5D).'" method="POST"> echo ''; echo " " echo " " echo "" echo "</form>"<br>e Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted October 14, 2008 Author Share Posted October 14, 2008 maq there is only one problem with it being html, when i generate a new row in my database, that doesnt get the html in it, so it is pointless doing it in html, cause i need an update and a delete button in each row. Unless there is a way to do this with html then ofc ill happily do it Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 14, 2008 Share Posted October 14, 2008 Have you corrected your syntax errors? Post what you came up with... Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted October 14, 2008 Author Share Posted October 14, 2008 Im a little confused on what needs to be changing, is the html bits or the sql Quote Link to comment 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.