My Insert Query Doesn't work

Discuss everything related to PHP Hotel Site. All versions. Any questions related to the PHP Hotel Site developing or using should be posted to this forum.

Moderators: ne_moj, zewa

apphoteluser
Junior
Junior
Posts: 14
Joined: Mar 16th, '17, 06:34

My Insert Query Doesn't work

Postby apphoteluser » Apr 25th, '17, 09:15

Hi all,

I want to make a query insert into other table when update rooms number, i tried like this:

$testIns = 'INSERT INTO '.TABLE_ROOM_OCCUPACY.' (booking_id, booking_no, room_id, room_no)
VALUES (3,3,3,3)';

or

$testIns = 'INSERT INTO '.TABLE_ROOM_OCCUPACY.' (booking_id, booking_no, room_id, room_no)
VALUES ("3","3","3","3")';

But, it's not worked.
I already make "TABLE_ROOM_OCCUPACY" in Settings.inc.php, also already tried select * from that table (it's worked for select query).
I put that code in UpdateRoomNumbers().

Is there anything that i need to do first, before insert query?

Thanks & Regards,

Eko

administrator
Site Admin
Site Admin
Posts: 5937
Joined: Jan 7th, '09, 23:18
Contact:

Re: My Insert Query Doesn't work

Postby administrator » Apr 25th, '17, 18:43

What exactly doesn't work?

apphoteluser
Junior
Junior
Posts: 14
Joined: Mar 16th, '17, 06:34

Re: My Insert Query Doesn't work

Postby apphoteluser » Apr 26th, '17, 01:42

I tried to insert that query but when I check into table, the value which I inserted is not there.
But, when I tried this below code, it's worked. Do you know what's wrong with that?

$servername = "localhost";
$dbname = "uhotel";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'INSERT INTO '.TABLE_ROOM_OCCUPACY.' (booking_id, booking_no, room_id,room_no)
VALUES ("5", "5", "5","5")';
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

ne_moj
Master
Master
Posts: 243
Joined: Jan 1st, '16, 14:11

Re: My Insert Query Doesn't work

Postby ne_moj » Apr 26th, '17, 08:56

Try this query:

Code: Select all

$servername = "localhost";
$dbname = "uhotel";
$username = "root";
$password = "";

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'INSERT INTO '.TABLE_ROOM_OCCUPACY.' (booking_id, booking_no, room_id,room_no)
VALUES ("5", "5", "5","5")';
// use exec() because no results are returned
if($conn->exec($sql) === false){
echo $conn->errorInfo();
}else{
echo "New record created successfully";
}


As I understand it, PDO::exec does not throw exceptions (http://php.net/manual/en/pdo.exec.php)

apphoteluser
Junior
Junior
Posts: 14
Joined: Mar 16th, '17, 06:34

Re: My Insert Query Doesn't work

Postby apphoteluser » Apr 26th, '17, 09:25

Thanks Ne_moj,

Yes, it's worked but i'ms still confused why need to use to create a new PDO?
why cant' directly use the usual code like this:
$testIns = 'INSERT INTO '.TABLE_ROOM_OCCUPACY.' (booking_id, booking_no, room_id, room_no)
VALUES ("3","3","3","3")';

but, thanks for the help :D


Return to “ApPHP HotelSite / uHotelBooking Talk {developers/users}”