Merge an update form with Joined Tables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spikey246
Forum Newbie
Posts: 5
Joined: Tue Mar 31, 2015 3:24 am

Merge an update form with Joined Tables

Post by spikey246 »

Hi all, I have a page to update but it's gotten a bit above my limit. I know what I want to do but putting two things together is proving slightly difficult.

Information is being pulled from two tables to display varius items.

However in a table I wish to update called 'Snippets' there is a collumn called 'Order' (orders the order snippets are in).
There is a form echoing the Order number (int). Change number, click save changes the order.

I'm not sure how to get the update script in (located at bottom of following script) to the current one with the joins to get the update forms to work?

Thanks in advance if someone can help me understand how to get this working.

Code: Select all

<?php

session_start();



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="/styles/style.css">
<link rel="stylesheet" href="/styles/responsive.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</script>
<title>Title</title>
</head>
<body>
<div class="container">
    	<div class="admin_header">
        <?php require('includes/header.php'); ?>
        </div>
        <div class="main_nav">
        <?php require('includes/admin_nav.php'); ?>
        </div>
        <div class="content">
        <p>&nbsp;</p>
<div class="news">
<div class="heading">
<?php
if($_SESSION['username'])
{
	echo "
	
	Hello again, ".$_SESSION['username'];
}
	else
		header ("location: login.php");
?>
<?php
require('../db/db_connection.php');
require('../db/security.php');
?>
</div>

</div>
<div class="article_wrap">
<div class="articles"><div class="articles_heading_wrap"><div class="articles_heading"><h4><?php echo'{$row->Campaignname}'?> </h4></div></div><div class="update"> (random)</div></div>

<!-- SCRIPTS TO EDIT AND SEND-->
<?php
if(!empty($_GET['id']))
{
   $sql = "
SELECT snippets.id AS snipid, snippets.Title, snippets.Link, snippets.Text, snippets.Created AS Created, camp_names.ribbon as Ribbon, camp_names.alt_text, camp_names.name as Campaignname, camp_names.id AS campnamesid, snippets.Order AS Ordref
FROM snippets
LEFT JOIN camp_names ON snippets.Campaign = camp_names.id
WHERE Campaign = \"" .(int)$_GET['id'] . "\"
ORDER BY Ordref ASC
";
    // then do the query, etc....
} 

$results = $db->query($sql);

if($results->num_rows) {
	While($row = $results->fetch_object()) {
		echo "
		
<div class='snippets'>
	<div class='title'><strong>{$row->Title}</strong></div>
	<div class='ribbon_wrapper'>
    	<div class='ribbon'><img src='{$row->Ribbon}' alt='{$row->alt_text}' /></div>
        <div class='ribbon_text'>
		<form action='campaign.php?id=<?php echo $id; ?>' method='POST' border='0'>
		<input name='Order' type='text' value='{$row->Ordref}' size='2' />
		<input type='submit' value='save'/> <a href='edit.php?id={$row->snipid}'>Edit</a> | <a href='delete.php?id={$row->snipid}'>Delete</a>
		</form></div>
    </div>
</div>	
		
";
	}
} else {
	echo 'No Results';
}
?>
</div>
<p>&nbsp;</p>
        </div>
        <div class="footer">
        <?php require('/includes/footer.php'); ?>
        </div>
  </div>
<script>
	$('.handle').on('click', function(){
		$('main_nav ul').toggleClass('showing');
	});
</script>
</body>
</html>

<!-- THIS IS THE UPDATE SCRIPT I'M TRYIN TO IMPLEMENT-->
<?PHP
if($_POST){
 
    //write query
    $sql = "UPDATE
                snippets
            SET
				Order = ?
            WHERE
                id= ?";
 
    $stmt = $db->prepare($sql);
 
    // you can bind params this way,
    // if you want to see the other way, see our add.php
    $stmt->bind_param(
        'ii',
		$_POST['Order'],
        $_POST['id']
    );
 
    // execute the update statement
    if($stmt->execute()){
        echo "Entry Updated.";
 
        // close the prepared statement
        $stmt->close();
    }else{
        die("Unable to update.");
    }
}
 
$sql = "SELECT
            id, Order
        FROM
            snippets
        WHERE
            id = \"" . $db->real_escape_string($_GET['id']) . "\"
";
 
// execute the sql query
$result = $db->query( $sql );
 
//get the result
$row = $result->fetch_assoc();
 
extract($row);
 
//disconnect from database
$result->free();
$db->close();
?>
Post Reply