Jump to content

SELECT then an UPDATE in same page


gb75

Recommended Posts

Hi

I'm a newbie to php and mysql and i'm having trouble with a query.

Here's the issue

First of all I have a form which when completed updates a table.

Then I have a database page which first runs a SELECT query to retrieve the results of the form... What I need to do is then have the ability to update the retrieved form but this time I need to have an UPDATE query embedded into the code to update the table. So therefore a SELECT and UPDATE query in the same piece of code.

 

Any help greatly appreciated.....Gary

Link to comment
Share on other sites

if i understand correctly, the form is used to pull information from the database, then you can update the information and submit it back into the database. 

 

if that is the case, when selecting the information to edit, also pull out the ID of the entry.  when the information is changed and you want to put it back into the DB with the new values, just update the entry with that same ID with the new values

Link to comment
Share on other sites

something like this for an idea

 

<?php
$action= $_GET["action"];

if($action=="")
{
$action = "add";
}

if ($action=="update")
{
retieve info from database etc sql="select name, age from table etc
$id = $row["id"];
$name =$row["firstname";
}
?>

<html>
<head>
</head> etc etc
<body>

<form name="form" method="post" action="submitform.php">
<input type="hidden" name="action" value="<?=$action?>" />
<input type="hidden" name="id" value="<?=$id?>" />
<input type="text" name="firstname" value="<?=$firstname?>" />
<input type="submit" name="submit" value="submit" />
</form>

<a href="samepagenameasform.php?action=update">Update</a>


</body>
</html>

//submitform.php
<?

$action= $_POST["action"];
$id= $_POST["id"];
$firstname = $_POST["firstname"];


if ($action=="add")
{
sql ="insert into table etc";
}

if ($action=="update")
{
update tablename set firstname=firstname where id =$id";
etc etc
}

Link to comment
Share on other sites

Hi Shadiadiph & Lonewolf217

Thanks for the quick responses -

Yes, the form is used to pull information from the database, then I update the information and submit it back into the database.

 

I have tried to adapt the piece of code Shadiadiph posted but the 1st SELECT retrieves a blank (there is an entry in the table  ;D)

 

I have posted the updated code below

 

 

$conn = mysql_connect("$location","$username","$password");

if (!$conn) die ("Could not connect MySQL");

mysql_select_db($database,$conn) or die ("Could not open database");

 

$action= $_GET["action"];

 

if($action=="")

{

$action = "add";

}

 

if ($action=="update")

{

$query = "SELECT genid, requestorname FROM request_detail_t WHERE genid='16'";

$genid = $row["genid"];

$RequestorName =$row["RequestorName"];

}

 

 

echo '

<form method="post">

<table width="600" border="0" cellpadding="1">

<tr>

<td width="165">Identifier:</td>

<td><input name="genid" type="text" size="52" value="' . $row['genid'] . '"></td>

</tr>

<tr>

<td width="165">Requestor Name:</td>

<td><input name="RequestorName" type="text" size="52" value="' . $row['RequestorName'] . '"></td>

</tr>

<tr>

<td><input name="edit" type="submit" id="add" class="button" value="edit"></td>

</tr>

 

</form>';

 

 

 

 

$action= $_POST["action"];

$genid= $_POST["genid"];

$RequestorName = $_POST["RequestorName"];

 

 

if ($action=="update")

{

$query = "UPDATE request_detail_t SET genid ='$genid', RequestorName='$RequestorName'

            WHERE genid='$genid'";

}

?>

Link to comment
Share on other sites

try this i thought you wanted to insert or update but this will only get where genid-16

 

$conn = mysql_connect("$location","$username","$password"); 
if (!$conn) die ("Could not connect MySQL"); 
mysql_select_db($database,$conn) or die ("Could not open database"); 


$query = "SELECT genid, requestorname FROM request_detail_t WHERE genid='16'";
$genid = $row["genid"];
$RequestorName =$row["RequestorName"];



echo '
<form method="post" action="$php_self">
<table width="600" border="0" cellpadding="1">
<tr> 
<td width="165">Identifier:</td>
<td><input name="genid" type="text" size="52" value="'.$genid.'"></td>
</tr>
<tr> 
<td width="165">Requestor Name:</td>
<td><input name="RequestorName" type="text" size="52" value="'.$RequestorName.'"></td>
</tr>
<tr> 
<td><input name="submit" type="submit" class="button" value="edit"></td>
</tr>

</form>';




$genid= $_POST["genid"];
$RequestorName = $_POST["RequestorName"];


$update = "UPDATE request_detail_t SET genid ='$genid', RequestorName='$RequestorName' WHERE genid='$genid'";
$result = mysql_query($update);

print "Updated";
?>

Link to comment
Share on other sites

Yes, I want to redirect the user to another page upon submission

 

So basically once the update has been entered in the form, the submit button will update the database, but also at the same time it will take the users back to the database page (in this case crd_database.php)

Link to comment
Share on other sites

basically like this

 

$conn = mysql_connect("$location","$username","$password"); 
if (!$conn) die ("Could not connect MySQL"); 
mysql_select_db($database,$conn) or die ("Could not open database"); 


$query = "SELECT genid, requestorname FROM request_detail_t WHERE genid='16'";
$genid = $row["genid"];
$RequestorName =$row["RequestorName"];



echo '
<form method="post" action="$php_self">
<table width="600" border="0" cellpadding="1">
<tr> 
<td width="165">Identifier:</td>
<td><input name="genid" type="text" size="52" value="'.$genid.'"></td>
</tr>
<tr> 
<td width="165">Requestor Name:</td>
<td><input name="RequestorName" type="text" size="52" value="'.$RequestorName.'"></td>
</tr>
<tr> 
<td><input name="submit" type="submit" class="button" value="edit"></td>
</tr>

</form>';




$genid= $_POST["genid"];
$RequestorName = $_POST["RequestorName"];


$update = "UPDATE request_detail_t SET genid ='$genid', RequestorName='$RequestorName' WHERE genid='$genid'";
$result = mysql_query($update);

header("Location: crd_database.php");
exit;
?>

Link to comment
Share on other sites

Thanks for your help yesterday however, I still have an issue.

When coding the header("Location: crd_database.php"); after the SELECT it takes me straight to the crd_database page before the user has had a chance to update the fields from the SELECT.

 

What I want is for the select to populate the form, then have the chance to update the form and then on the  submit of the changes it updates the table again and goes off to the crd_database page.

 

Coding the header("Location: crd_database.php"); at the end gives me an error -

Warning: Cannot modify header information - headers already sent by etc etc

 

Here is the current coding (apologies not sure how to block the code as per previous posts)

 

$conn = mysql_connect("$location","$username","$password");

if (!$conn) die ("Could not connect MySQL");

mysql_select_db($database,$conn) or die ("Could not open database");

 

if (isset($_GET['id'])) {

 

$id = $_GET['id'];

$tor = $_GET['tor'];

}

 

 

$query = "SELECT genid, RequestorName FROM request_detail_t WHERE genid='$id'";

//$genid = $row["genid"];

//$RequestorName =$row["RequestorName"];

header("Location: crd_database.php");

 

echo '

<form method="post" action="'.$_SERVER["PHP_SELF"].'" />

 

<table width="600" border="0" cellpadding="1">

<tr>

<td width="165">Identifier:</td>

<td><input name="genid" type="text" size="52" value="'.$id.'"></td>

</tr>

<tr>

<td width="165">Requestor Name:</td>

<td><input name="RequestorName" type="text" size="52" value="'.$tor.'"></td>

</tr>

<tr>

<td width="100"> </td>

<td> </td>

</tr>

<tr>

<td width="100"> </td>

<td><input name="update" type="submit" id="add" class="button" value="edit" ></td>

</tr>

 

</form>';

 

 

 

 

$genid= $_POST["genid"];

$RequestorName = $_POST["RequestorName"];

 

 

$update = "UPDATE request_detail_t SET genid ='$genid', RequestorName='$RequestorName' WHERE genid='$genid'";

$result = mysql_query($update) or die('Error, update query failed');

 

 

?>

Link to comment
Share on other sites

because you dont have any conditional around the header.

 

you need to have some way of saying this (pseudo code)

 

if (user submitted form) {

  verify contents;

  if (contents verified) {

      header redirect;

  }

}

 

for example, change your form to something like this

 

<form method="post" action="'.$_SERVER["PHP_SELF"]."?action=Submit".'" /> 

 

then you can have this at the top of your page

if(isset($_GET['action']) && $_GET['action']=='Submit') {
   verify the form contents;
   submit to database;
   header("Location: crd_database.php");
}

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.