PHP MYSQL DATABASE INSERTING IN TABLE HELP

0 Oussama Fahchouch · June 24, 2015
I have a problem dear stackoverflowers, could someone please help me out?
This is my code:

<?php
$host = "localhost";
$user = "root";
$pass = "password";
$db = "hotelcalifornia";

$room_Number = ($_POST['Room_Number']);
$room_Category = ($_POST['Room_Category']);
$room_Description = ($_POST['Room_Description']);
$room_Detail = ($_POST['Room_Detail']);

$conn = mysql_connect($host, $user, $pass);
$db = mysql_select_db($db, $conn);

mysql_select_db($db, $conn);

$sql = "INSERT TO room (roomNumber, roomCategory, roomDescription,roomDetail) VALUES ('$room_Number','$room_Category', '$room_Description','$room_Detail')";

mysql_query($sql, $conn); 

?>


Can someone tell me why i can't insert this data into my table in the database?

Post a Reply

Replies

Oldest  Newest  Rating
+1 Ron Butcher · June 26, 2015
Do you have errors turned on inside of your php.ini file?  If not that is why it is showing a blank page rather than an error.

For your problem, variables set outside of a class, and inside of a class are two different variables (even if they share a name) and are in no way related.  In order to get the class to see the variable, you must pass it into the class via a function.  I modified your code:


<?php
class RoomAdministrationSubsystem extends ReservationSystem{
// Gegevens Database
public $host;
public $user;
public $pass;
public $db;
// Gegevens invoer nieuwe kamer
public $room_Number;
public $room_Category;
public $room_Description;
public $room_Detail;

function AddRoomCategory($room_Number, $room_Category, $room_Description, $room_Detail){
$conn = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->db, $conn);

$AddRoomToDatabase = "INSERT INTO room (roomNumber, roomCategory, roomDescription,roomDetail) VALUES ('$room_Number','$room_Category', '$room_Description','$room_Detail')";

mysql_query($AddRoomToDatabase, $conn) or die(mysql_error($conn)); 
 
}

public function setDBVar($host, $user, $pass, $db)
{
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->db   = $db;
}
}

$room_Number      = $_POST['Room_Number'];
$room_Category    = $_POST['Room_Category'];
$room_Description = $_POST['Room_Description'];
$room_Detail      = $_POST['Room_Detail'];

$host = "localhost";
$user = "root";
$pass = "password";
$db   = "hotelcalifornia";

$testobject = new RoomAdministrationSubsystem();
$testobject->setDBVar($host, $user, $pass, $db);
$testobject->AddRoomCategory($room_Number, $room_Category, $room_Description, $room_Detail);



I added a function to pass in the variables for the database.  By calling the function setDBVar and passing in the four database variables, you can tell the class to use these variables.  When you call the AddRoomCategory function, you also need to pass in the variables that you will be using.

You also need to define the variables before you try to use them.  I moved the variable definitions above the class calls so that they are assigned and ready to go.

The last thing I added was a "or die" statement at the end of you query.  This will freeze the page if the query fails and display the warning that MySQL returns.
0 Kenneth Malicay · June 24, 2015
INSERT INTO*
0 Oussama Fahchouch · June 24, 2015
Thanks!

But now i want to use this in object oriented programming, could you help me plz.

It's not inserting into the database..

This is the code:


class RoomAdministrationSubsystem extends ReservationSystem{
// Gegevens Database
public $host;
public $user;
public $pass;
public $db;
// Gegevens invoer nieuwe kamer
public $room_Number;
public $room_Category;
public $room_Description;
public $room_Detail;

function AddRoomCategory(){
$conn = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->db, $conn);

$AddRoomToDatabase = "INSERT INTO room (roomNumber, roomCategory, roomDescription,roomDetail) VALUES ('$this->room_Number','$this->room_Category', '$this->room_Description','$this->room_Detail')";

mysql_query($AddRoomToDatabase, $conn); 
  
}
}


$testobject = new RoomAdministrationSubsystem();
$testobject->AddRoomCategory();

$host = "localhost";
$user = "root";
$pass = "password";
$db   = "hotelcalifornia";

$room_Number      = $_POST['Room_Number'];
$room_Category    = $_POST['Room_Category'];
$room_Description = $_POST['Room_Description'];
$room_Detail      = $_POST['Room_Detail'];
0 Oussama Fahchouch · June 24, 2015
the class that is extended is mentioned above in the document, thats not the problem.

HTML doesnt give me any error or warning.., but when i look into my database, it doesnt insert the data into the table..
0 Kenneth Malicay · June 24, 2015
I'm not very familiar with PHP but whenever something goes wrong on my code I usually print something out on the console.
function AddRoomCategory(){
$conn = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->db, $conn);

$AddRoomToDatabase = "INSERT INTO room (roomNumber, roomCategory, roomDescription,roomDetail) VALUES ('$this->room_Number','$this->room_Category', '$this->room_Description','$this->room_Detail')";
// Print the AddRoomToDatabase here.
mysql_query($AddRoomToDatabase, $conn);
                       // Print success.

}

Try this. If it didn't print out success then copy the print of AddRoomToDatabase in mySql query and then try to run it. It would then give you the error.

If it prints out success then I probably couldn't help due to my lack of knowledge in the language. Sorry. :)


$AddRoomToDatabase = "INSERT INTO room (roomNumber, roomCategory, roomDescription,roomDetail) VALUES ('" + $this->room_Number + "','" + $this->room_Category + "', '" + $this->room_Description + "','" + $this->room_Detail + "')";


Also I just noticed. Try this if it works.
  • 1

SQL & Databases

107,164 followers
About

Everything SQL and Databases related in here!

Links
Moderators
Bucky Roberts Administrator