Jump to content

Updating mysql from Dropdown list with Submit button


victimssun

Recommended Posts

I've seen a lot of questions related to this, but not exactly what I'm looking for. I am a beginner and am stumped. I currently have a drop down list that is being updated via a query from mysql. I want to add a select button with this list and when clicked update a different table. I need to add the logged in userID (which is ($query_row[userID]), maybe?) and the ingredient that they selected from the drop down. I have already tried making the submit button point to an insert.php page but was having trouble passing the info with $_POST.

 

Here is what I have for my dropdown list page:

<?php
//Connect to mysql
mysql_connect("*", "*", "*") or die(mysql_error());
mysql_select_db("*") or die(mysql_error());

//check cookies to make sure they are logged in
if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
$query = mysql_query("SELECT userID FROM users WHERE username = '$username'");
$query_row = mysql_fetch_array($query);
while($info = mysql_fetch_array( $check ))
{

//if the cookie has the wrong password, they are taken to the login page
if ($pass != $info['password'])
{ header("Location: login.php");
}

//otherwise they are shown the member area
else
{
//to make sure info is being pulled correctly
echo "Welcome $username<p>";
echo($query_row[userID]);

//this is pulling the ingredients from db
$result = @mysql_query("SELECT ingredientID,ingredient FROM ingredients");

//this is putting the ingredients into a dropdown
print "<p>Select ingredient(s):\n";
print "<select name=\"ingredient\">\n";
while ($row = mysql_fetch_assoc($result)){
$ingredientID = $row['ingredientID'];
$ingredient = $row['ingredient'];
print "<option value=$ingredientID>$ingredient\n";
}
print "</select>\n";
print "</p>\n";

echo "<a href=drinklist.php>View Current List</a>";
print "</p>\n";
echo "<a href=logout.php>Logout</a>";
}
}
}
else

//if the cookie does not exist, they are taken to the login screen
{
header("Location: login.php");
}
?>

 

I will appreciate anything I can get. Thanks

I noticed you didn't close off each option:

<?php
//Connect to mysql
mysql_connect("*", "*", "*") or die(mysql_error());
mysql_select_db("*") or die(mysql_error());
//check cookies to make sure they are logged in
if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
$query = mysql_query("SELECT userID FROM users WHERE username = '$username'");
$query_row = mysql_fetch_array($query);
while($info = mysql_fetch_array( $check ))
{

	//if the cookie has the wrong password, they are taken to the login page
	if ($pass != $info['password'])
	{ header("Location: login.php");
	}

	//otherwise they are shown the member area
	else
	{
		//to make sure info is being pulled correctly
		echo "Welcome $username<p>";
		echo($query_row[userID]);

		//this is pulling the ingredients from db
		$result = @mysql_query("SELECT ingredientID,ingredient FROM ingredients");

		//this is putting the ingredients into a dropdown
		print "<p>Select ingredient(s):\n";
		print "<select name=\"ingredient\">\n";
		while ($row = mysql_fetch_assoc($result)){
			$ingredientID = $row['ingredientID'];
			$ingredient = $row['ingredient'];
			print "<option value=$ingredientID>$ingredient</option>\n";
		}
		print "</select>\n";
		print "</p>\n";

		echo "<a href=drinklist.php>View Current List</a>";
		print "</p>\n";
		echo "<a href=logout.php>Logout</a>";
	}
}
}
else

//if the cookie does not exist, they are taken to the login screen
{
header("Location: login.php");
}
?>

Could that be the issue?

That would definitely have caused some of my issues. I appreciate the quick response. So I now have a form that I need to insert (right?). I understand this if I was using text fields, but not with the drop down.

<form action="insert.php" method="post">
userID: <input type="text" name="userID" />
ingredientID: <input type="text" name="ingredientID" />
<input type="submit" />
</form>

Thanks

 

I don't mean to be a double poster, but just for an update. I am able to pass the userID with the insert.php by using

sql = mysql_query("INSERT INTO useringredient (userID, ingredientID) VALUES ($query_row[userID],'$_POST[ingredientID]')");

and for the main page it has

		<form name = "insert" form action="insert.php" method="post">
			<input type="submit" />
		</form>

 

However, I am unable to get the selected ingredient (ingredientID) from the drop down and have it $_POST to the table as well by way of insert.php. Any help?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.