I have been following a solution (How to insert json array into mysql database) into how to insert JSON data into a MySQL table. The code seems to run okay but it is still returning an empty data set.
I'm currently testing a hypothesis and my JSON file has 100,000 records in it. I have created a similar JSON file with just three records thinking that the size of the file may have been impeding it but that didn't work either.
I have checked my database name, table name and row names but is correct with the code shown. Any pointers?
<?php
$json = file_get_contents('E:\xampp\htdocs\MOCK_DATA.json');
$obj = json_decode($json, true);
$connection = mysqli_connect("localhost", "root", "");
mysqli_select_db($connection, "et_test") or die('Couldnt connect database');
foreach($obj as $item)
{
mysqli_query($connection, "INSERT INTO 'et_test'.'test' (first_name, last_name, colour, country, city)
VALUES ('".$item['first_name']."','".$item['last_name']."','".$item['colour']."','".$item['country']."','".$item['city']."')");
}
mysqli_close($connection);
?>
As per OP's wish to close the question and be marked as solved:
This is the problematic piece of code'et_test'.'test'
Use backticks, because using quotes around table names are the wrong identifiders:
http://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.html
SQL
mysqli_query($connection, "INSERT INTO `et_test`.`test` ...
Use or die(mysqli_error($connection)) to mysqli_query(), in order to get the "real" error.
Consider getting into using prepared statements, or PDO with prepared statements.
As it stands, your present code is open to SQL injection.
Related
This should be pretty simple, I am just new to PHP...
If I have this..
http://localhost/write_data.php?value=99 and inside the write_data.php is this...
$dbconnect = mysqli_connect($server, $dbusername, $dbpassword);
$dbselect = mysqli_select_db($dbconnect, "plantData_001");
// Prepare the SQL statement
$sqlAddress = "INSERT INTO sensorData (value) VALUES ('".$_GET["value"]."')";
// Execute SQL statement
mysqli_query($dbconnect, $sqlAddress);
everything works fine and 99 is appended into the data table. But how do I add multiple values? I have a column named id that needs to be called like this...
http://localhost/write_data.php?id=1234567;value=99
Do you want to insert multiple values?
You should accept the new way to pass values by script query param.
Something like that: http://localhost/write_data.php?values=99,100,88,20,44.
Then you should use i.e. explode function to split values into array and would be able to insert multiple rows.
BUT - you should implement also the functionality to secure your input from query param. Please, read about SQL injection and MySQLi - https://stackoverflow.com/a/16282269/8890700
This question has kinda been asked already but I couldn't find my answer. I searched a while and found these related questions, but they didn't help me to understand or answer my problem.
SQL Insert Into with Inner Join
T-SQL INSERT INTO with LEFT JOIN
My question is how to insert data in 2 tables using joins. For example (with php) a user can enter his/her name and the foods he/she likes.
I store them in a variable and an array (the length of the array is not always 3 like below):
$name = "Niels"
$foodsHeLikes = array("apple", "pear", "banana");
This is how I want to store them:
USERS:
UserID name
1 Niels
FOODS:
FoodID userID name //userID is linked to UserID in users table
1 1 apple
2 1 pear
3 1 banana
The link to the first question I pasted above has an insert with a join but I don't see anywhere to put the values in like with a normal insert?
The query from that question:
INSERT INTO orders (userid, timestamp)
SELECT o.userid, o.timestamp FROM users u INNER JOIN orders o ON o.userid = u.id
Judging by what's been going on in the comment section, what you're asking is that you would like to have a more optimal query process. Right now you are using two different queries to populate your two tables, and you're wondering whether that could be done more optimally.
First things first, it's not possible to populate TWO different tables with ONE query.
However, what you could do, is use transactions.
The rest of this answer will follow the assumption that you are using PHP as your backend scripting language (as you tagged yourself).
Also, it is not inherently obvious whether you use prepared statements for your queries or not. In the case you don't, I would highly recommend using prepared statements. Otherwise, you're opening yourself up to SQL Injections (SQLI Attacks).
I will proceed by using mysqli prepared statements in this answer.
<?php
// Your input post variables
$name = $_POST['name'];
$foodArray = $_POST['foodArray'];
/*
I'm using a function to handle my queries,
simply because it makes large piles of code easier to read.
I now know that every time the function:
createUserAndFood($name, $foodArray);
is called, that it will populate my user and food table.
That way I don't have to worry about writing all the code multiple times.
*/
function createUserAndFood($name, $foodArray){
// food array values
$foodValues = array_values($foodArray);
// DB variables
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
/*
Stops the query from auto commiting,
I'll explain later, you can "maybe" disregard this.
*/
$conn->autocommit(FALSE);
// Declare the query
$sql = "INSERT INTO userTable(name) VALUES(?)";
// Prepare and bind
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $name);
// Execute the query
$stmt->execute();
// Fetch last inserted id
$lastID = $conn->insert_id;
$sql = "INSERT INTO foodTable(userId, food) VALUES(?, ?)";
$stmt = $conn->prepare($sql);
for($i = 0; $length = count($foodValues) > $i; $i++){
$stmt->bind_param("is", $lastID, $food);
$food = $foodValues[$i];
$stmt->execute();
}
// Commits the query / queries
$conn->commit();
// Close connection
$stmt->close();
$conn->close();
}
?>
Since you wanted to optimize your queries, the general idea that we are using here, is that we are making use of the MySQL function LAST_INSERT_ID(); via PHP and store it into a variable.
Now, this is mainly relevant if you are using auto incremented id's. If you are not, you can disregard this specific logic and use something else. But if you are, then keep reading.
The reason why we are storing the last id into a variable is because we need to use it multiple times (the new user might have more than one favorite food afterall). If you were not to store the last id into a variable, it would instead take the auto incremented value of the second table after the initial insert, which means upon your third insert statement and forward, you would be working with the wrong id.
Now, as I promised to explain, the reason I'm using $conn->autocommit(FALSE); and $conn->commit(); is because you might not want incomplete data sets in your database. Imagine that a user input is happening, but your database crashes in the middle of it all. You'll have incomplete data sets. If this is not really a concern of yours, then you can disregard that.
To simplify what's going on at the MySQL side of things, think of it like this:
BEGIN;
INSERT userTable SET name = '$name';
SET #lastID = LAST_INSERT_ID();
INSERT foodTable SET id = #lastID, food = '$food';
COMMIT;
I have read through some of the related questions but I still don't understand. So I decided to post a question here. I have a query like below
$query = "INSERT INTO Table (q1,q2,q3,q4....q25) VALUES ('".$_POST['Q1']."',
'".$_POST['Q2']."',
'".$_POST['Q3']."',
'".$_POST['Q4']."'.....)";
mysql_query($query);
The $_POST['Qx'] value is obtained from a survey where people are allowed to type in comments. Often people would like in words like "don't, can't, doesn't ...". The apostrophe will cause the problem when inserting the data to the table.
I have read through some articles suggesting the method of
mysql_real_escape_string($_POST['Q1'])
But I have 25 questions, some of them even have sub-questions.. so I have around 70 data to input to the table.
Is there any good method that I can adopt to be able to pass apostrophes into MySQL table?
edit: so I have around 70 data to input to the table.
70 is "nothing " for a for-loop ;-)
for($i=1; $i<71; $i++) {
$params[$i] = mysql_real_escape_string($_POST['Q'.$i], $link);
}
But you might want to consider a redesign of your database tables.
You have to encode/escape the parameters. In case of the (deprecated) mysql extension that would be mysql_real_escape_string()
$link = mysql_connect(...);
...
$query = sprintf(
"
INSERT INTO
Table
(q1,q2,q3,q4....q25)
VALUES
('%s','%s' ...)
",
mysql_real_escape_string($_POST['Q1'], $link),
mysql_real_escape_string($_POST['Q2'], $link)
...
);
mysql_query($query, $link) or die(mysql_error($link));
but better use a supported extension like mysqli or pdo.
And take a look at prepared statements.
So i have php code to insert form data in a table. Here's the code:
$link = #mysql_connect("***", "***", "****");
if (!$link) {
echo "save_failed";
return;
}
mysql_select_db("***", $link);
$sql="INSERT INTO Conference (`First Name`, `Last Name`)
VALUES ('$_POST[fname]', '$_POST[lname]')";
mysql_close($link);
The *** are replaced with the actual values in the real code, obviously. But is there anything wrong with the above code? I tried to run it, it didn't have any errors with connection but it also didn't insert anything. Here's is what my mysql table looks like:
Also, I need the table to have an auto incremented number so that each entry is unique with it's own index value. Any ideas on either problem? Thanks
You haven't executed the query, which should be done as it follows:
mysql_query($sql, $link);
Also, please consider using mysqli or even better PDO as the mysql package is deprecated (see the red box), i.e. mysql_query().
I want to load data from several CSV documents in a database.
The problem is that there is a table for a file, thus the table name has to be stored in a variable progressively updated by the loop. The tables are created, but after that the script fails; Nothing happens, my tables and their fields always have 0 lines.
I think that my idea is feasible, because some topics describe some tips in this way, but nothing works in my case.
This is my code that fails:
$query = "INSERT INTO". $name_of_the_file." (id, content) values (". $arrayGenre[0].",'".trim($arrayGenre[1])."')";
where $name_of_the_file refers to the table created in the loop, referring ultimately to the file targeted by the loop.
Following your recommendations, I added a space and print the query by:
echo ("<br>". $query);
Here is an example for the first line. No offense for the xxx substitutions: these are nominative data.
INSERT INTO names.csv (id, url, content) values (1,'xxx_some_adressxxx,'agency: xxx')
I do not know enough PHP to say that it is the expected result, but that seems a good SQL request and the values are fine.
Here are the next lines of my script:
$mysqli->query($query);
and at the beginning of my script, the definition of $mysqli:
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
New edit :
Well it was in fact SQL code which was badly written...
Try adding a space:
$query = "INSERT INTO ". $name_of_the_file ...
It's always worth outputting your SQL via echo to make sure it looks like what you expect it to look like.
Edit:
INSERT INTO names.csv (id, url, content) values (1,'xxx_some_adressxxx,'agency: xxx')
You need to add an apostrophe at the end of the second field:
INSERT INTO names.csv (id, url, content) values (1,'xxx_some_adressxxx','agency: xxx')
I'd also recommend that you look into things like PDO, to make your code more robust and more secure.
You can also try pasting your SQL directly into the database, to see if it gives you an error.