PHP mysqli does not insert into database - php

I am very familiar with PHP, but this is my first time using the mysqli library. I am trying to insert a row into the database and it just does not insert. My code look like this (database login details changed for security):
... // $_REQUEST variables processed
$oConn = new mysqli("localhost","user","password","mydatabase") or die("Error " . mysqli_error($oConn));
$rProvider = $oConn->query("select * from providers where id = $iProviderID");
$aProvider = mysqli_fetch_array($rProvider);
// $aProvider has all the information from the provider that I wanted, so my database connection is working
$oConn->query("insert into bookings (provider_id, provider_rates_id, secret, preferred_date, alternate_date, adults, children, transfer_required, firstname, lastname, email, phone, comments) values
($iProviderID, $iRatesID, $sSecret, '$sDate', '$sAltDate', $iAdults, $iChildren, $iTransfer, '$sFirstNameSQL', '$sLastNameSQL', '$sEmailSQL', '$sPhoneSQL', '$sCommentsSQL'");
$iBookingID = $oConn->insert_id;
I am getting no error and $iBookingID is 0. The row just doesn't get inserted into the database. I have gone through the PHP manual and similar posts on StackExchange, but have not been able to resolve this issue.

typo-u forgot to add single quotes and also a bracket.
$oConn->query("insert into bookings (provider_id, provider_rates_id, secret, preferred_date, alternate_date, adults, children, transfer_required, firstname, lastname, email, phone, comments) values
('$iProviderID', '$iRatesID', '$sSecret', '$sDate', '$sAltDate', '$iAdults', '$iChildren', '$iTransfer', '$sFirstNameSQL', '$sLastNameSQL', '$sEmailSQL', '$sPhoneSQL', '$sCommentsSQL')");

... // $_REQUEST variables processed
$oConn = new mysqli("localhost","user","password","mydatabase") or die("Error " . $oConn->error);
$oConn->query("insert into bookings (provider_id, provider_rates_id, secret, preferred_date, alternate_date, adults, children, transfer_required, firstname, lastname, email, phone, comments) values
('$iProviderID', '$iRatesID', '$sSecret', '$sDate', '$sAltDate', '$iAdults', '$iChildren', '$iTransfer', '$sFirstNameSQL', '$sLastNameSQL', '$sEmailSQL', '$sPhoneSQL', '$sCommentsSQL')");
$iBookingID = $oConn->insert_id;
echo $iBookingID;

there's a typo on your code, you're missing to close the ")" of the values part of your query.
The correct, query:
$oConn->query("insert into bookings (provider_id, provider_rates_id, secret, preferred_date, alternate_date, adults, children, transfer_required, firstname, lastname, email, phone, comments) values ($iProviderID, $iRatesID, $sSecret, '$sDate', '$sAltDate', $iAdults, $iChildren, $iTransfer, '$sFirstNameSQL', '$sLastNameSQL', '$sEmailSQL', '$sPhoneSQL', '$sCommentsSQL');");

If this is not a php error, you can try using echo $oConn->error to get mysql query error!

Related

Statement query php / mysql

My problem is that the value "Arvin", "Tarrega", "Rizal", "Math", "Male" comes from another table which is "student". The value that I have there in the status and date column field comes from a user input. I want to put a statement query which will combine this two into one. Please help me. Btw, the other table doesn't have the status and date field. Only the attendance table has that 2 fields.
table name: attendance
Here is the code I'm using to get that result:
$sql = "INSERT INTO attendance(date, status) VALUES('$_POST[set_date]', '$_POST[status]');
INSERT into attendance(fname, lname, subject, section, gender) SELECT fname, lname, subject, section, gender from student;";
What I would do is to use the fact that you can "select" string in an sql query.
e.g.:
select 'hello' from any_table
In your case, I would do :
$sql = "INSERT into attendance(date, status, fname, lname, subject, section, gender) SELECT '$_POST[set_date]','$_POST[status]',fname, lname, subject, section, gender from student;";
This way you can have all the information you need to insert in one sql query.

Error in executing insertion query

Isn't this query correct?
$insert = INSERT INTO geninfo (S.N, Name, Address, DOB) VALUES ('Suresh','Ratnanagar','1989/04/10');
I got following error, please help I am a beginner.
Parse error: syntax error, unexpected 'INTO' (T_STRING) in
C:\xampp\htdocs\google.php on line 9
$insert = "INSERT INTO `geninfo` (`S.N`, `Name`, `Address`, `DOB`) VALUES ('Suresh','Ratnanagar','Missing address here','1989/04/10');";
Note that I have also corrected your MySQL query. S.N refers to the column named N on the table named S, which I'm pretty certain is not what you wanted.
Also I just realised you have four columns, but only three values. Fixed that too.
You have no quotes, it should be like this:
$insert = "INSERT INTO geninfo (S.N, Name, Address, DOB) VALUES ('Suresh','Ratnanagar','1989/04/10')";
upd
It seems you are storing date of birth as a string, not as a timestamp (or similar) which is not a good idea
You need to give a (NULL or '') for the S.N field and quotes should be given before and after each and every value.
$insert = "INSERT INTO geninfo (S.N, Name, Address, DOB) VALUES
('', 'Suresh','Ratnanagar','1989/04/10')";
Moreover the field name S.N could create problems. Let me know if this works.
$insert = "INSERT INTO geninfo (S.N, Name, Address, DOB) VALUES ('Suresh','Ratnanagar','1989/04/10')";

An error in SQL syntax

The SQL statement is as follows:
$sql = "
INSERT INTO usertable
(
userid,,
name,
username,
password,
typeofuser,
dateofaddition,
createdby,
status
)
VALUES (
$userid,
'$empname',
'$username',
'$password',
'$usertype',
'$doa',
'$createdby',
'$radiobt'
)
";
Remove the comma from after userid and correctly include your variables
$sql = "INSERT INTO usertable
(
userid,
name,
username,
password,
typeofuser,
dateofaddition,
createdby,
status
)
VALUES
(
".$userid.",
".$empname.",
".$username.",
".$password.",
".$usertype.",
".$doa.",
".$createdby.",
".$radiobt."
)";
Make sure you prepare this statement before getting it into the DB! (more info: Prepared statement (Wikipedia))
There is a double ,, after userid.
$sql = "INSERT INTO usertable(userid,name,username,password,typeofuser,dateofaddition,createdby,status) VALUES ($userid,'$empname','$username','$password','$usertype','$doa','$createdby','$radiobt')"
Let us take things step by step:
Field names in the query match the field name in the table (they should exactly be the same even the case should match).
What and from where are the values for the varaibles getting generated?
Do the values correspond to the datatype of the fields in the table?
For eg. If there is a variable which expects to receive integer as values and it is made to store strings or characters, mysql is bound to give an error message.
Check for these and let me know.

SQL / PHP Insert columns form one table to another

Hi I working on simple newsletter script, I got two groups there with two different tables for each group.
I want to move subscribers from one group to another, I put this query and It works:
$sql = "INSERT INTO newsletter_subscribers2 SELECT * FROM newsletter_subscribers WHERE id='".$_GET['nid']."'";
However after moving some previous subscribers I got error with duplicated entry key, as I supose query is moved with ID, so I want just move, name, lastname and email.
$sql = "INSERT INTO newsletter_subscribers2 VALUES (firstname, lastname, email) SELECT newsletter_subscribers.firstname, newsletter_subscribers.lastname, newsletter_subscribers.email FROM newsletter_subscribers WHERE id='".$_GET['nid']."'";
However this part of code doesnt seemst to work as I got syntax error, can anyone help me with this I will be very happy.
Thanks for reply
You have to remove values
$sql = "INSERT INTO newsletter_subscribers2(firstname, lastname, email)
SELECT newsletter_subscribers.firstname, newsletter_subscribers.lastname,
newsletter_subscribers.email
FROM newsletter_subscribers WHERE id='".$_GET['nid']."'";
Remove the VALUES
$sql = "INSERT INTO newsletter_subscribers2 (firstname, lastname, email)
SELECT newsletter_subscribers.firstname, newsletter_subscribers.lastname,
newsletter_subscribers.email
FROM newsletter_subscribers WHERE id='".$_GET['nid']."'";
Edit: And always use mysql_real_escape_string to prevent SQL-injection:
$sql = "... WHERE id='".mysql_real_escape_string($_GET['nid'])."'";
Try this:
insert into newsletter_subscribers2 (col1, col2, col3, etc) select col1, col2, etc from newsletter_subscribers2 where 1 = 1

Basic insert into SQL

I know this is really simple i haven't touched PHP and SQL in a few years.
This works
$mysqli->query("INSERT INTO sonyCES2013.registration (id,
firstName, lastName, eMail, telephone, outlet, comfirm,
preferTime, date) VALUES (NULL,Cat, 'Catherine',
'Cat#gmail.com', '123-456-4561', 'Some Text', 'Yes', '4:00pm'
,'1/09/14')");
this doesn't work
$mysqli->query("INSERT INTO sonyCES2013.registration (id,
firstName, lastName, eMail, telephone, outlet, comfirm,
preferTime, date) VALUES
(NULL,{$fName},{$lName},{$eMail},{$telephone},{$outlet},{$comfirmation},{
$preferTime},{$day})");
help and yes i did check that the variables aren't empty and i did try without the `` between each {}
You need to add ' ' to the variables to make the interpeter able to understand what you are trying to do ( in this case passing some php variable as parameters)
Use this and see if it works:
$mysqli->query("INSERT INTO sonyCES2013.registration (id, firstName, lastName,
eMail, telephone, outlet, comfirm, preferTime, date) VALUES
(NULL,'$fName','$lName','$eMail','$telephone','$outlet',
'$comfirmation','$preferTime','$day')");
It won't work that way because the resulting SQL looks something like this:
$mysqli->query("INSERT INTO sonyCES2013.registration (id, firstName, lastName, eMail, telephone, outlet, comfirm, preferTime, date) VALUES (NULL,Cat, Catherine, Cat#gmail.com, 123-456-4561, Some Text, Yes, 4:00pm ,1/09/14)");
Note the lack of single quotes around the strings, which make the SQL invalid.

Categories