Jump to content

Update Query


Icewolf
Go to solution Solved by Icewolf,

Recommended Posts

Hi

I am trying to create a page that display the information from the database. But also there is a place to update it. I can get it to display correctly but I am having problems with the update. It keeps failing.

</HTML>
<body>
	<Form action="Dropdown_new.php" method="POST" target="showhere">
	<select name="Member_ID">
	<option value="">Select one…</option>
	<option value="Icewolf">Icewolf</option>
	<option value="Travisr826">Travisr826</option>
	<option value="josehasthestick">josehasthestick</option>
	<option value="Hhn87">Hhn87</option>
	<option value="Twisted31007">Twisted31007</option>
	<option value="Midget_chunkin">Midget_chunkin</option>
	<option value="cashstro718">cashstro718</option>
	<option value="mpnak313">mpnak313</option>
	<option value="Wisconsin24">Wisconsin24</option>
	<option value="usmc57-2">usmc57-2</option>
	<option value="Derbin11">Derbin11</option>
	<input type="submit" value="filter">
	</select>

	</form>
	<iframe width="800" height="100" name="showhere" marginheight="0" marginwidth="0" frameborder="0" scrolling="no"> </iframe>
	<Form method ="POST" name="update" action="Update.php" />
		
	Bank:
	<input type="text" name="bank" />
	Reward 1:
	<input type="text" name="reward1" />
	Reward 2:
	<input type="text" name="reward2" />
	Reward 3:
	<input type="text" name="reward3" />
	
	<input type="submit" name="submit" value="Update" />
	</form>
	
</body>
</html>

Here is the PHP

<?php 
$username = "UserName";
$password = "Password";
$hostname = "localhost";
 
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MySQL");
echo "<font face=tahoma color=#ff000><b>Connected to MySQL</b></font><br><br>";

//select a database to work with
$selected = mysql_select_db("pdogclan_points",$dbhandle)
  or die("Did this change");
  
// Formulate Query
if (isset($_POST["Member_ID"])){
$memid = mysql_real_escape_string($_POST["Member_ID"]);
$bank = $_POST['bank'];
$reward1 = $_POST['reward1'];
$reward2 = $_POST['reward2'];
$reward3 = $_POST['reward3'];
$query = "UPDATE Points_Rewards Set Bank = '$bank', Reward_1 = '$reward1', Reward_2 = '$reard2', Reward_3 = '$reward3' WHERE Member_ID = '$memid'" or die("Could Not Formulate the Query");
}

if(mysql_query($query)){
echo "updated";}
else{
echo "fail";}
?>

I am getting through all the code but it hitting the last echo. I need to pull the ID from the top form. I don't know if this is correct way to do this.

 

Thanks

Edited by Icewolf
Link to comment
Share on other sites

Do you realize that your update query is hard coded for specific values and does not use the values passed in the POST data? So, every time you run that page you will have the same value int he database. So, you probably aren't seeing any changes because of that.

Link to comment
Share on other sites

You are putting this in the wrong spot...

 

 

or die("Could Not Formulate the Query");

 

 

//try this and see what it tells you
$query = "UPDATE Points_Rewards Set Bank = '$bank', Reward_1 = '$reward1', Reward_2 = '$reard2', Reward_3 = '$reward3' WHERE Member_ID = '$memid'";
$result = mysql_query($query) or die(mysql_error());
Edited by akphidelt2007
Link to comment
Share on other sites

your form page has two separate forms. only the first one has a Member_ID field. the Member_ID isn't submitted with the second form. you need one form that has all the fields in it.

 


 

all your form processing code should be inside the conditional if(){} statement -

 

if(isset($_POST["Member_ID"])){

 

    all the form processing code needs to be in here


}

 

you currently have the mysql_query() statement outside of and after the conditional statement.

Link to comment
Share on other sites

Most like isset($_POST['Member_ID']) isn't getting set.

 

echo $query right before the if(mysql_query($query)){} and see what it says.

You are correct. It is not pulling  the member_id. How do I get to the ID if it is on another form? Do I need to create another drop down in this form?

Link to comment
Share on other sites

your form page has two separate forms. only the first one has a Member_ID field. the Member_ID isn't submitted with the second form. you need one form that has all the fields in it.

 


 

all your form processing code should be inside the conditional if(){} statement -

 

if(isset($_POST["Member_ID"])){

 

    all the form processing code needs to be in here

 

}

 

you currently have the mysql_query() statement outside of and after the conditional statement.

Thanks Mac I was wondering this but how do use two different buttons on here to work. One is to pull the information the other is to update the information.

 

AK I wasn't sure how to get the two buttons to work on one form.

Edited by Icewolf
Link to comment
Share on other sites

Thanks Mac I was wondering this but how do use two different buttons on here to work. One is to pull the information the other is to update the information.

 

AK I wasn't sure how to get the two buttons to work on one form.

 

You can have multiple submit buttons in one form. Just got to give them a different name or value and check which one was pushed.

Edited by akphidelt2007
Link to comment
Share on other sites

Easiest way is give the submit buttons the same name, say "btnSubmit", but with different values. Then check the value of $_POST['btnSubmit'] to see which was clicked.

 

If the user presses return button to submit the form then the result depends on the user's browser - eg Firefox sends no value for btnSubmit, IE sends the first value on the form

Edited by Barand
Link to comment
Share on other sites

Okay I think I understand what you all are saying but I don't know if I did it right. I can get the filter button to work but not the submit.

HTML

<!DOCTYPE HTML>
<html>
<head>
<title>Icewolf is the Man!!</title>
</head>
<body>
	<Form action="Dropdown_new.php" method="POST" target="showhere">
	<select name="Member_ID">
	<option value="">Select one…</option>
	<option value="Icewolf">Icewolf</option>
	<option value="Travisr826">Travisr826</option>
	<option value="josehasthestick">josehasthestick</option>
	<option value="Hhn87">Hhn87</option>
	<option value="Twisted31007">Twisted31007</option>
	<option value="Midget_chunkin">Midget_chunkin</option>
	<option value="cashstro718">cashstro718</option>
	<option value="mpnak313">mpnak313</option>
	<option value="Wisconsin24">Wisconsin24</option>
	<option value="usmc57-2">usmc57-2</option>
	<option value="Derbin11">Derbin11</option>
	<input type="submit" name="filter" value="filter">
	</select>
	Bank:
	<input type="text" name="bank" />
	Reward 1:
	<input type="text" name="reward1" />
	Reward 2:
	<input type="text" name="reward2" />
	Reward 3:
	<input type="text" name="reward3" />
	
	<input type="submit" name="submit" value="Update" />
	

	</form>
	<iframe width="800" height="100" name="showhere" marginheight="0" marginwidth="0" frameborder="0" scrolling="no"> </iframe>
	
	
</body>
</html>

PHP

<?php 
$username = "Userid";
$password = "Password";
$hostname = "localhost";
 
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MySQL");
echo "<font face=tahoma color=#ff000><b>Connected to MySQL</b></font><br><br>";

//select a database to work with
$selected = mysql_select_db("pdogclan_points",$dbhandle)
  or die("Did this change");
  
// Formulate Query
if (isset($_POST['filter'])){
$memid = mysql_real_escape_string($_POST["Member_ID"]);
$query = sprintf("SELECT Member_ID, Bank, Reward_1, Reward_2, Reward_3 FROM Points_Rewards WHERE Member_ID = '$memid'") or die("Could Not Formulate the Query");
}
//execute the SQL query and return records
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
//if (!$result) {
 //   $message  = 'Invalid query: ' . mysql_error() . "\n";
 //   $message .= 'Whole query: ' . $query;
 //   die($message);
//}

//fetch tha data from the database
while ($row = mysql_fetch_array($result))   
echo "<table width=750 cellspacing=2 cellpadding=2 border=2>
		<tr>
			<td bgcolor=#000000 width=150><font face=tahoma color=white>ID: {$row['Member_ID']}</font></td>". 
			"<td width=150><font face=tahoma>Bank: {$row['Bank']}</td>". 
			"<td width=150><font face=tahoma>Reward 1: {$row['Reward_1']}</td>". 
			"<td width=150><font face=tahoma>Reward 2: {$row['Reward_2']}</td> ". 
			"<td width=150><font face=tahoma>Reward 3: {$row['Reward_3']}</td>
		</tr>
	  </table><br></font>";//display the results
// Formulate Update Query
Else if (isset($_POST["submit"])){
$memid = mysql_real_escape_string($_POST["Member_ID"]);
$bank = $_POST['bank'];
$reward1 = $_POST['reward1'];
$reward2 = $_POST['reward2'];
$reward3 = $_POST['reward3'];
}
$query = "UPDATE Points_Rewards Set Bank = '$bank', Reward_1 = '$reward1', Reward_2 = '$reward2', Reward_3 = '$reward3' WHERE Member_ID = '$memid'";
$result = mysql_query($query) or die(mysql_error());
		

//echo $query;
//if(mysql_query($query)){
//echo "updated";}
//echo "fail";}
//close the connection
mysql_close($dbhandle);
?>
Link to comment
Share on other sites

What do you mean if you give them the same name? I thought if I use Name="" defines the button. The filter button works just fine. Then when I hit the Update button I can see the values are coming over I am just getting an error message "Invalid query: Query was empty Whole query:"

Link to comment
Share on other sites

What do you mean if you give them the same name?

 

I meant exactly that

 

EG

 

<?php

if (isset($_POST['btnSubmit'])) {
    if ($_POST['btnSubmit']=='Filter') {
        echo "Filter button clicked";
    }
    else {
        echo "Update button clicked";
    }
    echo '<br /><hr /><br />';
}
?>

<form method="post">
<input type="submit" name="btnSubmit" value="Update" />
<br />
<input type="submit" name="btnSubmit" value="Filter" />

</form>
Link to comment
Share on other sites

Here is what is interesting I can get the filter to work now problem when I have both buttons here. I am getting an error "Invalid query: Query was empty Whole query:". and I notice is that it updates the database to 0 for that id. I know the query works because I removed the filter piece and it works fine.

Edited by Icewolf
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.