Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Updating my database using a database button...
Old 10-06-2008, 05:15 PM Updating my database using a database button...
Junior Talker

Posts: 4
Name: Lee Bartlett
Trades: 0
Where am i going wrong here, i dont think im linking to the form2.php page properlly but im not sure how i should do this and keep the delete button working, can anyone help/advise please.

Code:
<?php  require_once("includes/db_connection.php"); ?>
<?php

$name = $_POST['name'];
$email = $_POST['email'];
$location = $_POST['location'];
$type = $_POST['type'];
$buissnes_name = $_POST['buissnes_name'];
$id = $_POST['id'];
?>

<?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>Free or Paid</td><td>Delete</td><td>Update</td></tr>";
while($row = MYSQL_FETCH_ARRAY($res))
{
echo "<tr><td><form action="'.basename($_SERVER['PHP_SELF']).'" method="POST">".$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['type']."</td>";
echo '<td><form action="'.basename($_SERVER['PHP_SELF']).'" method="POST">
<input type="hidden" name="id" value="'.$row['id'].'"><input type="submit" name="button=" id="button" value="Delete Record"></form></td>';
echo "<td>( <a href=form2.php>Update</a> ) </td></tr>";
}
echo "</table><br>"; 
?>

<a href="userform.php">Add a new user.</a>

and the update page, where i would like to do the updates.

Code:
<?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
}
?>
Lee-Bartlett is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-07-2008, 02:53 PM Re: Updating my database using a database button...
Experienced Talker

Posts: 41
Name: Jabis Sevon
Location: Tampere, Finland
Trades: 0
Here about every post I've replied ask about the same question, so I'll continue with the same approach, that has gotten at least a part of script working:

when you first try to reference request methods, like $_POST or $_GET (or $_REQUEST) USE THE isset($_GET/POST/REQUEST) accordingly first... Your first code snippet checks the isset() after you've tried to declare variables from unchecked $_POST's so do for example as follows:

PHP Code:
<?php

$fieldname 
= isset($_POST['fieldname']) ? $_POST['fieldname'] : '';

?>
Now secondly, you're not closing the echo's in your second part you pasted, be sure to close the echo with a semicolon like follows
<?php echo $field; ?>

Thirdly I'd do proper input checking on everything that you query your database against, for example if your id's are always integers, I'd check the id for being numerical like:
PHP Code:
$id = isset($_POST['id']) ? $_POST['id'] : '';
if(!empty(
$id) && is_numeric($id)) { ... continue with your script ... }
else { die(
"id is empty or not numeric"); } 
The next issue I'd figure out would be checking your mysql queries, for proper escaping and usage of quotes, for example:
PHP Code:
//this is yours:
$name $_POST['name'];
$sql "SELECT * from tblbasicform WHERE name = '$name'";

//this is my version:
function escape($s) {
      if (
function_exists('mysql_real_escape_string')) {
         
$s mysql_real_escape_string($s);
      } else {
         
$s mysql_escape_string($s);
      }
      return 
$s;
   }

$name = isset($_POST['name']) ? $_POST['name'] : '';
$name escape($name);
$sql "SELECT * from tblbasicform WHERE name = '".$name."'"
You check out your syntax, correct the issues and let us know how you're progressing, okay?

ps. oh and be sure to not use short tag syntax, because it's not supported on a large number of servers, so instead of <? ?> use <?php ?>
jabis is offline
Reply With Quote
View Public Profile Visit jabis's homepage!
 
Old 10-08-2008, 08:18 AM Re: Updating my database using a database button...
Junior Talker

Posts: 4
Name: Lee Bartlett
Trades: 0
This could be completly wrong, but im rather new to php and didnt quite understand some of the stuff, like the first bit. So heres my second attempt

PHP Code:
<?php  require_once("includes/db_connection.php"); ?>
 

<?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>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['type']."</td>";
echo 
'<td><form action="'.basename($_SERVER['PHP_SELF']).'" method="POST">
<input type="hidden" name="id" value="'
.$row['id'].'"><input type="submit" name="button=" id="button" value="Delete Record"></form></td>';

}
?>

<?php
function escape($s) {
      if (
function_exists('mysql_real_escape_string')) {
         
$s mysql_real_escape_string($s);
      } else {
         
$s mysql_escape_string($s);
      }
      return 
$s;
   }

$name = isset($_POST['name']) ? $_POST['name'] : '';
$name escape($name);
$sql "SELECT * from tblbasicform WHERE name = '".$name."'";


echo 
"<form action=\"form2.php\" method=\"post\">";

while(
$row mysql_fetch_array($res)){

echo 
"<input type=\"hidden\" name=\"name\" value="$row['name']">"
echo "<input type=\"submit\" name=\name\" button=\"submit\" id=\"button\" value=\"Update Record\"></form></td>";

}


echo 
"</tr>";

echo 
"</table><br>"

?>

<a href="userform.php">Add a new user.</a>

Last edited by Lee-Bartlett; 10-08-2008 at 08:20 AM..
Lee-Bartlett is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Updating my database using a database button...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13571 seconds with 12 queries