How to update data using one select in other database table - php

I have two databases and i have one table "TabelaX" in database "Servidor1" with out data and other database "Servidor2" with one table "TabelaY". And i want do one select in table "TabelaY" and with her data do one Update in table "TabelaX" which is in another database. I already made some code but it is not working correctly.
<?php
$conn= mysqli_connect('localhost','root',null,'Servidor2') or die
(mysqli_connect_error());
if (!$conn) {
die("Falha de conexao: ". mysqli_connect_error());
}
$ID = $_POST['ID'];
$sql = "SELECT * FROM TabelaY WHERE ID = $ID";
$result = mysqli_query($conn, $sql);
mysqli_select_db($conn,"Servidor1");
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$row1 = $row["ID"];
$row2 = $row["Data"];
}
} else {
echo "0 results";
}
$sql = "INSERT INTO Servidor1.TabelaX (ID, Data)
SELECT ID, Data
FROM Servidor3.TabelaW
WHERE ID = $ID;";
$sql = "UPDATE Servidor1.TabelaX SELECT ID, Data FROM
Servidor3.TabelaW SET Data = $row2 WHERE $row1 = $ID;";
if (mysqli_multi_query($conn, $sql)) {
echo "Dados Inseridos";
} if (mysqli_multi_query($conn, $sql)) {
echo "Dados Atualizados";
}
mysqli_close($conn);

I have no idea what your query is trying to do, because you assign to $sql twice without ever executing the first query, but if you're asking how to update a row in tableX based on data from tableY, then:
UPDATE Servidor1.TabelaX as x, Servidor2.TabelaY as y
SET x.Data = y.Data
WHERE x.id = y.id
AND x.id = $someIdForWhichYouWantToUpdate
Also, do not do this:
$ID = $_POST['ID'];
$sql = "SELECT * FROM TabelaY WHERE ID = $ID";
Imagine what happens when the user posts 1; DROP DATABASE Servidor1 into the form. This is called SQL injection and your code is full of vulnerabilities to it.

Related

Multiple MySQL Queries (first one to get an id based on the user) second one to get information of another project in a database with the id

I am trying to accomplish the following:
Get the project id from a database where the username =...
-> Users can be signed into different projects and it is being tracked in a database
Next step is to get information of the project database to display it for the user
-> But since the user can be signed into different projects I want him to see them underneath each other in a not yet specific order (I a
This is basically what I was trying to get to work and it displays the right content but unfortunately stops at the first project. My assumption is that it stops at the first project id and doesn't continue
-> I checked to see what code of the code works and as soon as is cut the information gathering query I get all project_id's
Here is basically the code that I am working with right now:
$sql = "SELECT project_id FROM project_members_db WHERE user_ID = '$user_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
//Album Informationen ziehen herausfinden
$sql = "SELECT project_name, sticker_count, manufacturer, sport, creation_datetime FROM project_db WHERE project_id = '$row[project_id]'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
//OUTPUT
echo "
$row['project_name'];
$row['count'];
$row['sport'];
$row['manufacturer'];
$row['creation_datetime'];
";
}
}
}
}
It looks like you ought to be able to do this with a single query - and as you are taking user supplied input you ought to consider using a prepared statement
$sql='select `project_name`, `sticker_count`, `manufacturer`, `sport`, `creation_datetime`
from `project_db` where `project_id` = ( select `project_id` from `project_members` where `user_id` = ? )';
$stmt=$conn->prepare( $sql );
$stmt->bind_param('s',$user_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($project_name,$sticker_count,$manufacturer,$sport,$creation_datetime);
while( $stmt->fetch() ){
echo $project_name,$sticker_count,$manufacturer,$sport,$creation_datetime;
}
You overwrite the first result with the second result. Change to:
$sql = "SELECT project_id FROM project_members_db WHERE user_ID = '$user_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//Album Informationen ziehen herausfinden
$sql = "SELECT project_name, sticker_count, manufacturer, sport, creation_datetime FROM project_db WHERE project_id = '$row[project_id]'";
$result2 = $conn->query($sql);
if ($result2->num_rows > 0) {
// output data of each row
while($row2 = $result2->fetch_assoc()) {
//OUTPUT
echo $row2['project_name'] . ' '
$row2['count'] . ' '
$row2['sport'] . ' '
$row2['manufacturer'] . ' '
$row2['creation_datetime'];
}
}
}
}
Note putting arrays in your string doesn't work, so i have concatenated the echo instead.

How to apply this code to get mysql to php output?

I am making a database with a html output to show the data from my 4 tables.
The 4 tables have been made through normalization from 1 big table.
I have done this with just 1 table after getting the mysql_query(select bla bla)
while($row = mysqli_fetch_assoc($result)) {
echo $row["Kod_Barang"].' nex row '.$row["Nama_Barang"];
}
} else {
echo "0 results";
The coding i currently have
<?php
$resultstok = mysqli_query($connection, "SELECT Kod_Rak, Harga, Tarikh_Jual from stok");
$resultbarang = mysqli_query($connection, "SELECT Nama_Barang, Bil_Barang from barang");
$resultlokasi = mysqli_query($connection, "SELECT Jenis_Barang from lokasi");
$resulttarikh = mysqli_query($connection, "SELECT Tarikh_Beli from tarikh");
if (mysqli_num_rows($resultstok) > 0) {
$rowstok = mysqli_fetch_assoc($resultstok);
$rowbarang = mysqli_fetch_assoc($reseltbarang);
$rowlokasi = mysqli_fetch_assoc($resultlokasi);
$rowtarikh = mysqli_fetch_assoc($resulttarikh);
while
}
?>
I expect to get each of the data out but i dont know how to apply the first code into the 2nd

Profile will not display when called. php - mysql

<?php
if (!isset($_POST['submitted'])) {//1
// Checs for the ID
if (isset($_GET['id']) && is_numeric($_GET['id'])) {//2
// MySQL Connect
require_once('mysql_connect.php');
$id = mysql_real_escape_string($_GET['id']);
$query = "SELECT id, name FROM websites WHERE id = $id";
$result = mysql_query($query) OR die (mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
?>
// ROW WITH THE ERROR
<?php echo $row['name']; ?></strong><br /><?php echo $row['banner']; ?><? echo $row['description'];?>
<?php
} else {
echo '<font color="red">You have to select a server to view</font>';
die();
}
} else {
// MySQL Connect
require_once('mysql_connect.php');
$id = mysql_real_escape_string($_POST['id']);
// Choose the web for votes
$query = "SELECT id, votes FROM websites WHERE id = $id";
$result = mysql_query($query) OR die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$votes = $row['votes'];
$url = $row['url'];
$id = $row['id'];
$banner = $row['banner'];
$result = mysql_query($query) OR die(mysql_error());
} // end
?>
All that is printing is the Name, the rest is not being printed.
I'm just wondering where i'm going wrong?
Its supposed to print the Name, Banner, and description from $id.
You never actually select banner and description in your query so they are not available in your resultset.
$query = "SELECT id, name, banner, description FROM websites WHERE id = $id";
You need to specify ALL desired fields that you wish to retrieve in your SQL query:
$query = "SELECT id, name, banner, description FROM websites WHERE id = $id";
Alternatively, use SELECT * FROM websites to retrieve all available rows.

Multiple SELECT Statements and INSERTS in 1 file

I'm working with a file and I'm attempting to do multiple select statements one after another and insert some values. So far the insert and the select I've got working together but when attempting to get the last SELECT to work I get no value. Checking the SQL query in workbench and everything works fine. Here's the code:
$id = "SELECT idaccount FROM `animator`.`account` WHERE email = '$Email'";
$result = mysqli_query($dbc, $id) or die("Error: ".mysqli_error($dbc));
while($row = mysqli_fetch_array($result))
{
echo $row[0];
$insert_into_user = "INSERT INTO `animator`.`user` (idaccount) VALUES ('$row[0]')";
}
$select_userid = "SELECT iduser FROM `animator`.`user` WHERE iduser = '$row[0]'";
$results = mysqli_query($dbc, $select_userid) or die("Error: ".mysqli_error($dbc));
while($rows = mysqli_fetch_array($results))
{
echo $rows[0];
}
I do not want to use $mysqli->multi_query because of previous problems I ran into. Any suggestions? And yes I know the naming conventions are close naming... They will be changed shortly.
Your code makes no sense. You repeatedly build/re-build the $insert_int-User query, and then NEVER actually execute the query. The $select_userid query will use only the LAST retrieved $row[0] value from the first query. Since that last "row" will be a boolean FALSE to signify that no more data is available $row[0] will actually be trying to de-reference that boolean FALSE as an array.
Since you're effectively only doing 2 select queries (or at least trying to), why not re-write as a single two-value joined query?
SELECT iduser, idaccount
FROM account
LEFT JOIN user ON user.iduser=account.idaccount
WHERE email='$Email';
I'm not sure what you're trying to do in your code exactly but that a look at this...
// create select statement to get all accounts where email=$Email from animator.account
$id_query = "SELECT idaccount FROM animator.account WHERE email = '$Email'";
echo $id_query."\n";
// run select statement for email=$mail
$select_results = mysqli_query($dbc, $id_query) or die("Error: ".mysqli_error($dbc));
// if we got some rows back from the database...
if ($select_results!==false)
{
$row_count = 0;
// loop through all results
while($row = mysqli_fetch_array($result))
{
$idaccount = $row[0];
echo "\n\n-- Row #$row_count --------------------------------------------\n";
echo $idaccount."\n";
// create insert statement for this idaccount
$insert_into_user = "INSERT INTO animator.user (idaccount) VALUES ('$idaccount')";
echo $insert_into_user."\n";
// run insert statement for this idaccount
$insert_results = mysqli_query($dbc, $insert_into_user) or die("Error: ".mysqli_error($dbc));
// if our insert statement worked...
if ($insert_results!==false)
{
// Returns the auto generated id used in the last query
$last_inisert_id = mysqli_insert_id($dbc);
echo $last_inisert_id."\n";
}
else
{
echo "insert statement did not work.\n";
}
$row_count++;
}
}
// we didn't get any rows back from the DB for email=$Email
else
{
echo "select query returned no results...? \n";
}

get multiple values from table1, change them to values based on table2 and write the result to table1

hi
i can rewrite a column's values in a mysql databse based on another columns values, but this time i need to do this with a twist...
there are two tables (and multiple values at once): specifications and products.
products has two columns: specificationids and specificationorderids
specifications has two columns aswell: id and specificationorder
specificationids has multiple values formatted like this: ,31,29,27,18,
these are also the id in the specifications table. each id has a specificationorder value (same row, other column). now what i want to do is, that i want to swap the values of the id with the value of specificationorder and write these to specificationorderids in the products table in the same format.
ofcourse this process has to loop through all the id's in the products table.
i hope i made the problem clear and understandable
thanks for your help!
Swap id and specificationorder, specificationorder writen in specificationorderids if I understand you:
<?php
/* Swap id and specificationorder */
$sql = "SELECT id, specificationorder from specifications";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result))
{
$id = $row[0];
$sporder = $row[1];
$sql = "UPDATE specifications SET id=$id, specificationorder=$sporder WHERE id=$id";
mysql_query($sql);
}
/* specificationorder writen in specificationorderids */
$sql = "SELECT specificationorder from specifications";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result))
{
$sporder = $row[0];
$sql = "INSERT INTO products(specificationorderids) VALUES($sporder)";
mysql_query($sql);
}
?>
for the past couple days i havent been able to come up with a normal code, so heres what ive got so far. ive added comments so you know what i was thinking when experimenting with the code... as in my previous comment i include an image of what i want to achive: img822.imageshack.us/i/specm.png
//get specificationids
$sql = "SELECT specificationids from products";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$spids = $row[0];
//get specifications
$sql2 = "SELECT id from specifications";
$result2 = mysql_query($sql2);
$row2 = mysql_fetch_row($result2);
$spid = $row2[0];
//get specificationorder
$sql3 = "SELECT specificationorder from specifications";
$result3 = mysql_query($sql3);
$row3 = mysql_fetch_row($result3);
$sporder = $row3[0];
//if the value of specificationids matches a specificationid, then....
while(VALUES($spids)==$spid)
{
$spids=$sporder; //...it should be replaced by the specificationorder
//and then update the specificationorderids column accordingly
$sql = "UPDATE products(specificationorderids) VALUES($spids)";
mysql_query($sql);
}

Categories