I need to figure out a way to prepare an insert statement in PHP for a SQL db and I'm having trouble comprehending the logic. The insert statement will be from data I'm receiving in JSON. The JSON contains questions and answers. The table I'm inserting them into is the problem. On the front end (website) the table has column names that represent the questions and it look like this.
Date | Address | City | Zip
Now the problem is in the database these names don't carry over. The column names are just column1 column 2 etc. So it looks like this
Column1 | Column2 | Column3 | Column4
Another thing - when each table like this is created it will have different values so I can't hard code the relationship. Also these tables are created from importing an excel spreadsheet from the front end (website). So I came up with a plan to duplicate row 1 & 2 of the spreadsheet so that now when you import them the database looks like this
id | Column1 | Column2 | Column3 | Column4
1 | Date | Address | City | Zip
So now I have some reference to the values that will need to be inserted. Now the next steps involved will be.
Query for column name based on the data in row 1 OR change the column name to the data in row 1.
Insert data into the correct columns based on the query
So in summary I cannot insert the answers into the correct column representing the question without the proper information. I personally think changing the column names in PHP would be the better option. However I'm pretty new to PHP and SQL so I don't even know if this is possible. Any help is appreciated
EDIT
to give a slightly better understanding of the problem I am posting the PHP I currently have.
$json = safe($_POST['assessment']);
/*
$json =
'{
"info":
[
"Date: 6-22-13",
"Phone #: (555) 555-5555",
"Address: 304 N. SCOTTSDALE RD.",
"City: SCOTTSDALE",
"Zip: 85251",
"State: AZ"
]
}';
*/
$data = json_decode($json,true);
$collection = array();
foreach($data['info'] as $piece) {
$info = explode(':', $piece);
$collection[] = array('question' => $info[0], 'answer' => $info[1]);
}
echo '<pre>'; print_r($collection); echo '</pre>'
The following should create an associative array that maps the names of columns to the column numbers in the database schema:
$stmt = $db->prepare('select Column1, Column2, Column3, ... from table where id = 1');
$result = $stmt->execute();
$data = $result->fetch(PDO::FETCH_ASSOC);
$col_map = array_flip($data);
You can then use this when preparing future queries of the database.
If you don't know how many columns you could have, you could do this:
$stmt = $db->prepare('select * from table where id = 1');
$result = $stmt->execute();
$data = $result->fetch(PDO::FETCH_ASSOC);
unset($data['id']); // Remove the `id` column, which doesn't conform to the ColumnN pattern
$col_map = array_flip($data);
Related
I am trying to use the selected id's as an array a other statement. It seems it is not counting all the result as it is much lower that it is.. I have tried to find my answer on google but none of the options are working for me or i do not know how to use them in my case. There are no errors and i have error log on!
Here is my code, what am i doing wrong?
$counttheid = array();
$stmt3 = $mysqli->prepare("SELECT
id
FROM account
WHERE level <= '5' AND door = ? AND `group_name` = ? AND betaald = 'Yes'");
$stmt3->bind_param("ss",$usernamesession,$groupname);
$stmt3->execute();
$result3 = $stmt3->get_result(); //only works when nd_mysli is set on the server!
while ($rowid = $result3->fetch_assoc())
{
$counttheid[] = $rowid['id'];
$countid = implode(',', $counttheid);
}
$sql = "SELECT SUM(mobcash) AS totalcash FROM account WHERE id IN (?)
";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s",$countid);
$stmt->execute();
$stmt->bind_result($row['totalcash']);
while($stmt->fetch()) $sumcash = $row['totalcash'];
//echo print_r($counttheid);
//echo implode(',', $counttheid);
echo $sumcash;
I am no profesional developer just started learning this, any help is welcome!
Since you have edited the question, my original answer is no longer relevant.
I suggest for you to simplify your two queries into a single query. In your first query you select a bunch of ids and in the second query you sum a different value from the same table using the ids. You can just to that in one query:
SELECT SUM(mobcash) AS totalcash
FROM account
WHERE level <= '5'
AND door = ?
AND `group_name` = ?
AND betaald = 'Yes';
Original answer
You use $result->fetch_all(MYSQLI_ASSOC), meaning each row from the result set will be an associative array with the column names as the keys and the cell values as values. That is also the case, if you only select one column.
That means for this example table
id | name | balance
----+------+---------
1 | acc1 | 12.34
2 | acc2 | 1.23
your variable $dataid will have the following value (for the simplified query SELECT id FROM account):
$dataid = [
[
"id": 1
],
[
"id": 2
]
];
To get more familiar with PHP, you could write some foreach loops yourself, but you can also use the built-in PHP function array_column (php.net: array_column):
$ids = array_column($dataids, "id");
From an SQL perspective I would also suggest for you to learn about nested queries, since you could avoid this PHP logic altogether.
This question already has answers here:
mysqli query results to show all rows
(4 answers)
Closed 4 years ago.
i've been trying for quite some time already to get this working, but no success at all
I looked around many places, and by what i understood, the method im using isnt retrieving the data i want, but in fact, ALL the data inside the DB (despite there only 1 value to be returned)
This is my code right now:
$query = "SELECT id from produtos where tipo = 'Tubo' and inteiro_pedaco = '$tipo' and marca = '$marca' and comprimento = '$comprimento' and diaexterno = '$externo' and diainterno = '$interno'";
$result = $conec->query($query);
echo $result;
die;
At the code above im trying to retrieve ID from a table named produtos
And here is the table 'produtos' content:
id: 102 | tipo: Tubo | inteiro_pedaco: Inteiro | marca: Science | comprimento: 1000 | diaexterno: 1 | diainterno: 1 |
id: 103 | tipo: Whatever | inteiro_pedaco: Whatever | marca: Whatever | comprimento: Whatever | diaexterno: Whatever | diainterno: Whatever |
etc...
$result variable was supposed to retrieve "102"
After retrieving 102, i want to echo it just for tests purposes
However, if i can manage to make it work and echo "102", my next step is making an insert into ANOTHER table with $result content, which is "102"
I want to insert at entrada_produtos table some data with the following command:
mysqli_query($conec,"INSERT INTO entrada_produtos (fk_id, usuario, data_inclusao, qtd) VALUES ('$result', '$usuario', now(), '$qtd')");
Any help would be appreciated, plus, i dont want just some code working, i would like to understand how it works
If possible, try to explain any code posted bellow, it would be of great help, also, i want to make it as simple as possible, i dont wanna use like 10 lines of code just to retrieve some data into a variable (if its the only possible way, then there's nothing i can do, but go this way...)
Thanks in advance
You need to fetch the results:
$row = $result->fetch_assoc();
$id = $row['id'];
echo $id;
fetch_assoc() returns the next row of results as an associative array.
You can then use the $id variable in your INSERT query.
There's no need to use two queries to insert this into another table, you can do that with one query.
$stmt = $conec->prepare("
INSERT INTO entrada_produtos (fk_id, usuario, data_inclusao, qtd)
SELECT id, ?, now(), ?
FROM produtos
where tipo = 'Tubo'
and inteiro_pedaco = ?
and marca = ?
and comprimento = ?
and diaexterno = ?
and diainterno = ?");
$stmt->bind_param("sssssss", $usuario, $qtd, $tipo, $marca, $comprimento, $externo, $interno);
$stmt->execute();
I've rewritten this as a prepared statement to prevent SQL injection. The ? in the query are placeholders, which are filled in with the variable values given in the call to bind_param().
BTW, if you're selecting the row that you just inserted into produtos, you can use the MySQL function LAST_INSERT_ID() or the PHP variable $conec->insert_id to get the auto-increment ID that was assigned, you don't need a query for that.
I have a two columns in database:- copy (which is array) and bookid. My code is;
$query = mysqli_query($db, "select copy from book_outward WHERE bookid like 'B1'");
while ($row = mysqli_fetch_array($query)) {
$copyid = $row['copy'];
}
Database shows like this
+----------------+
| id copy bookid |
+----------------+
| 1 1 B1 |
| 2 2,3 B1 |
| 3 4 B1 |
| 4 2 B2 |
+----------------+
but it stores only last values which was entered in 'B1'. I also tried
$copyid[] = $row['copy'];
but in this case I have to change array keys manually every time.
My aim is to insert copy into column bookid='B1' and before it has to make sure that only UNIQUE values can be stored in database for B1.
HTML :-
<input type="text" name="bookid" />
<input type="text" name="copies[]" />
PHP code for inserting:-
$book_id = $_POST['bookid'];
$copies = implode(',',$_POST['copies']);
$result = mysqli_query($db, "insert into book_outward(bookid,copy) values ('$book_id','$copies')");
As some of the comments mention it it not the best way to to it but it is possible.
You can obtain all the copyid data by:
$query = mysqli_query($db, "select copy from book_outward WHERE bookid like 'B1'");
$copyid = "";
while ($row = mysqli_fetch_array($query)) {
$copyid .= $row['copy'] . ",";
}
$copyidsFromDB = explode(",",rtrim($copyid , ','));
After that you can check if what you got in the request are in there using array_intersect:
$copies = $_POST['copies']
// if not an array use: $copies = explode(",", $_POST['copies'])
if (count(array_intersect($copies, $copyidsFromDB) == 0)
// insert to DB
Solved by myself by adding one line only
`$copies2 = explode(",",rtrim($copies , ','));`
before array_intersect Thanks code helps greatly.
So, sounds to me like the issue is the original data DB schema has some issues.
A sql-like DB is not a great place to put 'array' style value. It already has a mechanism for doing this: a link table.
That'd save you the trouble of having to manually shuffle around your primary keys in this table. It's way easier to check and validate.
If you HAVE to do this via php code, for example you don't have SQL access, like in a controlled build environment, you should take advantage of 'string keys' to on your $copyid, (hint rename to $copyidmap) to group together like pieces of data and preserve key relationships.
so the following would be unique:
map->{bookId}->{copyid}->{id} // Where id is that primary table id. Obviously, validate and do your undefined array assignments.
I have a database table with the following structure. id column is added later. So all id cells are empty.
id | song | album | artist
-----------------------------
| song1 | alb1 | art1
| song2 | alb2 | art2
| song3 | alb3 | art3
| song4 | alb4 | art4
| song5 | alb5 | art5
I have an array which holds the id values for the table. I'll be updating the table (the id column) with the values in the array. For example:
$array = [
"C45Rm3fLGn",
"ocIik81up2",
"IcuSn9T77y",
"tJv7AbF53r",
"a9eZ6xYM5Y",
];
These items are unique random strings.
How should I proceed? I'm thinking about iterating the array and using UPDATE on each item.
$array = [
"C45Rm3fLGn",
"ocIik81up2",
"IcuSn9T77y",
"tJv7AbF53r",
"a9eZ6xYM5Y",
];
$rows = $mysqli->query("SELECT * FROM songs")->fetch_all(MYSQLI_ASSOC);
for ($i = 0; $i < count($array); $i++) {
$row = $rows[$i];
$id = $array[$i];
$mysqli->query("UPDATE songs SET id = '$id' WHERE song = '{$row["song"]}' AND artist = '{$row["artist"]}'");
}
Is there a more preferable way?
UPDATE: I do not use auto increment and the id column didn't exist at the time of table was created. Now, I've added an id column. There are 300+ records. IDs of the records are unique random strings. Before I add another record to the database, every record needs to have a unique random string for its id, so that when I insert a new record, I can create a random string and check if it's unique or not by comparing it to the ids in the table.
At this stage, I just need to update the id column using an array. Array items are irrelevant.
Prepared statements are designed to be prepared once and executed many times, with a minimum of overhead. Using them is also an absolute necessity if the data you're inserting is user-generated.
It's been a long time since I've done anything with MySQLi (I strongly recommend looking at PDO for much simpler code) but this should work:
$array = [
"C45Rm3fLGn",
"ocIik81up2",
"IcuSn9T77y",
"tJv7AbF53r",
"a9eZ6xYM5Y",
];
$rows = $mysqli->query("SELECT * FROM songs")->fetch_all(MYSQLI_ASSOC);
$stmt = $mysqli->prepare("UPDATE songs SET id=? WHERE song=? AND artist=?");
foreach ($array as $i=>$id) {
$row = $rows[$i];
$stmt->bind_param("sss", $id, $row["song"], $row["artist"]);
$stmt->execute();
}
I would also recommend building your array such that it references the columns you're renaming. You're relying on both the database and the array being in the same order, which may not always be the case.
Please try this query:
UPDATE `myTable` SET `id`= CONCAT(item-name, "-id");
Or you can set a counter like this:
UPDATE `myTable`, (SELECT #i := 0) c SET `id`= CONCAT(item-name, "-id-", #i:=#i+1)
I hope this will help you.
I've tried this: http://be.php.net/manual/en/function.mysql-list-fields.php, but there's too much detail here.
What I'm looking for is a function that returns an array of strings. This array should only contain the names of all columns that a certain table has.
So for instance, if I have a table with these columns:
username | email | gender |age | married | number_of_children | street | province
I should get the same thing as if I did this:
$vars = array('username','email','gender','age','married','number_of_children','street','province');
Is there a PHP function already that can do this? Or shall I need some SQL statements of my own?
you could also do
$query = mysql_query("SELECT * FROM `table`");
$result = mysql_fetch_assoc($query);
$keys = array_keys($result);
$keys will now contain all of the column names because they were the array keys in the $result array.
Theres a MySQL Query, which returns the column names asa result set.
SHOW COLUMNS FROM tablename
I think there is no direct PHP function that will do what you want.
You can:
use that function discarding the unneeded results.
use a SQL query over the INFORMATION_SCHEMA
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'database' AND TABLE_NAME = 'table'
use the MySQL specific 'desc table' or 'show columns from table' SQL sentences