Jump to content

posting the wrong value


dannyone

Recommended Posts

hello everyone can someone please help with this

 

i have this page where i am submitting a dropdown and then some checkboxs to a mysql table. the code is working and inserting nicely, but its taking the last value from the drop down and inserting that one instead of the value i have selected?!

 

could someone please point out where i am going wrong im rather puzzled!

 

thanks

 

<?php
$row = $_POST['Room_ID'];
if (!isset($_POST['submit'])) 

{ 
?>
<html>
<head>
<title>Student Timetable</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Select a Student:<br />
<?php
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
	die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
	die("Unable to select database");
}
echo'<select name="Room_ID">';

$res=mysql_query("select Room_ID from Rooms");
if(mysql_num_rows($res)==0) echo "there is no data in table..";

else
echo "<option>--SELECT ROOM--</option>";
for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);

echo"<option>$row[Room_ID]</option>";
}
echo'</select>';

?>
<input type="submit" value="submit" name="submit">
<br /><br />

</form>

<?
}else{
echo "" .$row. "";
}
?>

<?
if (!isset($_POST['submit1'])) { // if page is not submitted to itself echo the form
?>
<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
<input type="checkbox" name="media[]" value="Poster" />Poster<br />
<input type="checkbox" name="media[]" value="Website" />Website<br />
<input type="checkbox" name="media[]" value="Magazine" />Magazine<br />
<input type="submit" value="submit1" name="submit1">
</form>
<?
} else {
mysql_connect("localhost", "danny", "danny") or
die("Could not connect: " . mysql_error());
mysql_select_db("tutorial");
$media_array = $_POST['media'];
foreach ($media_array as $one_media) {
$source .= $one_media.", ";
}
$media = substr($source, 0, -2);

$query = "INSERT INTO Rooms_Free (Room_ID, timeslot)
VALUES( '$row[Room_ID]' ,'$media')";
$result = mysql_query($query);
}
?>

 

im using $row[Room_ID] to store the value they have selected.

 

thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/150210-posting-the-wrong-value/
Share on other sites

first...change this:

echo"<option>$row[Room_ID]</option>";

to

echo"<option value=\"$row[Room_ID]\">$row[Room_ID]</option>";

 

then, at the end:

<?php
} else {
mysql_connect("localhost", "danny", "danny") or
die("Could not connect: " . mysql_error());
mysql_select_db("tutorial");
$media_array = $_POST['media'];
foreach ($media_array as $one_media) {
$source .= $one_media.", ";
}
$media = mysql_real_escape_string(substr($source, 0, -2));
$media_id = mysql_real_escape_string($_POST['Room_ID']);

$query = "INSERT INTO Rooms_Free (Room_ID, timeslot)
VALUES( '$media_id' ,'$media')";
$result = mysql_query($query);
}
?>

iv got a bit of it working however can some1 advise me of a way so that i can store the value the user has selected from the drop down into a array so i can save it to my mysql db

 

<?php
$row = $_POST['Room_ID'];
if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
<form method="post" action="<?PHP_SELF;?>">
<SELECT NAME="Room_ID">
<option value="Room_ID" SELECTED>Select A Room</option>
<?php
mysql_connect("localhost", "danny", "danny", "tutorial") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("tutorial") or die(mysql_error());
echo "Connected to Database";
$result = mysql_query ("SELECT Room_ID FROM Rooms");
if ($row = mysql_fetch_array($result)) {
do {
print ("<OPTION VALUE=\"".$row['Room_ID']."\">");
print $row['Room_ID'];
print ("</OPTION>");
} while($row = mysql_fetch_array($result));
} else {
mysql_connect("localhost", "danny", "danny") or
die("Could not connect: " . mysql_error());
mysql_select_db("tutorial");
$query = "INSERT INTO Rooms_Free (Room_ID)
VALUES ()";
$result = mysql_query($query);
}}
echo $_POST['Room_ID'] ;
?>
<input type="submit" value="submit" name="submit">
</select>

</form>

 

i just dont know what to use in the VALUES section to get it to enter. the echos work but they wont enter into the db... can some1 please help

 

Thanks

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.