Jump to content

Stuck On A Two Form Processing Page Which Processes The First Form But Not The Second


jlindsey

Recommended Posts

I know what the issue is, I just can't figure out the solution. The problem is I have a page which searches for users in a database by their User ID. That is the first form that is processed and it works great. It finds the user and outputs the correct information. The second form is then supposed to let users enter data and then update the database. Only it doesn't update the database at all. I know the issue has something to do with how I'm processing two forms on one page. I just can't seem to get the second form to work and update the database. I think I am this close to getting this worked out I just need a little hope with the script below.

 

<?php

 

 

$form = "<form id='search' action='./purchasegifts.php' method='get'>

 

<label>User ID:

<input type='text' name='keyname' />

</label>

 

<input type='submit' name='viewgift' value='Search Gift Registry' />

</form>";

 

echo $form;

echo "<br></br>";

 

$searchTerm = trim($_GET['keyname']);

 

 

//check whether the name parsed is empty

if($searchTerm == "")

{

echo "";

exit();

}

 

 

//database connection info

$host = "localhost"; //server

$db = "users"; //database name

$user = "######"; //dabases user name

$pwd = "#######"; //password

 

//connecting to server and creating link to database

$link = mysqli_connect($host, $user, $pwd, $db);

 

//MYSQL search statement

$sql = "SELECT * FROM users WHERE userid='$searchTerm'";

$myData = mysqli_query($link, $sql);

$myData2 = mysqli_query($link, $sql);

$myData3 = mysqli_query($link, $sql);

 

 

if(mysqli_num_rows($myData) >= 1)

{

echo '<table align="center" cellspacing="0" cellpadding="5" border="2">

<tr>

 

<td align="center"><b>First Name</b></td>

<td align="center"><b>Last Name</b></td>

</tr>';

while($row = mysqli_fetch_array($myData2))

{

echo "<tr>";

echo "<td>" . $row['FirstName'] . "</td>";

echo "<td>" . $row['LastName'] . "</td>";

echo "</tr>";

}

echo "</table>";

echo "<br></br>";

 

 

if (isset($_POST['updatebtn'])){

$UpdateQuery = "UPDATE users SET purchased_gift='$_POST[purchased_gift]' WHERE username='$username'";

mysql_query($UpdateQuery, $link);

};

echo '<form id="update" method="post" action="purchasegifts.php">';

// Table header.

echo '<table align="center" cellspacing="0" cellpadding="5" border="2">

<tr>

 

<td align="center"><b>Gifts</b></td>

<td align="center"><b>Purchase Gifts</b></td>

 

</tr>';

 

{

 

while ($row = mysqli_fetch_array($myData3))

 

{

echo "<tr>";

echo "<td>" . $row['Gift'] . "</td>";

echo '

 

<td align="left"><input type="text" size="25"

name="purchased_gift" value="'.$row['purchased_gift'].'"></td>

</tr>';

echo "<tr>";

echo "<td>" . $row['Gift2'] . "</td>";

echo '

 

<td align="left"><input type="text" size="25"

name="purchased_gift2" value="'.$row['purchased_gift2'].'"></td>

</tr>';

 

}

}

 

echo '</table>';

 

echo '<br /><div align="center"><input type="submit"

name="Submit" value="Purchase Gifts" /></div>

<input type="hidden" name="updatebtn" value="updatebtn" />';

 

echo '</form>';

 

mysqli_close($link);

 

}

else

echo "There was no matching record for the User ID " . $searchTerm;

 

 

?>

Edited by jlindsey
Link to comment
Share on other sites

How are you updating the db table? Can you show your update query (presumably in purchasegifts.php).

 

And this just plain hurts to see:

 

$myData = mysqli_query($link, $sql);
$myData2 = mysqli_query($link, $sql);
$myData3 = mysqli_query($link, $sql);

 

Consolidate your 3 queries into 1 and make better use of your HTML display code.

Edited by mrMarcus
Link to comment
Share on other sites

Well that is where I'm having trouble, cant figure out what to do with my updatequery or where to put it. I left it out of the script above hoping someone would help me with the updatequery. I updated the script above with the updatequery but I know it needs work. Just getting into php so learning as I go.

Edited by jlindsey
Link to comment
Share on other sites

OK I did some cleaning up to the script, Still cannot get the second form which is located inside the script to work. I moved the first form outside my php tags and it call on the data just fine and pulls up the correct information that is needed. After the search is complete a user can then enter data into a new form that was displayed by the first search. Except my script is not updating my database to whatever it is that I entered in the form. But if I go into the database and manually enter it, the information is then displayed on the form where I need to update information. So obviously I am doing something wrong with this script. I cleaned it up a bunch so it should be easier to read. Thank You for any help.

 

 

<form action = "" method = "GET">

<input type = "text" name = "keyword" />

<input type = "submit" name = "searchbtn" value = "Search Gift Registry" />

</form>

<br></br>

<?php

if($_GET) {

$searchTerm = $_GET['keyword'];

$connect = mysql_connect("localhost","#####","######");

if($connect) {

mysql_select_db("users",$connect);

$query = "SELECT * FROM users WHERE userid='$searchTerm'";

$results = mysql_query($query);

while($row = mysql_fetch_array($results)) {

$output .= "<b>First Name:</b> " . $row['FirstName'] . "<br />";

$output .= "<b>Last Name:</b> " . $row['LastName'] . "<br />";

$output .= "<b>User ID:</b> " . $row['userid'] . "<br />";

 

}

echo $output;

 

if (isset($_POST['submitbtn'])){

$UpdateQuery = "UPDATE users SET purchased_gift=$_POST[purchased_gift]' WHERE userid='$searchTerm'";

mysql_query($UpdateQuery, $connect);

};

 

$query = "SELECT * FROM users WHERE userid='$searchTerm'";

$myData = mysql_query($query, $connect);

 

echo '<form method="post" action="">';

// Table header.

echo '<table align="center" cellspacing="0" cellpadding="5" border="2">

<tr>

 

<td align="center"><b>Gifts</b></td>

<td align="center"><b>Purchase Gifts</b></td>

 

 

 

</tr>';

while ($row = mysql_fetch_array($myData)) {

echo "<tr>";

echo "<td>" . $row['Gift'] . "</td>";

echo '

 

<td align="left"><input type="text" size="25"

name="purchased_gift" value="'.$row['purchased_gift'].'"></td>

</tr>';

echo "<tr>";

echo "<td>" . $row['Gift2'] . "</td>";

echo '

 

<td align="left"><input type="text" size="25"

name="purchased_gift2" value="'.$row['purchased_gift2'].'"></td>

</tr>';

 

}

 

echo '</table>';

 

echo '<br /><div align="center"><input type="submit"

name="Submit" value="Update Gift Registry" /></div>

<input type="hidden" name="submitbtn" value="submitbtn" />';

 

echo '</form>';

} else {

die(mysql_error());

 

}

 

 

}

 

mysql_close($connect);

 

?>

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.