MySQL update two different tables at once (CRUD) - php

I have two tables like this:
[tblFacilityHrs] id uid title description
[tblFacilityHrsDateTimes] owner_uid startEventDate endEventDate startTime endTime days recurrence finalDate
I've joined the table as follows:
$result = mysql_query("SELECT * FROM tblFacilityHrs JOIN tblFacilityHrsDateTimes ON tblFacilityHrs.uid =tblFacilityHrsDateTimes.owner_uid") or trigger_error(mysql_error());
To display both tables I go through each record and put it all in a table:
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
echo "<tr>";
echo "<td valign='top'>" . nl2br( $row['title']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['description']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['startEventDate']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['endEventDate']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['startTime']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['endTime']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['days']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['recurrence']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['finalDate']) . "</td>";
My question is now, how do I edit each record?
In my main page under the table I would normally do this:
pseudo code
echo "<td valign='top'><a href=edit.php?id={$row['id']}>Edit</a></td><td><a href=delete.php?id={$row['id']}>Delete</a></td> ";
Then on my edit page I would do something like this:
pseudo code
if (isset($_GET['id']) ) {
$id = (int) $_GET['id'];
if (isset($_POST['submitted'])) {
foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); }
$sql = "`title` = '{$_POST['title']}' , `description` = '{$_POST['description']}' WHERE `id` = '$id' ";
mysql_query($sql) or die(mysql_error());
echo (mysql_affected_rows()) ? "Edited row.<br />" : "Nothing changed. <br />";
echo "<a href='list.php'>Back</a>";
How does this work since the tables are joined? How can I edit both of them?

I think you should update each Table seperatly ; first the master table and then the detail table , and after that reabind your dataview

Related

Not showing correct info PHP and Oracle query

My question is about an Oracle database which i can connect with PHP. With the same query using SQL Developer i've got this:
But in PHP:
$tsql= "select orden, fase, maquina_id, DescMaterial, dhsalida, scg_MaterialesLotes.lote, scg_MaterialesLotes.lotecli from scg_fases inner join scg_materiales on scg_fases.IdBoletin = scg_Materiales.IdBoletin inner join scg_MaterialesLotes on scg_Materiales.IdMatOf = scg_MaterialesLotes.IdMatOF";
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
{
?>
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
$orden = $row["orden"];
$fase = $row["fase"];
$puesto = $row["maquina_id"];
$art = $row["CodMaterial"];
$desc = $row["DescMaterial"];
$fecha = $row["dhsalida"];
$lote = $row["scg_MaterialesLotes.lote"];
$loteCli = $row["scg_MaterialesLotes.lotecli"];
echo "<tr>";
echo "<td align=center>" . $orden . "</td>";
echo "<td align=center>" . $fase . "</td>";
echo "<td align=center>" . $puesto . "</td>";
echo "<td align=center>" . $art . "</td>";
echo "<td align=center>" . $desc . "</td>";
echo "<td align=center>" . $fecha . "</td>";
echo "<td align=center>" . $lote . "</td>";
echo "<td align=center>" . $loteCli . "</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
<?php
}
else
{
echo "Submission unsuccessful.";
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
Using browser it shows:
Why i can't show the same info? Where is the problem? Thanks
1) Add CodMaterial field to your query You're not including the field CodMaterial in your query, so this line:
$art = $row["CodMaterial"];
Does nothing. So you can either add this field to the long list of fields in your query, or just use SELECT * FROM... to retrieve every field.
2) Don't use tablename.columnname in your query results rows When you query for scg_MaterialesLotes.lote for example, the resulting attribute will be called lote, so instead of having this in your PHP code:
$lote = $row["scg_MaterialesLotes.lote"];
Use this:
$lote = $row["lote"];
Do the same for scg_MaterialesLotes.lotecli
3) Echo $fecha as a string, at the moment is an object of type Instead of using:
echo "<td align=center>" . $fecha . "</td>";
Use this instead:
echo "<td align=center>" . ($fecha ? $fecha->format("Y-m-d H:i:s") : ""). "</td>";

Recursive function does not display anything

I have a tree with nodes and I wanted to display every node's level in a table,
but when it does not display anything.
I tried to put a comment when I do: findlevel($_SESSION['id'], $level +1, $idtofind); at the end of the function, if I do that it works but only for the first level of the Tree.
My code:
function findlevel($parent_id, $level, $idtofind)
{
global $data, $index, $tree;
$parent_id = $parent_id === NULL ? "NULL" : $parent_id;
if (isset($index[$parent_id])) {
foreach ($index[$parent_id] as $id) {
$current_id = $data[$id]["id"];
$levell = $level+1;
if($current_id == $idtofind) {
echo $current_id . " " . $idtofind . $levell . "<br>";
return $levell;
}
findlevel($_SESSION['id'], $level + 1, $idtofind);
}
}
}
Code of my table:
$result = $conn->query("SELECT id, email, tipoutente, fullname, datainserimento, DATEDIFF(datainserimento, '$datainserimentosponsor') AS datadiff FROM utenti WHERE inseritoda = " . $_SESSION['id']);
while($row = mysqli_fetch_array($result)){
echo "<tr class=\"righe\">";
$levelnode =findlevel($_SESSION['id'], 0, $row['id']);
if($row['tipoutente'] == "GRUPPO PRIVATO" || $row['tipoutente'] == "GRUPPO NEGOZI" || $row['tipoutente'] == "GRUPPO ASSOCIAZIONI") {
echo "<td class=\"gruppi\">$progressivoutenti</td>";
echo "<td class=\"gruppi\">" . $row['fullname'] . "</td>";
echo "<td class=\"gruppi\">" . $row['email'] . "</td>";
echo "<td class=\"gruppi\">" . $row['tipoutente'] . "</td>";
echo "<td class=\"gruppi\">" . $row['datainserimento'] . "</td>";
echo "<td class=\"gruppi\">" . $row['datadiff'] . "</td>";
echo "<td class=\"gruppi\">" . $levelnode . "</td>";
//}
} else {
echo "<td class=\"dati\">$progressivoutenti</td>";
echo "<td class=\"dati\">" . $row['fullname'] . "</td>";
echo "<td class=\"dati\">" . $row['email'] . "</td>";
echo "<td class=\"dati\">" . $row['tipoutente'] . "</td>";
echo "<td class=\"dati\">" . $row['datainserimento'] . "</td>";
echo "<td class=\"dati\">" . $row['datadiff'] . "</td>";
echo "<td class=\"dati\">" . $levelnode . "</td>";
}
$progressivoutenti++;
echo "</tr>";
}
echo "</table>";
Edit:
This is the rest of the code:
$data = array();
$index = array();
$albero = array();
$query = $conn->query("SELECT * FROM utenti where utentepadre is not null order by fullname");
$query->data_seek(0);
while ($row = $query->fetch_assoc()) {
$id = $row["id"];
$parent_id = $row["utentepadre"] === NULL ? "NULL" : $row["utentepadre"];
$data[$id] = $row;
$index[$parent_id][] = $id;
}
That's how my tree looks like:
My Tree
And the result is:
The result of the php page.
The result of the variable $data:
Variable $data content

php foreach get all data from database table

I would like to get all the values of: echo "<td class=\"points\">" . $row2['PIY'] . "/" . $row2['PIK'] . "</td>"; Some how it only returns the first on. After that i would like to calculate the sum of them.
At the moment the code only gets the first.
echo "<table class=\"zebra\">";
$sum1=0;
$sum2=0;
$numbering =1;
$query2 = "SELECT pisteet_1 As PIY, pisteet_2 as PIK, nimi As NIM, opisto As OPI, pisteet.kaupunki_id As KA FROM
pisteet INNER JOIN joukkueet ON joukkueet.id = pisteet.team_id INNER JOIN oppilaitokset ON oppilaitokset.opisto_id = joukkueet.opisto_id GROUP BY nimi, team_id ORDER BY team_id ASC";
foreach ($db->query($query2) as $row2) {
echo "<tr class=\"all " . $row2['KA'] . "\">";
echo "<td>" . $numbering . "</td>";
echo "<td>" . $row2['NIM'] ."<span>" . $row2['OPI'] ."</span></td>";
//--------should get all the points----------
echo "<td class=\"points\">" . $row2['PIY'] . "/" . $row2['PIK'] . "</td>";
$sum1 +=$row2['PIY'];
$sum2 +=$row2['PIK'];
echo '<td class="Sum">'.$sum1.'/'.$sum2."</td>";
//-------------------------------------------
echo "</tr>";
$numbering ++;
}
echo '</table>';

mysqli_fetch loop not working

I have a loop inside other loop which is not working, this is the code:
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
echo "</tr>";
}
When I query $result2 directly into the BBDD for the first result it shows three results but the code doesn't go in the second LOOP. Why? Any error here?
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
$result2 = mysqli_query($connection, $result2);
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
echo "</tr>";
}
Use:
$query = "SELECT ....";
$result2 = mysqli_query($db, $query);
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
Before read this How can I prevent SQL injection in PHP? topic. After try to use mysql_query()
Try This
<?php
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
$results = mysqli_query($db,$result2);
while($row2 = mysqli_fetch_array($results))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row['outcomeName'] . "</td>";
}
echo "</tr>";
}
?>
Before fetching data execute mysql query using mysqli_query() function then run mysqli_fetch_array(). It would be good if you count result as $count = mysqli_num_rows($query) and manage your code with if... else .
I think $result2 should be output of mysqli_query not just merely query. Talking in analogous to MySQL.
Probably you should have something like this
$result2 = mysqli_query($result2);

MySQL not pulling all rows

Hey guys, first time using stackoverflow.
can you guys help me debug?
Heres the problem, this query is selecting all of the rows from my database, its only outputting the first one twice for some reason.
$top10_query = "SELECT * FROM kicks";
$result = mysqli_query($cxn, $top10_query) or die("Couldn't execute query.");
$row = mysqli_fetch_assoc($result);
$rating = $row['rating'];
$description = $row['description'];
$completed = $row['completed'];
$userid = $row['userid'];
$posted = $row['posted'];
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td class='rating'>" . $rating . "</td>";
echo "<td class='description'>" . $description . " </td>";
echo "<td class='completed_" . $completed . "'>" . $completed . "</td>";
echo "<td class='author'>";
echo "Posted by: <a href='profile?userid=" . $userid . "'>" . $userid . "</a><br />";
echo "on "; echo $posted;
echo "</td>";
echo "</tr>";
}
You are looping over the rowset, but never retrieving its value more than once. You pulled all of the values out of the first row, and cached them here:
$rating = $row['rating'];
$description = $row['description'];
$completed = $row['completed'];
$userid = $row['userid'];
$posted = $row['posted'];
Move this code into the loop, and remove the first fetch.
You need to update $rating, $description, etc. within the while loop:
<?php
$top10_query = "SELECT * FROM kicks";
$result = mysqli_query($cxn, $top10_query) or die("Couldn't execute query.");
while($row = mysqli_fetch_assoc($result)) {
$rating = $row['rating'];
$description = $row['description'];
$completed = $row['completed'];
$userid = $row['userid'];
$posted = $row['posted'];
echo "<tr>";
echo "<td class='rating'>" . $rating . "</td>";
echo "<td class='description'>" . $description . " </td>";
echo "<td class='completed_" . $completed . "'>" . $completed . "</td>";
echo "<td class='author'>";
echo "Posted by: <a href='profile?userid=" . $userid . "'>" . $userid . "</a><br />";
echo "on "; echo $posted;
echo "</td>";
echo "</tr>";
}
?>
Or, of course, you can inline $rating, etc., writing $row['rating'] instead.
Note: you probably want to run your variables through htmlspecialchars before inserting them into HTML. Otherwise, a description like <script>alert('hacked');</script> could execute a script, opening yourself up to XSS attacks.
You can also use extract. I do not recommend you do this, however, as it may cause problems and confusion for other developers:
<?php
$top10_query = "SELECT * FROM kicks";
$result = mysqli_query($cxn, $top10_query) or die("Couldn't execute query.");
while($row = mysqli_fetch_assoc($result)) {
extract($row);
echo "<tr>";
echo "<td class='rating'>" . $rating . "</td>";
echo "<td class='description'>" . $description . " </td>";
echo "<td class='completed_" . $completed . "'>" . $completed . "</td>";
echo "<td class='author'>";
echo "Posted by: <a href='profile?userid=" . $userid . "'>" . $userid . "</a><br />";
echo "on "; echo $posted;
echo "</td>";
echo "</tr>";
}
?>
The variables $rating etc are not "binded" to the expressions $row['rating'] etc. Once set, They will forever take these values unless you modify them again.
See PHP: Assignment Operators for detail.
Try to rewrite them as:
$top10_query = "SELECT * FROM kicks";
$result = mysqli_query($cxn, $top10_query) or die("Couldn't execute query.");
while($row = mysqli_fetch_assoc($result)) {
$rating = $row['rating']; // <-- use the new value every time a row is fetched.
$description = $row['description'];
$completed = $row['completed'];
$userid = $row['userid'];
$posted = $row['posted'];
echo "<tr>";
echo "<td class='rating'>" . $rating . "</td>";
echo "<td class='description'>" . $description . " </td>";
echo "<td class='completed_" . $completed . "'>" . $completed . "</td>";
echo "<td class='author'>";
echo "Posted by: <a href='profile?userid=" . $userid . "'>" . $userid . "</a><br />";
echo "on "; echo $posted;
echo "</td>";
echo "</tr>";
}

Categories