Jump to content

Running Queries ( Hey guys please help, Uni student doing his re-sits)


adamdyer

Recommended Posts

Ok, honestly I have NO basic knowledge of PHP MySQL - Im good at dreamweaver and designing but im no good with this - I have to build a database for a Hotel that allows people to register and book rooms etc, however I can type it all in, Its just the connection from 'getting' the information from the Database to the HTML side - I'll paste what I've got so far and hopefully someone can help me :-) (Please?)

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<title>View BOOKINGS using PHP</title>

<P><B>BOOKING DATABASE</B></P>
<hr>
<?php

# connect to MySQL
$conn = @mysql_connect("localhost","ajdref","ref2")
or die ("sorry - could not wont connect to MySQL"); 

# select the specified database
$rs = @mysql_select_db("ajdref", $conn)
or die ("sorry - could not connect to the ajdref");

#create the query - sets $sql to contain the required query
$sql = "SELECT * FROM `Booking Database`"; 

#execute the query - uses the mysql_query() function to issue the $sql query to the $conn connection
#$rs contains the result set in the form of an array
$rs = mysql_query($sql,$conn);
while ($row=mysql_fetch_array($rs))
{
echo($row["Unique ID"]);
echo("..");
echo($row["Booking ID"]);
echo("..");
echo($row["Customer Name"]);
echo("..");
echo($row["Check In"]);
echo("..");
echo($row["Check Out"]);
echo("..");
echo($row["Price Cat"]);
echo("..");
echo($row[""]);
echo("..");
echo($row[""]);
echo("..");
echo($row[""]);
echo(" <hr> ");
}

?>

<p><A href="index.html" target="_self">EXIT REPORT</A></p>

</body>
</html> 

</body>
</html>

 

Thanks Guys.

 

EDITED BY akitchin:  code tags are good for the mind, body and soul.  and for making your post easier to read.  please use them.

Link to comment
Share on other sites

First are you getting any errors?

 

Anytime you deal with sql you should show erros to see what the problem may be eg:

 

# connect to MySQL
$conn = @mysql_connect("localhost","ajdref","ref2")
or die ("sorry - could not wont connect to MySQL" . mysql_error()); 

 

What is it doing?

Link to comment
Share on other sites

Yeah , sorry about that guys I copied the wrong code at the start - this is the add customer code - so the Print Screen is now right. :-)

 

<HTML>
<HEAD>
<TITLE>Add customer to database</TITLE>
</HEAD>
<BODY>

</br>

Your Inputs Were : </p>

<?php

$CID = $_GET['Unique ID'];
echo( "Unique ID = <B>$CID</B></br>" );

$CID = $_GET['Booking ID'];
echo( "Booking ID = <B>$CID</B></br>" );

$CID = $_GET['Customer Name'];
echo( "Customer Name = <B>$CID</B></br>" );

$CID = $_GET['Check In'];
echo( "Check In = <B>$CID</B></br>" );

$CID = $_GET['Check Out'];
echo( "Check Out = <B>$CID</B></br>" );

$CID = $_GET['Price Cat'];
echo( "Price Cat = <B>$CID</B></br>" );

$CID = $_GET['Type Of Room'];
echo( "Type Of Room = <B>$CID</B></br>" );

$CID = $_GET['Password'];
echo( "Password = <B>$CID</B></br>" );

# connect to MySQL
$conn = @mysql_connect("localhost","ajdref","ref2")
or die ("sorry - could not wont connect to MySQL"); 

# select the specified database
$rs = @mysql_select_db("ajdref", $conn)
or die ("sorry - could not connect to the ajdref");

#create the query - sets $sql to contain the required query
$sql = "SELECT * FROM `Booking Database`"; 

$sql = "INSERT INTO `Booking Database` (Unique ID, Booking ID, Customer Name, Check In, Check Out, Price Cat, Type Of Room, Password)

VALUES ( '$CID', 'use the variable names you defined earlier' )"; 

$rs=mysql_query($sql,$conn);

if($rs){ echo("Customer added");}
else { echo("Error! Customer not added");}

?>

<p><A href="index.html" target="_self">MAIN MENU</A></p>

</BODY>
</HTML>

Link to comment
Share on other sites

update the $CID to the names

 

$UniqueID= $_GET['Unique ID'];
echo( "Unique ID = <B>$UniqueID</B></br>" );

$BookingID= $_GET['Booking ID'];
echo( "Booking ID = <B>$BookingID</B></br>" );

etc etc

 

now update the $SQL

$sql = "INSERT INTO `Booking Database` (Unique ID, Booking ID, Customer Name, Check In, Check Out, Price Cat, Type Of Room, Password)

VALUES ( '$UniqueID', '$BookingID', etc etc etc )"; 

 

 

EDIT:

update

mysql_query($sql,$conn);

to

mysql_query($sql,$conn)or die(mysql_error());

 

 

Link to comment
Share on other sites

it looks like you're passing the GET string incorrectly.  your string should look like this:

 

filename.php?Customer Name=something&Check In=something

 

keep in mind those spaces should be %20, not + or anything else.  furthermore, you need to use different variable names for each $_GET variable you're using, rather than $CID for each one because it will simply be overridden each time.  your SELECT is unnecessary here.

 

EDIT:  MadTechie covered the $CID issue, nevermind that.

Link to comment
Share on other sites

Ok So ive done what you guys have said but it stil saying its wrong  :'(

 

<HTML>
<HEAD>
<TITLE>Add customer to database</TITLE>
</HEAD>
<BODY>

</br>

Your Inputs Were : </p>

<?php

$UniqueID= $_GET['Unique ID'];
echo( "Unique ID = <B>$CID</B></br>" );

$BookingID= $_GET['Booking ID'];
echo( "Booking ID = <B>$CID</B></br>" );

$CustomerName= $_GET['Customer Name'];
echo( "Customer Name = <B>$CID</B></br>" );

$CheckIn= $_GET['Check In'];
echo( "Check In = <B>$CID</B></br>" );

$CheckOut= $_GET['Check Out'];
echo( "Check Out = <B>$CID</B></br>" );

$PriceCat= $_GET['Price Cat'];
echo( "Price Cat = <B>$CID</B></br>" );

$TypeOfRoom= $_GET['Type Of Room'];
echo( "Type Of Room = <B>$CID</B></br>" );

$Password= $_GET['Password'];
echo( "Password = <B>$CID</B></br>" );

# connect to MySQL
$conn = @mysql_connect("localhost","ajdref","ref2")
or die ("sorry - could not wont connect to MySQL"); 

# select the specified database
$rs = @mysql_select_db("ajdref", $conn)
or die ("sorry - could not connect to the ajdref");

#create the query - sets $sql to contain the required query
$sql = "SELECT * FROM `Booking Database`"; 

$sql = "INSERT INTO `Booking Database` (Unique ID, Booking ID, Customer Name, Check In, Check Out, Price Cat, Type Of Room, Password)

VALUES ( '$UniqueID', '$BookingID', '$CustomerName', '$CheckIn', '$CheckOut', '$PriceCat', '$TypeOfRoom', '$Password' )"; 

mysql_query($sql,$conn)or die(mysql_error());

if($rs){ echo("Customer added");}
else { echo("Error! Customer not added");}

?>

<p><A href="index.html" target="_self">MAIN MENU</A></p>

</BODY>
</HTML>


 

printscreen2.png

Link to comment
Share on other sites

it looks like you're passing the GET string incorrectly.  your string should look like this:

 

filename.php?Customer Name=something&Check In=something

 

OR the

form method="get"

not

form method="post"

 

if you are using method="post" then you can replace the $_GET with $_POST

 

 

Link to comment
Share on other sites

change

$sql = "INSERT INTO `Booking Database` (Unique ID, Booking ID, Customer Name, Check In, Check Out, Price Cat, Type Of Room, Password)

VALUES ( '$UniqueID', '$BookingID', '$CustomerName', '$CheckIn', '$CheckOut', '$PriceCat', '$TypeOfRoom', '$Password' )"; 

 

to

 

$sql = "INSERT INTO `Booking Database` (`Unique ID`, `Booking ID`, `Customer Name`, `Check In`, `Check Out`, `Price Cat`, `Type Of Room`, `Password`) VALUES ( '$UniqueID', '$BookingID', '$CustomerName', '$CheckIn', '$CheckOut', '$PriceCat', '$TypeOfRoom', '$Password' )"; 

 

also your not getting the data, read my last post

Link to comment
Share on other sites

ok , so now its actually saying theres a customer added which is good, but your right the data is not being added.

 

sorry for being stupid - but im not sure what Im meant to do with this bit:

 

it looks like you're passing the GET string incorrectly.  your string should look like this:

 

Code:

filename.php?Customer Name=something&Check In=something

 

 

Link to comment
Share on other sites

look at your current URL:

 

addcustomer.php?Customer+Username=000009&Booking+ID=000009A ...

 

that '+' means nothing to the GET query.  you need to replace those with spaces or with %20, or, for better practice, use underscores rather than whitespace of any kind:

 

addcustomer.php?Customer_Username=000009&Booking_ID=000009A ...

Link to comment
Share on other sites

sorry ' akitchin ' - but i am pretty dumb

 

 

addcustomer.php?Customer+Username=000009&Booking+ID=000009A ...

 

 

where is that in my code?

 

<HTML>
<HEAD>
<TITLE>Add customer to database</TITLE>
</HEAD>
<BODY>

</br>

Your Inputs Were : </p>

<?php

$UniqueID= $_GET['Unique ID'];
echo( "Unique ID = <B>$CID</B></br>" );

$BookingID= $_GET['Booking ID'];
echo( "Booking ID = <B>$CID</B></br>" );

$CustomerName= $_GET['Customer Name'];
echo( "Customer Name = <B>$CID</B></br>" );

$CheckIn= $_GET['Check In'];
echo( "Check In = <B>$CID</B></br>" );

$CheckOut= $_GET['Check Out'];
echo( "Check Out = <B>$CID</B></br>" );

$PriceCat= $_GET['Price Cat'];
echo( "Price Cat = <B>$CID</B></br>" );

$TypeOfRoom= $_GET['Type Of Room'];
echo( "Type Of Room = <B>$CID</B></br>" );

$Password= $_GET['Password'];
echo( "Password = <B>$CID</B></br>" );

# connect to MySQL
$conn = @mysql_connect("localhost","ajdref","ref2")
or die ("sorry - could not wont connect to MySQL"); 

# select the specified database
$rs = @mysql_select_db("ajdref", $conn)
or die ("sorry - could not connect to the ajdref");

#create the query - sets $sql to contain the required query
$sql = "SELECT * FROM `Booking Database`"; 

$sql = "INSERT INTO `Booking Database` (`Unique ID`, `Booking ID`, `Customer Name`, `Check In`, `Check Out`, `Price Cat`, `Type Of Room`, `Password`) VALUES ( '$UniqueID', '$BookingID', '$CustomerName', '$CheckIn', '$CheckOut', '$PriceCat', '$TypeOfRoom', '$Password' )"; 

mysql_query($sql,$conn)or die(mysql_error());

if($rs){ echo("Customer added");}
else { echo("Error! Customer not added");}

?>

<p><A href="index.html" target="_self">MAIN MENU</A></p>

</BODY>
</HTML>


 

heres also the HTML side of it if that makes a difference:

 

<HTML>

<HEAD>
</HEAD>
<BODY BGCOLOR=lemonchiffon>
<form NAME="GetCode" ACTION="addcustomer.php" METHOD=get>
  <FONT size=4>New customer registration form</FONT> 
  <P><b>Fill in the following fields then click 'Send' at the bottom of the page.</b> 
  </P>             
  <p>Customer Username ( max 6 characters ) 
    <INPUT Name ="Customer Username" size = 6 >
  </p>
                                   
  <P>Booking ID 
    <INPUT Name ="Booking ID" size = 30 >
  </P>
                    
  <P>Customer Name 
    <INPUT Name ="Customer Name" size = 60 >
  </P>
                 
  <p>Check In						
    <INPUT Name ="Check In" size = 20 >
  </p>
               
  <p>Check Out
    <INPUT Name ="Check Out" size = 40 >
  </p>
                  
  <p>Price Cat 
    <INPUT Name = "Price Cat" size= 20 >
  </p>
  
   <p>Type Of Room 
    <INPUT Name = "Type Of Room" size= 20 >
  </p>
  
  <p> Password 
    <INPUT Name = "Password" type="password" size= 20 >
  </p>
  
  <p>                    
<p> <b>Now click 'Send', or 'Clear' to reset the answer boxes to their initial default values. </b></p>
  
<div align="left"> 
<input type="reset"  value="Clear" name="RESET">
<input type="submit" value="Send" name="SUBMIT" >
<p><A href="index.html" target="_self">EXIT WITHOUT REGISTERING</A></p>
</div>

</form>
</BODY>
</HTML>


Link to comment
Share on other sites

  <INPUT Name ="Customer_Username" size = 6 >
 </p>

 <P>Booking ID 
   <INPUT Name ="Booking_ID" size = 30 >
 </P>

 <P>Customer Name 
   <INPUT Name ="Customer_Name" size = 60 >
 </P>

 <p>Check In						
   <INPUT Name ="Check_In" size = 20 >
 </p>

 <p>Check Out
   <INPUT Name ="Check_Out" size = 40 >
 </p>

 <p>Price Cat 
   <INPUT Name = "Price_Cat" size= 20 >
 </p>

  <p>Type Of Room 
   <INPUT Name = "Type_Of_Room" size= 20 >
 </p>


 

 

All you space should be replaced by _ the browser dont understand spaces

as well you need to do those too sorry

 


$UniqueID= $_GET['Unique_ID'];
echo( "Unique ID = <B>$UniqueID</B></br>" );

$BookingID= $_GET['Booking_ID'];
echo( "Booking ID = <B>$BookingID</B></br>" );

$CustomerName= $_GET['Customer_Name'];
echo( "Customer Name = <B>$CustomerName</B></br>" );

$CheckIn= $_GET['Check_In'];
echo( "Check In = <B>$CheckIn</B></br>" );

$CheckOut= $_GET['Check_Out'];
echo( "Check Out = <B>$CheckOut</B></br>" );

$PriceCat= $_GET['Price_Cat'];
echo( "Price Cat = <B>$PriceCat</B></br>" );

$TypeOfRoom= $_GET['Type_Of_Room'];
echo( "Type Of Room = <B>$TypeOfRoom</B></br>" );


Link to comment
Share on other sites

Not sure how its more secure ?

but i do use POST for forms and GET for links..

 

if you do want this then

change

<form NAME="GetCode" ACTION="addcustomer.php" METHOD=get>

 

to

<form NAME="GetCode" ACTION="addcustomer.php" METHOD="POST">

 

and the $_GET in the php to $_POST

 

EDIT: personally i would remove the spaces ;)

 

remember to update the php if you do

Link to comment
Share on other sites

HEY! it kinda works, now when I add a customer , I check PhpMyAdmin and the data is there, except one small snag - whenever I ask the customer to enter their 'Customer Username' - whatever they type, in my PhpMyAdmin, the username always says - Username...instead of what the customer has typed  ???

 

 

Link to comment
Share on other sites

$sql = "INSERT INTO `Booking Database` (`Customer Username`, `Booking ID`, `Customer Name`, `Check In`, `Check Out`, `Price Cat`, `Type Of Room`, `Password`) VALUES ( '$Customer Username', '$BookingID', '$CustomerName', '$CheckIn', '$CheckOut', '$PriceCat', '$TypeOfRoom', '$Password' )";

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.