This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
i try this code to import a CSV file into my database but i got this error : Warning: mysqli::query(): Empty query
$db = new mysqli('localhost','root','', 'BD_Conference');
$sql=mysql_query("INSERT INTO tbl_conference (pid, name, chairs,keynote, abstract, speaker, affiliation, ville, pays, salle, date, time, session, image_url) VALUES ('','$champs1','$champs2','$champs3','$champs4','$champs5','$champs6','$champs7','$champs8','$champs9','$champs10','$champs11','$champs12','$champs13')");
$result = $db-> query($sql) ;
check proper mysqli connection code should be
$db= mysqli_connect('localhost','root','', 'BD_Conference');
$sql=("INSERT INTO tbl_conference (pid, name, chairs,keynote, abstract, speaker, affiliation, ville, pays, salle, date, time, session, image_url) VALUES ('','$champs1','$champs2','$champs3','$champs4','$champs5','$champs6','$champs7','$champs8','$champs9','$champs10','$champs11','$champs12','$champs13')");
$result = mysqli_query($db,$sql) ;
please use mysqli_query instead of mysql_query. So the connection is opened using mysqli.
$db = new mysqli('localhost','root','', 'BD_Conference');
if ($db->connect_errno) {
echo "Errno: " . $mysqli->connect_errno . "\n";
}
$sql="INSERT INTO tbl_conference (pid, name, chairs,keynote, abstract, speaker, affiliation, ville, pays, salle, date, time, session, image_url) VALUES ('','$champs1','$champs2','$champs3','$champs4','$champs5','$champs6','$champs7','$champs8','$champs9','$champs10','$champs11','$champs12','$champs13')";
$result = $db->query($sql) ;
Related
This question already has answers here:
How to include a PHP variable inside a MySQL statement
(5 answers)
Closed 3 years ago.
I'm making a quiz and once the calculation of the grade is finished, I want to add that data to test_attempt table.
Here's its structure.
Here's the code of the query:
<?php
$connection = mysqli_connect("localhost", "root", "", "vartvald");
if ($connection->connect_error) {
die("Connection failed:" . $connection->connect_error);
}
$user=$_SESSION['user'];
$userid=$_SESSION['userid'];
$sql = "INSERT INTO test_attempts (date, id, mark, top_mark, fk_user, fk_test) VALUES
('',null,'$grade','$top_grade','$userid','$fk');";
var_dump($sql);
$connection->close();
?>
What am I doing wrong?
You have few mistakes. Your main problem is that you never prepared any query and never executed it. To do it you need to use prepare(), bind_param(), and execute(). Also you are not opening the mysqli connection correctly and your error checking will never work (Please read: Should we ever check for mysqli_connect() errors manually?)
After fixing your errors your code would look something like this:
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connection = new mysqli("localhost", "root", "", "vartvald");
$connection->set_charset('utf8mb4');
$user = $_SESSION['user'];
$userid = $_SESSION['userid'];
$stmt = $connection->prepare('INSERT INTO test_attempts (date, id, mark, top_mark, fk_user, fk_test) VALUES(NULL,NULL,?,?,?,?)');
$stmt->bind_param('ssss', $grade, $top_grade, $userid, $fk);
$stmt->execute();
I have not validated whether your SQL is correct in itself, but if you have error reporting switched on, PHP should tell you if you have a mistake.
Your code will never add data in the database because you aren't calling any funciton that insert data:
$sql = "INSERT INTO test_attempts (date, id, mark, top_mark, fk_user, fk_test) VALUES
('',null,'$grade','$top_grade','$userid','$fk');";
var_dump($sql);
// missed code to insert data in the database
$connection->close(); // here you close the connection
Before closing the connection, call mysqli_query:
mysqli_query($connection,"$sql");
Try this:
$sql = "INSERT INTO test_attempts (date, id, mark, top_mark, fk_user, fk_test) VALUES
(CURRENT_TIMESTAMP,null,'$grade','$top_grade','$userid','$fk');";
Try the following, here you can see that the CURRENT_TIMESTAMP is passed as first params for data and also below the $sql you can see the mysqli_query which is useed here to execute the insert query.
$sql = "INSERT INTO test_attempts (date, id, mark, top_mark, fk_user, fk_test) VALUES
(CURRENT_TIMESTAMP,null,'$grade','$top_grade','$userid','$fk');";
mysqli_query($connection, $sql);
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 4 years ago.
I'm attempting to connect my first php script to the mysql database. I'm using XAMPP as my server.
I keep getting the message "error querying database".
Bellow is my code. I really would appreciate the help.
//Below is how you connect to a database and insert data
//First create a variable with the connection commands so the query function you will use in a moment won't be so long
$dbc = mysqli_connect('localhost', 'root', '', 'aliendatabase') or die ('Error connecting to MySQL server.');
//Next create a variable to hold your query commands for the same reason
$query = "INSERT INTO aliens_abduction (first_name, last_name " .
"when_it_happened, how_long, how_many, alien_description, " .
"what_they_did, fang_spotted, other, email) " .
"VALUES ('Sally', 'Jones', '3 days ago', '1 day', 'four', " .
"'slimmy', 'asked questions', " .
"'yes', 'I may have seen your dog. Contact me.', " .
"'sally#gregs-list.net')";
//Now the variable that holds the query function
$result = mysqli_query($dbc, $query)
or die ('Error querying database.');
You can use
INSERT INTO table SET a=1, b=2, c=3
to avoid any confusion
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
I'm working on a little browser game, but when I have a condition in connection to MySQLi database, it doesn't work.
In else closure it should write $name, but it doesn't.
if ($conn->connect_error){
die("Connection failed: ".$conn->connect_error);
}
else{
//IF CONNECTION IS GOOD, GET DATA FROM DATABASE
$query = "SELECT name, separator, description, maintenance FROM configuration";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$name = $row['name'];
//this ↓↓↓
echo $name;
}
Use back ticks (``) when using reserved words.
From,
$query = "SELECT name, separator, description, maintenance FROM configuration";
To,
$query = "SELECT name, `separator`, description, maintenance FROM configuration";
Try with backticks:
$query = "SELECT name, `separator`, description, maintenance FROM configuration";
This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(2 answers)
Closed 7 years ago.
i'm new to PHP and MySQL. I'm having issues with one of my mysqli_query functions. I have a database connection that works perfectly as the first half of my php file actually stores data into certain tables of the database.
The problem starts when i want to perform another query against the database. Here's the code:
// INSERT statements.
$queryMember = "INSERT INTO member (surname, name, gender, email, telHome, telMobile, dob, studentNum, idNum) VALUES ('$sur_name', '$f_name', '$gender', '$email', '$tel_home', '$mobile_num', ,'$date_of_birth', '$studentNumber', '$id_number')";
$queryAddress = "INSERT INTO physicalDetails (houseNum, unitNum, streetAdd, suburb, city, province, code ) VALUES ('$house_number', '$unit_number', '$street_address', '$suburb_name', '$city_name', '$province_name', '$zip_code')";
$queryStaff = "INSERT INTO staff (staffID ) VALUES ('$staff_id')";
$queryStudent = "INSERT INTO student (major ) VALUES ('$student_major')";
// Statements that must query the database.
$resultMember = mysqli_query($dbc, $queryMember) or die ('Error while inserting data into member details table');
$resultAddress = mysqli_query($dbc, $queryAddress ) or die ('Error while inserting data into member physical details table');
$resultStaff = mysqli_query($dbc, $queryStaff ) or die ('Error while inserting data into staff information table' );
$resultStudent = mysqli_query($dbc, $queryStudent ) or die ('Error while inserting data into student major details table');
mysqli_close($dbc);
When i run my form, my first error is the following: "Error while inserting data into member details table".
I don't understand why it's giving me an error. Any advice or suggestions would be appreciated. :)
Don't output a fixed (and useless) error message: Have the DB tell you what you did wrong:
$resultMember = mysqli_query($dbc, $queryMember) or die (mysqli_error($dbc));
^^^^^^^^^^^^
If you had that, you'd have been told about your syntax errors:
$queryMember = "[..snip..], '$mobile_num', ,'$date_of_birth', '$studentNumber', '$id_number')";
^^^^
This question already has answers here:
How do I get the last inserted ID of a MySQL table in PHP?
(16 answers)
Closed 7 years ago.
I'm inserting a row in the following way:
require("localhost_credentials.php");
$conn = new mysqli($db_servername, $db_username, $db_password, $db_name);
if($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$q_title = $fixed_title;
$q_tags = $_POST['tag_input'];
$q_mod = "n";
$q_t_create = date("m/d/Y # G:i:s");
$q_t_modified = date("m/d/Y # G:i:s");
$querystr = "INSERT INTO mytable (title, tags, moderator, time_created, time_last_modified) ";
$querystr .= "VALUES (?, ?, ?, ?, ?);";
$statement = $conn->prepare($querystr);
$statement->bind_param("sssss", $q_title, $q_tags, $q_mod, $q_t_create, $q_t_modified);
$statement->execute();
I would like to get the id of the row I just inserted without having to do a second query. I've seen a few methods to do this on SO, but every time there's a debate as to which way it should and should not be done and I'm kind of confused.
Using prepared statements, how do I get the id of a newly inserted row using only one query?
As long as you do not execute multi insert, you can use
$conn->insert_id
It is populated automatically when a statement created from that connection executes INSERT query.
you can use something like this :
$last_id = $statement->insert_id($conn);
this will return the last inserted row id .