select and insert

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

select and insert

Post by hrubos »

Who can show me why I can't insert number of room into database when user choose room???I only can insert id_reversation well into database.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, http://www.pspad.com">
  <html>
  <head><title>Rezevace</title></head>
  <body>
<form method = "POST" action = "data.php">


<?php
require_once ("D:\htdocs\BP\db_connectFun.php");
db_connect();

$query="SELECT DISTINCT p.id_numberRoom,p.number_room,p.building,p.floor,p.state_room,
p.id_typeRoom, t.number_beg,t.price
FROM pokoj p,type_room t
WHERE p.id_typeRoom = t.id_typeRoom";

$query_numRoom = "SELECT DISTINCT p.number_room FROM pokoj p";
$result_numRoom = mysql_query($query_numRoom);
$result=mysql_query($query)or die(mysql_error());

$num=mysql_numrows($result);
mysql_close();


$i=0;

while ($i < $num ) {

$id_numberRoom=mysql_result($result,$i,"id_numberRoom");
$number_room=mysql_result($result,$i,"number_room");
$building=mysql_result($result,$i,"building");
$floor=mysql_result($result,$i,"floor");
$number_beg=mysql_result($result,$i,"number_beg");
$price=mysql_result($result,$i,"price");

?>
<tr>
<?php
$n=0;
echo "<td>$id_numberRoom</td>" ;
echo "<td>$number_room</td>" ;
echo "<td>$floor</td>";
echo "<td>$building</td>";
echo "<td>$number_beg</td>";
echo "<td>$price</td>";

if($number_beg==2) {
for($s=1;$s <=2;$s++ )
echo "<td> <input type= 'radio' value = '$n' name= 'id_reservation'></td>";
}
if($number_beg == 3){
for($s=1;$s<=3;$s++ )
echo "<td> <input type= 'radio' value = '$n' name= 'id_reservation'></td>";
}

echo "<td> <input type= 'radio' value = '$n' name= 'id_reservation'></td>";
//echo "<td> <input type= 'radio' value = '$number_room' name= 'number'></td>";
$i++ ;



db_connect();



if($number_room==$i){
$query_room = "insert into reser_1 " . "(number_room) values"
			. "('$number_room')";
 mysql_query($query_room) or die(mysql_error());
 }

}
?>
<tr>
<td>
 <input type="submit" value="Submit" name="submit">
 </td>
</tr>
</tr>
</form>
 </body>
</html>


//data.php
<?php
$id_reservation = $_POST['id_reservation'];
//$number = $_POST['number'];

if(!get_magic_quotes_gpc())
 {
 $id_reservation  = addslashes($id_reservation);
 }

 @ $db = mysql_connect("localhost", "root","ha1985") or die("Error");
 mysql_select_db("kolej",$db) or die("Can't choose database");
 
 $query = "insert into reser_1 " . "(id_reservation) values"
		. "('$id_reservation')";
 mysql_query($query) or die(mysql_error());
?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

could you please explain your question a little more. are you receiving an error? Do you just not know how to format your sql query to perform an insert of more than one column?

Code: Select all

insert into `mytable` (`col1`,`col2`) values ('some val','some other val')
or

Code: Select all

insert into `mytable` set `col1` = 'some val', `col2` = 'some other val'
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You only have id_reservation in your insert query. You need more fields specified to have more data.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

I haven't receive any error but when inserting to database, I haven't received number of room exactly.
That code : select --> output how many room and number of room in a table. Then user can chooe room by cliking radiobutton. ---> insert : id_resrevation into database(exactly) but number of room can't be inserted into databse follow id_resrevation(insert into table in which there are id_resrevaton and number_room)
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

Burrito wrote:could you please explain your question a little more. are you receiving an error? Do you just not know how to format your sql query to perform an insert of more than one column?

Code: Select all

insert into `mytable` (`col1`,`col2`) values ('some val','some other val')
or

Code: Select all

insert into `mytable` set `col1` = 'some val', `col2` = 'some other val'
follow you, but I don't know how change this type :

Code: Select all

echo "<td> <input type= 'radio' value = '$n' name = 'id_reservation'></td>";
because if i use
insert into `mytable` set `col1` = 'some val', `col2` = 'some other val'
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I still don't understand your question. are you having a hard time getting the value of the variable you are choosing to insert?
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

Burrito wrote:I still don't understand your question. are you having a hard time getting the value of the variable you are choosing to insert?
U can think that you want to reserver a room and u choose a number of room by cliking in radiobutton

Code: Select all

echo "<td> <input type= 'radio' value = '$n' name = 'id_reservation'></td>";
i++;

if($number_room=$i){
$query_room = "insert into reser_1 " . "(number_room) values"
                        . "('$number_room')";
 mysql_query($query_room) or die(mysql_error());
 }
Now what does system do???


Ofcourse after cliking radiobutton, number of room will be inserted into database --- > this is my problem. I couldn't insert number of room into database exactly.(number of room have been outputted by query SQL)

Plz help me, i have tried some ways but still not run
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you're not going to be able to do that on the same page. You have to select the room number then submit the page and post its value to the subsequent page where you'll perform your insert.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

I have changed a little but i have received error "Notice: Undefined index: number in D:\htdocs\BP\Room\outputRoom.php on line 23"

Code: Select all

<?php
require_once ("D:\htdocs\BP\db_connectFun.php");
$number = $_POST['number'];
db_connect();

$query="SELECT DISTINCT p.id_numberRoom,p.number_room,p.building,p.floor,p.state_room,
p.id_typeRoom, t.number_beg,t.price
FROM pokoj p,type_room t
WHERE p.id_typeRoom = t.id_typeRoom";

$query_numRoom = "SELECT DISTINCT p.number_room FROM pokoj p";
$result_numRoom = mysql_query($query_numRoom);
$result=mysql_query($query)or die(mysql_error());

$num=mysql_numrows($result);
mysql_close();


$i=0;

while ($i < $num ) {

$id_numberRoom=mysql_result($result,$i,"id_numberRoom");
$number_room=mysql_result($result,$i,"number_room");
$building=mysql_result($result,$i,"building");
$floor=mysql_result($result,$i,"floor");
$number_beg=mysql_result($result,$i,"number_beg");
$price=mysql_result($result,$i,"price");

?>
<tr>
<?php

$n=0;
echo "<td>$id_numberRoom</td>" ;
echo "<td>$number_room</td>" ;
echo "<td>$floor</td>";
echo "<td>$building</td>";
echo "<td>$number_beg</td>";
echo "<td>$price</td>";
/**
if($number_beg==2) {
for($s=1;$s <=2;$s++ )
echo "<td> <input type= 'radio' value = '$n' name= 'id_reservation'></td>";
}
if($number_beg == 3){
for($s=1;$s<=3;$s++ )
echo "<td> <input type= 'radio' value = '$n' name= 'id_reservation'></td>";
}
*/


echo "<td> <input type= 'radio' value = '$number_room' name = 'number'></td>";
$i++;
//echo "<td> <input type= 'radio' value = '$number_room' name= 'number'></td>";

//$i++ ;
db_connect();

if($number_room==$number)
{$query_room = "insert into reser_1 " . "(number_room) values"
			. "('$number_room')";
 mysql_query($query_room) or die(mysql_error());
}
<tr>
<td>
 <input type="submit" value="Submit" name="submit">
 </td>
</tr>
</tr>
</form>
 </body>
</html>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

are you posting to that page?

if not, $number will never get set.
Post Reply