Problems with mysqli_query function [duplicate] - php

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')";
^^^^

Related

Error querying database using php on XAMPP server [duplicate]

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

Insertion and Create SQL statement not working [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(2 answers)
Closed 5 years ago.
I´m new in php programming and so i tried to connect my php file with an sql database. It´s working till i come to the point were i want to use a query and execute them. Can someone please help me why i always get "Error querying database"?
$query = "INSERT INTO user (surname, name, e-mail, password) VALUES ('$text', '$text2', '$text3', '$text4')";
$query2 = "CREATE TABLE $text3 (
name VARCHAR(30) PRIMARY KEY,
password VARCHAR(30))";
//make the query
$result = mysqli_query($db, $query) or die('Error querying database.');
$result2 = mysqli_query($db, $query2) or die('Error querying database1.');
I am defenitely connected with the database before.
My second question is the right use of the Create Table statement. I want to create a table which is named like the users E-mail address. Is this the right usage?
CREATE TABLE $text3 (
name VARCHAR(30) PRIMARY KEY,
password VARCHAR(30))";
I especially want to know if i need to set ' before the $text3 or not.
I solved this Problem with the help of #FunkFortyNiner the problem is the - between the e-mail. I neededt to remove it.
Now the code looks like this:
$query = "INSERT INTO user (surname, name, email, password) VALUES ('$text', '$text2', '$text3', '$text4')";
$query2 = "CREATE TABLE $text3 (
name VARCHAR(30) PRIMARY KEY,
password VARCHAR(30))";
//make the query
$result = mysqli_query($db, $query) or die('Error querying database.');
$result2 = mysqli_query($db, $query2) or die('Error querying database1.');
Use
die('Error querying database.' . mysqli_error($db) );
To know about the exact error.
More specifically use e_mail or email instead of e-mail as the column name in your db schema.

import CSV file into database using HTML and Php [duplicate]

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) ;

Multiple mysqli_query not executed in same connection without closing connection

I have an issue with the following statement. No error is produced but my second mysqli_query is not being executed. In my previous project, I was forced to close and reopen the connection to the database before executing a new query but I'd like to believe that it's not very smart to close and reopen connections to a database several times in a script. Is there a way to go around this ?
Secondly, I've included my connection script in a separate file. So, should I close it after the first mysqli_query, how do I reopen it to be able to execute the second(subsequent) mysqli_query? I've thought of creating my own function that'll reopen the connection but I want to stay away from opening and closing database connections several times in a single script if possible.
$query = "SELECT * FROM customer WHERE email = '$email'";
$data = mysqli_query($dbc, $query) or die (mysqli_error($dbc));
if (mysqli_num_rows($data) == 0){
$queryInsertMember = "INSERT INTO customer VALUES ('$email', '$name', '$surname', SHA('$confirmedPassword'), '$gender', '$telMobile')";
mysqli_query($dbc, $queryInsertMember) or die (mysqli_error($dbc));
$queryInsertAddress = "INSERT INTO address VALUES ('$email', '$hzNumber', '$streetName', '$suburb', '$city', '$postalCode', '$province' , '')";
mysqli_query($dbc, $queryInsertAddress) or die (mysqli_error($dbc));
} else {
echo 'member already exists';
}

Multiple Table INSERT Queries in single SQL Statement [duplicate]

This question already has answers here:
PHP + MySQL transactions examples
(9 answers)
Closed 9 years ago.
I am trying to insert queries into multiple tables using the following code. I have tried without the TRANSACTION and it will not work, individually they work. Any help would be much appreciated.
Thanks in advance.
$query = mysql_query("BEGIN;
INSERT INTO `uc` (`ANO`, `CNO`, `P`) VALUES ('$ano', '$cno', '$p');
INSERT INTO `ct` (`ANO`, `CNO`, `RNO`) VALUES ('$ano','$cno','$rno');
COMMIT;");
$query_run = mysql_query($query);
$query = "BEGIN";
mysql_query($query) or die (mysql_error());
$query = "INSERT INTO `uc` (`ANO`, `CNO`, `P`) VALUES ('$ano', '$cno', '$p')";
mysql_query($query) or die (mysql_error());
$query = "INSERT INTO `ct` (`ANO`, `CNO`, `RNO`) VALUES ('$ano','$cno','$rno')";
mysql_query($query) or die (mysql_error());
$query = "COMMIT";
mysql_query($query) or die (mysql_error());

Categories