Jump to content

[SOLVED] Updating db with a button


Lee-Bartlett

Recommended Posts

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
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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']' ");
}

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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);

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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: 
<

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>"
}
?>

Link to comment
Share on other sites

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

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.