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
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);
}
?>

Link to comment
Share on other sites

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

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.